网页 综合 更新时间 2026-11-13 17:27:19

Console Input (Arguments & Options) (Symfony Docs)

自动蜘蛛 symfony.com 正文约 4711 字
Console Input (Arguments & Options) (Symfony Docs)
The most interesting parts of the commands are the arguments and options that you can make available. These arguments and options allow you to pass dynamic information from the terminal to the command.
Arguments are the strings - separated by spaces - that come after the command name itself. They are ordered, and can be optional or required.
In invokable commands, use the Argument attribute to define arguments directly in the __invoke() method parameters:
The Argument attribute accepts the following parameters:
The argument mode (required, optional, array) is inferred from the parameter type:
You can also use BackedEnum types for arguments and options. The string input is automatically converted to the corresponding enum case using BackedEnum::from(), and autocompletion is provided based on the enum cases.
Given a backed enum like this:
You can type-hint it directly in your command:
If the user provides a value that doesn't match any enum case, an error message is displayed along with the list of valid values.
If you prefer the classic approach, or need to extend the Command class, you can use the addArgument() method in the configure() method. For example, to add an optional last_name argument to the command and make the name argument required:
You now have access to a last_name argument in your command:
The command can now be used in either of the following ways:
It is also possible to let an argument take a list of values (imagine you want to greet all your friends). Only the last argument can be a list:
To use this, specify as many names as you want:
You can access the names argument as an array:
There are three argument variants you can use:
You can combine IS_ARRAY with REQUIRED or OPTIONAL like this:
Combining InputArgument::REQUIRED and InputArgument::OPTIONAL was deprecated in Symfony 8.1.
Unlike arguments, options are not ordered (meaning you can specify them in any order) and are specified with two dashes (e.g. --yell). Options are always optional, and can be set up to accept a value (e.g. --dir=src) or as a boolean flag without a value (e.g. --yell).
In invokable commands, use the Option attribute to define options directly in the __invoke() method parameters:
The Option attribute accepts the following parameters:
The option mode is inferred from the parameter type and default value:
Boolean flag (VALUE_NONE): bool type with default false. Usage: --yell sets the value to true:
Negatable flag (VALUE_NEGATABLE): bool type with default true or nullable ?bool with default null. Usage: --yell or --no-yell:
Value required (VALUE_REQUIRED): string, int or float types:
Array of values (VALUE_IS_ARRAY): array type. Usage: --role=ADMIN --role=USER:
Value optional (VALUE_OPTIONAL): Union types string|bool, int|bool, or float|bool with default false. Usage: --output (returns true) or --output=file.txt (returns 'file.txt'):
The #[Option] attribute enforces the following rules on the parameter type and its default value:
Examples of valid option definitions:
Examples of invalid option definitions:
Support for objects as default values was introduced in Symfony 8.1.
You can use objects as default values for arguments and options in invokable commands. This is useful when you need complex default values that are resolved at runtime:
In invokable commands, options must always have a default value. Object default values are especially useful in this context because they allow you to define complex, runtime-resolved defaults directly in the method signature.
If you prefer the classic approach, or need to extend the Command class, you can use the addOption() method in the configure() method. For example, add a new option to the command that can be used to specify how many times in a row the message should be printed:
Next, use this in the command to print the message multiple times:
Now, when you run the command, you can optionally specify a --iterations flag:
You can also declare a one-letter shortcut that you can call with a single dash, like -i:
Note that to comply with the docopt standard, long options can specify their values after a space or an = sign (e.g. --iterations 5 or --iterations=5), but short options can only use spaces or no separation at all (e.g. -i 5 or -i5).
While it is possible to separate an option from its value with a space, using this form leads to an ambiguity should the option appear before the command name. For example, php bin/console --iterations 5 app:greet Fabien is ambiguous; Symfony would interpret 5 as the command name. To avoid this situation, always place options after the command name, or avoid using a space to separate the option name from its value.
There are five option variants you can use:
访问原文 返回搜索 https://symfony.com/doc/current/console/input.html
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。