-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Finish 3744 #4405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Finish 3744 #4405
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
.. index:: | ||
single: Console; Console arguments | ||
|
||
Understand how Console Arguments are Handled | ||
============================================ | ||
|
||
It can be difficult to understand the way arguments are handled by the console application. | ||
The Symfony Console application, like many other CLI utility tools, follows the behavior | ||
described in the `docopt`_ standards. | ||
|
||
Have a look at the following command that has three options:: | ||
|
||
namespace Acme\Console\Command; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class DemoArgsCommand extends Command | ||
{ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('demo:args') | ||
->setDescription('Describe args behaviors') | ||
->setDefinition( | ||
new InputDefinition(array( | ||
new InputOption('foo', 'f'), | ||
new InputOption('bar', 'b', InputOption::VALUE_REQUIRED), | ||
new InputOption('cat', 'c', InputOption::VALUE_OPTIONAL), | ||
)) | ||
); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
// ... | ||
} | ||
} | ||
|
||
Since the ``foo`` option doesn't accept a value, it will be either ``false`` | ||
(when it is not passed to the command) or ``true`` (when ``--foo`` was passed | ||
by the user). The value of the ``bar`` option (and its ``b`` shortcut respectively) | ||
is required. It can be separated from the option name either by spaces or | ||
``=`` characters. The ``cat`` option (and its ``c`` shortcut) behaves similar | ||
except that it doesn't require a value. Have a look at the following table | ||
to get an overview of the possible ways to pass options: | ||
|
||
===================== ========= =========== ============ | ||
Input ``foo`` ``bar`` ``cat`` | ||
===================== ========= =========== ============ | ||
``--bar=Hello`` ``false`` ``"Hello"`` ``null`` | ||
``--bar Hello`` ``false`` ``"Hello"`` ``null`` | ||
``-b=Hello`` ``false`` ``"Hello"`` ``null`` | ||
``-b Hello`` ``false`` ``"Hello"`` ``null`` | ||
``-bHello`` ``false`` ``"Hello"`` ``null`` | ||
``-fcWorld -b Hello`` ``true`` ``"Hello"`` ``"World"`` | ||
``-cfWorld -b Hello`` ``false`` ``"Hello"`` ``"fWorld"`` | ||
``-cbWorld`` ``false`` ``null`` ``"bWorld"`` | ||
===================== ========= =========== ============ | ||
|
||
Things get a little bit more tricky when the command also accepts an optional | ||
argument:: | ||
|
||
// ... | ||
|
||
new InputDefinition(array( | ||
// ... | ||
new InputArgument('arg', InputArgument::OPTIONAL), | ||
)); | ||
|
||
You might have to use the special ``--`` separator to separate options from | ||
arguments. Have a look at the fifth example in the following table where it | ||
is used to tell the command that ``World`` is the value for ``arg`` and not | ||
the value of the optional ``cat`` option: | ||
|
||
============================== ================= =========== =========== | ||
Input ``bar`` ``cat`` ``arg`` | ||
============================== ================= =========== =========== | ||
``--bar Hello`` ``"Hello"`` ``null`` ``null`` | ||
``--bar Hello World`` ``"Hello"`` ``null`` ``"World"`` | ||
``--bar "Hello World"`` ``"Hello World"`` ``null`` ``null`` | ||
``--bar Hello --cat World`` ``"Hello"`` ``"World"`` ``null`` | ||
``--bar Hello --cat -- World`` ``"Hello"`` ``null`` ``"World"`` | ||
``-b Hello -c World`` ``"Hello"`` ``"World"`` ``null`` | ||
============================== ================= =========== =========== | ||
|
||
.. _docopt: http://docopt.org/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,6 @@ Console | |
introduction | ||
usage | ||
single_command_tool | ||
console_arguments | ||
events | ||
helpers/index |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should "be
--yell
,--yell=loud
or--yell loud
work"?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea - we have too many or's :). Can you open up a quick PR? I'm on my phone now :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i created a PR -> #4691