@@ -247,11 +247,12 @@ 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: ``--yell ``, ``yell=louder ``, and not passing
251
- the option at all . 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 ``false ``::
254
+ To solve this issue, you have to set the option's default value to
255
+ ``false ``::
255
256
256
257
// ...
257
258
use Symfony\Component\Console\Input\InputOption;
@@ -267,7 +268,32 @@ To solve this issue, you have to set the option's default value to ``false``::
267
268
)
268
269
;
269
270
270
- Now check the value of the option and keep in mind that ``false !== null ``::
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::
275
+
276
+ $optionValue = $input->getOption('yell');
277
+ if ($optionValue === false ) {
278
+ // option was not specified
279
+ $yell = false;
280
+ $yellLouder = false;
281
+ } elseif ($optionValue === null) {
282
+ // option was specified but no value given
283
+ $yell = true;
284
+ $yellLouder = false;
285
+ } else {
286
+ // option was specified with a value which is now stored in $optionValue
287
+ $yell = true;
288
+ if ($optionValue === 'louder') {
289
+ $yellLouder = true;
290
+ } else {
291
+ $yellLouder = false;
292
+ }
293
+ }
294
+
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 ``::
271
297
272
298
$optionValue = $input->getOption('yell');
273
299
$yell = ($optionValue !== false);
0 commit comments