Args arguments are passed after the command name and its options. In the
example below, "localhost" is the argument to the "server -d" command.
$ console server -d localhost
Arguments can be either optional or required. By default, all arguments are
optional, but you can explicitly make an argument optional or required by
passing one of the flags {@link OPTIONAL} and {@link REQUIRED} to the
constructor:
php
$argument = new Argument('server', Argument::REQUIRED);
Arguments can also be multi-valued. Multi-valued arguments can be passed any
number of times:
$ console server -d localhost google.com
To create a multi-valued argument, pass the flag {@link MULTI_VALUED} to the
constructor:
php
$argument = new Argument('server', Argument::MULTI_VALUED);
You can combine the {@link MULTI_VALUED} flag with either {@link OPTIONAL}
or {@link REQUIRED} using the bitwise operator "|":
php
$argument = new Argument('server', Argument::REQUIRED | Argument::MULTI_VALUED);
/** * Renders an argument. * * @param BlockLayout $layout The layout. * @param Argument $argument The argument to render. */ protected function renderArgument(BlockLayout $layout, Argument $argument) { $description = $argument->getDescription(); $name = '<c1><' . $argument->getName() . '></c1>'; $defaultValue = $argument->getDefaultValue(); if (null !== $defaultValue && (!is_array($defaultValue) || count($defaultValue))) { $description .= sprintf(' <b>(default: %s)</b>', $this->formatValue($defaultValue)); } $layout->add(new LabeledParagraph($name, $description)); }
/** * Adds an argument at the end of the argument list. * * The existing arguments stored in the builder are preserved. * * You cannot add arguments after adding a multi-valued argument. If you do * so, this method throws an exception. * * Adding required arguments after optional arguments is not supported. * Also in this case an exception is thrown. * * @param Argument $argument The argument to add. * * @return static The current instance. * * @throws CannotAddArgumentException If the argument cannot be added. */ public function addArgument(Argument $argument) { $name = $argument->getName(); if ($this->hasArgument($name)) { throw CannotAddArgumentException::existsAlready($name); } if ($this->hasMultiValuedArgument()) { throw CannotAddArgumentException::cannotAddAfterMultiValued(); } if ($argument->isRequired() && $this->hasOptionalArgument()) { throw CannotAddArgumentException::cannotAddRequiredAfterOptional(); } if ($argument->isMultiValued()) { $this->hasMultiValuedArg = true; } if ($argument->isOptional()) { $this->hasOptionalArg = true; } $this->arguments[$name] = $argument; return $this; }
/** * Creates an input argument for the given argument. * * @param Argument $argument The argument. * * @return InputArgument The created input argument. */ private function adaptArgument(Argument $argument) { $mode = null; if ($argument->isMultiValued()) { $mode |= InputArgument::IS_ARRAY; } if ($argument->isOptional()) { $mode |= InputArgument::OPTIONAL; } if ($argument->isRequired()) { $mode |= InputArgument::REQUIRED; } return new InputArgument($argument->getName(), $mode, $argument->getDescription(), $argument->getDefaultValue()); }
/** * @dataProvider getInvalidParseValueTests * @expectedException \Webmozart\Console\Api\Args\Format\InvalidValueException */ public function testParseValueFailsIfInvalid($flags, $input) { $argument = new Argument('argument', $flags); $argument->parseValue($input); }