Symfony console arguments & options
11 September 2025 (Updated 21 September 2025)
Arguments
What is an argument?
Arguments are the space-separated strings that come after the command name itself.
Define arguments
// ...
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
class GreetCommand extends Command
{
// ...
protected function configure(): void
{
$this
// ...
->addArgument('name', InputArgument::REQUIRED, 'Who do you want to greet?')
->addArgument('last_name', InputArgument::OPTIONAL, 'Your last name?')
;
}
}
Use argument in code
// ...
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class GreetCommand extends Command
{
// ...
protected function execute(InputInterface $input, OutputInterface $output): int
{
$text = 'Hi '.$input->getArgument('name');
$lastName = $input->getArgument('last_name');
if ($lastName) {
$text .= ' '.$lastName;
}
$output->writeln($text.'!');
return Command::SUCCESS;
}
}
Argument variants
An argument can be required, optional or an array:
InputArgument::REQUIRED
: Argument must be providedInputArgument::OPTIONAL
: Argument can be omitted (default behaviour)InputArgument::IS_ARRAY
: Argument can contain any number of values and will be an array of those values. Must be used at the end of the argument list.
You can combine the variants like so:
$this
// ...
->addArgument(
'names',
InputArgument::IS_ARRAY | InputArgument::REQUIRED,
'Who do you want to greet (separate multiple names with a space)?'
)
;
Options
What is an argument option?
Argument options aren’t ordered and start with two dashes (e.g., --dry-run
). Options are always optional and can be configured to accept a value (e.g., --name=john
) or serve as a boolean flag (--force
).
Define an option
// ...
use Symfony\Component\Console\Input\InputOption;
$this
// ...
->addOption(
// this is the name that users must type to pass this option (e.g. --iterations=5)
'iterations',
// this is the optional shortcut of the option name, which usually is just a letter
// (e.g. `i`, so users pass it as `-i`); use it for commonly used options
// or options with long names
null,
// this is the type of option (e.g. requires a value, can be passed more than once, etc.)
InputOption::VALUE_REQUIRED,
// the option description displayed when showing the command help
'How many times should the message be printed?',
// the default value of the option (for those which allow to pass values)
1
)
;
Use the option in code
for ($i = 0; $i < $input->getOption('iterations'); $i++) {
$output->writeln($text);
}
Use the option when using the command
php bin/console app:greet Fabien --iterations=5
Option variants
InputOption::VALUE_IS_ARRAY
: This option accepts multiple values (e.g.--dir=/foo --dir=/bar
)InputOption::VALUE_NONE
:Do not accept input for this option (e.g.--yell
). The value returned from is a boolean (false
if the option is not provided). This is the default behavior of optionsInputOption::VALUE_REQUIRED
: This value is required (e.g.--iterations=5
or-i5
), the option itself is still optionalInputOption::VALUE_OPTIONAL
This option may or may not have a value (e.g.--yell
or--yell=loud
)InputOption::VALUE_NEGATABLE
Accept either the flag (e.g.--yell
) or its negation (e.g.--no-yell
)
Links
Tagged:
Symfony