@@ -247,12 +247,11 @@ optionally accepts a value, but it's a bit tricky. Consider this example::
247
247
)
248
248
;
249
249
250
- This option can be used in 3 ways: ``greet --yell ``, ``greet yell=louder ``,
251
- and ``greet ``. However, it's hard to distinguish between passing the option
250
+ This option can be used in 3 ways: ``greet --yell ``, ``greet yell=louder ``,
251
+ and ``greet ``. However, it's hard to distinguish between passing the option
252
252
without a value (``greet --yell ``) and not passing the option (``greet ``).
253
253
254
- To solve this issue, you have to set the option's default value to
255
- ``false ``::
254
+ To solve this issue, you have to set the option's default value to ``false ``::
256
255
257
256
// ...
258
257
use Symfony\Component\Console\Input\InputOption;
@@ -268,32 +267,31 @@ To solve this issue, you have to set the option's default value to
268
267
)
269
268
;
270
269
271
- The input will now return the default value for the option when it is not
272
- specified (``greet ``), a null value when it is specified but not explicitly
273
- defined (``greet --yell ``), and the defined value when defined
274
- (``greet --yell=lounder ``). Now check the value of the option::
270
+ Now it's possible to differentiate between not passing the option and not
271
+ passing any value for it::
275
272
276
273
$optionValue = $input->getOption('yell');
277
- if ($optionValue === false ) {
278
- // option was not specified
274
+ if (false === $optionValue ) {
275
+ // in this case, the option was not passed when running the command
279
276
$yell = false;
280
- $yellLouder = false;
281
- } elseif ($optionValue === null) {
282
- // option was specified but no value given
277
+ $yellLouder = false;
278
+ } elseif (null === $optionValue) {
279
+ // in this case, the option was passed when running the command
280
+ // but no value was given to it
283
281
$yell = true;
284
- $yellLouder = false;
282
+ $yellLouder = false;
285
283
} else {
286
- // option was specified with a value which is now stored in $optionValue
284
+ // in this case, the option was passed when running the command and
285
+ // some specific value was given to it
287
286
$yell = true;
288
- if ($optionValue === 'louder' ) {
289
- $yellLouder = true;
287
+ if ('louder' === $optionValue ) {
288
+ $yellLouder = true;
290
289
} else {
291
- $yellLouder = false;
290
+ $yellLouder = false;
292
291
}
293
292
}
294
293
295
- Once you are clear on how the default value is implemented you could consense the
296
- above code into less lines of code since ``false !== null ``::
294
+ The above code can be simplified as follows because ``false !== null ``::
297
295
298
296
$optionValue = $input->getOption('yell');
299
297
$yell = ($optionValue !== false);
0 commit comments