|
| 1 | +.. index:: |
| 2 | + single: Console; Console arguments |
| 3 | + |
| 4 | +Understand how Console Arguments are Handled |
| 5 | +============================================ |
| 6 | + |
| 7 | +It can be difficult to understand the way arguments are handled by the console application. |
| 8 | +The Symfony Console application, like many other CLI utility tools, follows the behavior |
| 9 | +described in the `docopt`_ standards. |
| 10 | + |
| 11 | +Let's see a complete example on how the arguments are understood by Console application, |
| 12 | +regarding to the Console Options or Arguments defined in the application:: |
| 13 | + |
| 14 | + namespace Acme\Console\Command; |
| 15 | + |
| 16 | + use Symfony\Component\Console\Command\Command; |
| 17 | + use Symfony\Component\Console\Input\InputArgument; |
| 18 | + use Symfony\Component\Console\Input\InputInterface; |
| 19 | + use Symfony\Component\Console\Input\InputOption; |
| 20 | + use Symfony\Component\Console\Output\OutputInterface; |
| 21 | + |
| 22 | + class DemoArgsCommand extends Command |
| 23 | + { |
| 24 | + protected function configure() |
| 25 | + { |
| 26 | + $this |
| 27 | + ->setName('demo:args') |
| 28 | + ->setDescription('Describe args behaviors') |
| 29 | + ->setDefinition( |
| 30 | + new InputDefinition(array( |
| 31 | + new InputOption('foo', 'f'), |
| 32 | + new InputOption('bar', 'br', InputOption::VALUE_REQUIRED), |
| 33 | + new InputOption('baz', 'bz', InputOption::VALUE_OPTIONAL) |
| 34 | + ) |
| 35 | + ) |
| 36 | + ; |
| 37 | + } |
| 38 | + |
| 39 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 40 | + { |
| 41 | + // ... |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | +Let's take a look to the values results for differents inputs: |
| 46 | + |
| 47 | +==================== ========================================= |
| 48 | +Input Values |
| 49 | +==================== ========================================= |
| 50 | +--bar=Hello foo = false, bar = "Hello" |
| 51 | +--bar Hello foo = false, bar = "Hello" |
| 52 | +-br=Hello foo = false, bar = "Hello" |
| 53 | +-br Hello foo = false, bar = "Hello" |
| 54 | +-brHello foo = false, bar = "Hello" |
| 55 | +-fbzWorld -br Hello foo = true, bar = "Hello", baz = "World" |
| 56 | +-bzfWorld -br Hello foo = false, bar = "Hello", baz ="fWorld" |
| 57 | +-bzbrWorld foo = false, bz = "brWorld", baz = null |
| 58 | +==================== ========================================= |
| 59 | + |
| 60 | + |
| 61 | +Now, assume there is also an optional argument in the input definition:: |
| 62 | + |
| 63 | + new InputDefinition(array( |
| 64 | + // ... |
| 65 | + new InputArgument('arg', InputArgument::OPTIONAL), |
| 66 | + )); |
| 67 | + |
| 68 | +========================== ======================================== |
| 69 | +Input Values |
| 70 | +========================== ======================================== |
| 71 | +--bar Hello bar = "Hello", arg = null |
| 72 | +--bar Hello World bar = "Hello", arg = "World" |
| 73 | +--bar Hello --baz World bar = "Hello", baz = "World", arg = null |
| 74 | +--bar Hello --baz -- World bar = "Hello", baz = null, arg = "World" |
| 75 | +-b Hello -bz World bar = "Hello", baz = "World", arg = null |
| 76 | +========================== ======================================== |
| 77 | + |
| 78 | +The fourth example shows the special ``--`` seperator which -as you can read |
| 79 | +in docopt- seperates the options from the arguments. By that, ``World`` is |
| 80 | +no longer interpreted as a value of the ``baz`` option (which has an optional value), |
| 81 | +but as the value for the argument. |
| 82 | + |
| 83 | +.. _docopt: http://docopt.org/ |
| 84 | + |
0 commit comments