8000 minor #12951 Update Console Input Optional Options (Lopton) · symfony/symfony-docs@c1e9043 · GitHub
[go: up one dir, main page]

Skip to content

Commit c1e9043

Browse files
committed
minor #12951 Update Console Input Optional Options (Lopton)
This PR was submitted for the 5.0 branch but it was merged into the 4.4 branch instead (closes #12951). Discussion ---------- Update Console Input Optional Options The documentation for the Option Input commands was not easy to follow. This leads to confusion such as in (symfony/symfony#29228). Modified to make it more clear what the different outcomes are and to provide a simple example before the condensed example. Commits ------- 7315664 Update input.rst
2 parents 013132f + 7315664 commit c1e9043

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

console/input.rst

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,12 @@ optionally accepts a value, but it's a bit tricky. Consider this example::
247247
)
248248
;
249249

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
252252
without a value (``greet --yell``) and not passing the option (``greet``).
253253

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``::
255256

256257
// ...
257258
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``::
267268
)
268269
;
269270

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``::
271297

272298
$optionValue = $input->getOption('yell');
273299
$yell = ($optionValue !== false);

0 commit comments

Comments
 (0)
0