Skip to content

Commit 260e8a5

Browse files
authored
chore: clean-up the console option descriptions (#27)
1 parent 4847549 commit 260e8a5

3 files changed

Lines changed: 111 additions & 60 deletions

File tree

src/Console/Application.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use FormatPHP\Console\Command\PseudoLocaleCommand;
2727
use Symfony\Component\Console\Application as SymfonyConsoleApplication;
2828
use Symfony\Component\Console\Exception\LogicException;
29+
use Symfony\Component\Console\Input\InputOption;
2930

3031
/**
3132
* FormatPHP console application
@@ -40,8 +41,49 @@ class Application extends SymfonyConsoleApplication
4041
public function __construct()
4142
{
4243
parent::__construct(self::NAME);
44+
$this->cleanUpOptions();
4345

4446
$this->add(new ExtractCommand());
4547
$this->add(new PseudoLocaleCommand());
4648
}
49+
50+
private function cleanUpOptions(): void
51+
{
52+
$definition = $this->getDefaultInputDefinition();
53+
54+
$helpOption = new InputOption(
55+
'help',
56+
'h',
57+
InputOption::VALUE_NONE,
58+
'Display help for the given command.',
59+
);
60+
$verboseOption = new InputOption(
61+
'verbose',
62+
'v|vv|vvv',
63+
InputOption::VALUE_NONE,
64+
'Increase the verbosity of messages.',
65+
);
66+
67+
$newOptions = [];
68+
$options = $definition->getOptions();
69+
foreach ($options as $option) {
70+
if ($option->getName() === 'help') {
71+
$newOptions[] = $helpOption;
72+
73+
continue;
74+
}
75+
76+
if ($option->getName() === 'verbose') {
77+
$newOptions[] = $verboseOption;
78+
79+
continue;
80+
}
81+
82+
$newOptions[] = $option;
83+
}
84+
85+
$definition->setOptions($newOptions);
86+
87+
$this->setDefinition($definition);
88+
}
4789
}

src/Console/Command/ExtractCommand.php

Lines changed: 65 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
use function explode;
4747
use function getcwd;
4848

49+
use const PHP_EOL;
50+
4951
/**
5052
* Provides the `formatphp extract` command
5153
*/
@@ -78,100 +80,107 @@ protected function configure(): void
7880
->addArgument(
7981
'files',
8082
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
81-
'One or more paths to process for extraction. May use glob '
82-
. 'patterns for file matching; supports globstar (`**`) '
83-
. 'for recursive matching.',
83+
'One or more paths to process for extraction.' . PHP_EOL
84+
. 'May use glob patterns for file matching;' . PHP_EOL
85+
. 'supports globstar (**) for recursive matching.',
8486
)
8587
->addOption(
86-
'format',
88+
'--format',
8789
null,
8890
InputOption::VALUE_REQUIRED,
89-
'Formatter name or path to a formatter script that controls '
90-
. 'the shape of the JSON produced for `--out-file`.',
91+
'Formatter name or path to a formatter script' . PHP_EOL
92+
. 'that controls the shape of the JSON produced' . PHP_EOL
93+
. 'for `--out-file`.',
9194
)
9295
->addOption(
93-
'out-file',
96+
'--out-file',
9497
null,
9598
InputOption::VALUE_REQUIRED,
96-
'Target file path to save the JSON output file of all '
97-
. 'translations extracted from the `files`.',
99+
'Target file path to save the JSON output file' . PHP_EOL
100+
. 'of all translations extracted from the `files`.',
98101
)
99102
->addOption(
100-
'id-interpolation-pattern',
103+
'--ignore',
101104
null,
102-
InputOption::VALUE_REQUIRED,
103-
'If message descriptors are missing the id property, we will '
104-
. 'use this pattern to automatically generate IDs for '
105-
. 'them. Defaults to `[sha512:contenthash:base64:6]` where '
106-
. '`contenthash` represents the hash of `defaultMessage` '
107-
. 'and `description`.',
105+
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
106+
'Glob patterns for paths to exclude from' . PHP_EOL
107+
. 'extraction.',
108108
)
109109
->addOption(
110-
'extract-source-location',
110+
'--flatten',
111111
null,
112112
InputOption::VALUE_NONE,
113-
'Whether to extract metadata for the source files. If present, '
114-
. 'the extracted descriptors will each include `file`, '
115-
. '`start`, `end`, `line`, and `col` properties.',
113+
'Whether to hoist selectors & flatten sentences' . PHP_EOL
114+
. 'as much as possible, e.g: "I have {count,' . PHP_EOL
115+
. 'plural, one{a dog} other{many dogs}}" becomes' . PHP_EOL
116+
. '"{count, plural, one{I have a dog} other{I have' . PHP_EOL
117+
. 'many dogs}}". The goal is to provide as many' . PHP_EOL
118+
. 'full sentences as possible, since fragmented' . PHP_EOL
119+
. 'sentences are not translator-friendly.',
116120
)
117121
->addOption(
118-
'additional-function-names',
122+
'--extract-source-location',
119123
null,
120-
InputOption::VALUE_REQUIRED,
121-
'Comma-separated list of additional function names to search '
122-
. 'for when extracting messages.',
124+
InputOption::VALUE_NONE,
125+
'Whether to extract source file metadata. If' . PHP_EOL
126+
. 'present, the extracted descriptors will each' . PHP_EOL
127+
. 'include `file`, `start`, `end`, `line`, and' . PHP_EOL
128+
. '`col` properties.',
123129
)
124130
->addOption(
125-
'parser',
126-
'p',
127-
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
128-
'Parser name or path to a parser script to apply additional '
129-
. 'parsing in addition to the default PHP parsing.',
131+
'--addl-func',
132+
null,
133+
InputOption::VALUE_REQUIRED,
134+
'Comma-separated list of additional function' . PHP_EOL
135+
. 'names to search for when extracting messages.',
130136
)
131137
->addOption(
132-
'ignore',
138+
'--pragma',
133139
null,
134-
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
135-
'Glob patterns for paths to exclude from extraction.',
140+
InputOption::VALUE_REQUIRED,
141+
'Allows parsing of additional custom pragma to' . PHP_EOL
142+
. 'include custom metadata in the extracted' . PHP_EOL
143+
. 'messages.',
136144
)
137145
->addOption(
138-
'throws',
146+
'--preserve-whitespace',
139147
null,
140148
InputOption::VALUE_NONE,
141-
'Whether to throw an exception when failing to process any '
142-
. 'file in the batch.',
149+
'Whether to preserve whitespace and newlines in' . PHP_EOL
150+
. 'extracted messages.',
143151
)
144152
->addOption(
145-
'pragma',
146-
null,
147-
InputOption::VALUE_REQUIRED,
148-
'Allows parsing of additional custom pragma to include custom '
149-
. 'metadata in the extracted messages.',
153+
'--parser',
154+
'-p',
155+
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
156+
'Parser name or path to a parser script to apply' . PHP_EOL
157+
. 'additional parsing in addition to the default' . PHP_EOL
158+
. 'PHP parsing.',
150159
)
151160
->addOption(
152-
'preserve-whitespace',
161+
'--throws',
153162
null,
154163
InputOption::VALUE_NONE,
155-
'Whether to preserve whitespace and newlines in extracted '
156-
. 'messages.',
164+
'Whether to throw an exception when failing to' . PHP_EOL
165+
. 'process any file in the batch.',
157166
)
158167
->addOption(
159-
'flatten',
168+
'--id-pattern',
160169
null,
161-
InputOption::VALUE_NONE,
162-
'Whether to hoist selectors & flatten sentences as much as possible, '
163-
. 'e.g: "I have {count, plural, one{a dog} other{many dogs}}" '
164-
. 'becomes "{count, plural, one{I have a dog} other{I have many '
165-
. 'dogs}}". The goal is to provide as many full sentences as '
166-
. 'possible, since fragmented sentences are not translator-friendly.',
170+
InputOption::VALUE_REQUIRED,
171+
'If message descriptors are missing the id' . PHP_EOL
172+
. 'property, we will use this to autogenerate IDs.' . PHP_EOL
173+
. 'Defaults to `[sha512:contenthash:base64:6]`' . PHP_EOL
174+
. 'where `contenthash` represents the hash of' . PHP_EOL
175+
. '`defaultMessage` and `description`.',
167176
)
168177
->addOption(
169-
'add-missing-ids',
178+
'--add-missing-ids',
170179
null,
171180
InputOption::VALUE_NONE,
172-
'Whether to update the source code in place with autogenerated '
173-
. 'descriptor IDs. Descriptors that already have IDs will '
174-
. 'remain untouched.',
181+
'Whether to update the source code in place with' . PHP_EOL
182+
. 'autogenerated descriptor IDs. Descriptors that' . PHP_EOL
183+
. 'already have IDs will not change.',
175184
);
176185
}
177186

@@ -220,7 +229,7 @@ private function buildOptions(InputInterface $input): MessageExtractorOptions
220229
$options->outFile = $outFile;
221230

222231
/** @var string | null $idInterpolationPattern */
223-
$idInterpolationPattern = $input->getOption('id-interpolation-pattern');
232+
$idInterpolationPattern = $input->getOption('id-pattern');
224233
$options->idInterpolationPattern = $idInterpolationPattern ?? IdInterpolator::DEFAULT_ID_INTERPOLATION_PATTERN;
225234

226235
/** @var string[] $parsers */
@@ -242,7 +251,7 @@ private function buildOptions(InputInterface $input): MessageExtractorOptions
242251
$options->addGeneratedIdsToSourceCode = (bool) $input->getOption('add-missing-ids');
243252

244253
/** @var string $inputFunctionNames */
245-
$inputFunctionNames = $input->getOption('additional-function-names') ?? '';
254+
$inputFunctionNames = $input->getOption('addl-func') ?? '';
246255
$additionalFunctionNames = array_map('trim', explode(',', $inputFunctionNames));
247256
$options->functionNames = array_unique(array_merge($options->functionNames, $additionalFunctionNames));
248257

src/Console/Command/PseudoLocaleCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,22 @@ protected function configure(): void
6363
. 'One of: ' . implode(', ', PseudoLocale::LOCALES),
6464
)
6565
->addOption(
66-
'in-format',
66+
'--in-format',
6767
null,
6868
InputOption::VALUE_REQUIRED,
6969
'Formatter name or path to a formatter script that' . PHP_EOL
7070
. 'controls the shape of the JSON read from `file`.' . PHP_EOL
7171
. 'Defaults to "formatphp".',
7272
)
7373
->addOption(
74-
'out-format',
74+
'--out-format',
7575
null,
7676
InputOption::VALUE_REQUIRED,
7777
'Formatter name or path to a formatter script that' . PHP_EOL
78-
. 'controls the shape of the JSON produced as output.',
78+
. 'controls the shape of the JSON output produced.',
7979
)
8080
->addOption(
81-
'out-file',
81+
'--out-file',
8282
null,
8383
InputOption::VALUE_REQUIRED,
8484
'Target file path to save the JSON output.',

0 commit comments

Comments
 (0)