Definition command

A command consists of a command group and an execution command. A class is a command group. The method in the class corresponds to the operation command. The operation of a command is performed by executing the operation command corresponding to the command group.

All functions of Swoft can be used in the command logic.

Command structure

                                         value of option: opt1
                                option: opt1  |
                                       |      |
php bin/swoft group:cmd john male 43 --opt1 value1 -y
        |         |      |    |   |                 |
     script    command   |    |   |_____        option: yes, it use shortcat: y, and it is a bool, so no value.
                         |    |___     |
                 argument: name  |   argument: age
                            argument: sex 

Parameters and options

  • None - beginning is considered a parameter (eg: status=2 arg0 )
  • Conversely, the beginning of - is the option data.
    • -- at the beginning is the long option (long-option)
    • One - at the beginning is a short option (short-option)

Support for assignment of mixed options --id=154 and --id 154 are equivalent

Note: The string entered below will be considered a boolean

  • on|yes|true -- true
  • off|no|false -- false

annotation

The definition of the command is mainly through the two annotations @Command and @CommandMapping . @Command defines the command group name, and @CommandMapping defines the mapping relationship of the operation commands.

The command's commands use help information and are also defined by annotations.

Command

@Command defines the command group, marking a class as the console command class. Scope: class

Have attributes:

  • name string defines the command group name. If it is default, it is automatically parsed according to the class name.
  • alias string The command group alias, which can still be accessed via an alias. Allow multiple, separated by commas
  • desc string command group description information description, support color label
  • coroutine bool defines whether to run under coroutine , the default is true, the framework will start a coroutine to run the commands in this group

Tips: If desc is empty, the first line of the class will be automatically parsed as a command group description.

CommandMapping

@CommandMapping defines the operation command mapping relationship, indicating a specific command. Scope: method

Have attributes:

  • name string defines the name command group. If it is the default, the method name is used.
  • alias string command alias, which can still be accessed via an alias. Allow multiple, separated by commas
  • Descriptive information description of the desc string command, support color label

Tips: If desc is empty, the first line of the class will be automatically parsed as a description.

CommandOption

@CommandOption defines an option for a command. Scope: method|class

Have attributes:

  • name string Required Defines the name of the command option. Eg: opt
  • short string defines a short option for the command option name.
  • Default value of the default mixed command option
  • Description of the desc string command option description, support color label
  • type string command type value type
  • mode int command option value input limit: mandatory, optional, etc.

Tips: The special @CommandOption can be commented with the command class, which is equivalent to adding a common option to all commands inside.

CommandArgument

@CommandArgument defines the parameters of a command. Scope: method

Have attributes:

  • name string Required Defines the name of the command parameter. Eg: opt
  • Default value of the default mixed command parameter
  • Description of the desc string command parameter description, support color label
  • type string value type of the command parameter
  • mode int command parameter value input limit: mandatory, optional, etc.

Tips: Command parameters are obtained according to the input position (in order), and name is the name given to the parameters of this position in the code.

Example comment

@example annotation will be treated specially ( not annotated ). If you have this comment on your command method, its contents will be displayed on the command help information.

Code example

Below is the serve:run command definition in sowftcli . The command help shows the effect as follows:

Hot-restart-help

 /**
 * Provide some commands for manage and watch swoft server project
 *
 * @Command()
 */
class ServeCommand
{
    /**
     * Start the swoft server and monitor the file changes to restart the server
     *
     * @CommandMapping()
     * @CommandArgument("targetPath", type="path",
     *     desc="Your swoft project path, default is current work directory"
     * )
     * @CommandOption("interval", type="integer", default=3,
     *     desc="Interval time for watch files, unit is seconds"
     * )
     * @CommandOption(
     *     "bin-file", short="b", type="string", default="bin/swoft",
     *     desc="Entry file for the swoft project"
     * )
     * @CommandOption(
     *     "start-cmd", short="c", type="string", default="http:start",
     *     desc="the server startup command to be executed"
     * )
     * @CommandOption(
     *     "watch", short="w", default="app,config", type="directories",
     *     desc="List of directories you want to watch, relative the <cyan>targetPath</cyan>"
     * )
     * @example
     *   {binFile} run -c ws:start -b bin/swoft /path/to/php/swoft
     * @param Input $input
     */
    public function run(Input $input): void
    {
        Show::aList([
            'options'   => $input->getOpts(),
            'arguments' => $input->getArgs(),
        ]);
    }
} 

Use run

  • Now you can execute php bin/swoft and the serve group command will appear in the command list.
  • Execute php bin/swoft serve or php bin/swoft serve -h to see the specific commands in the serve group.
  • Executing php bin/swoft serve:run -h will see the complete help for this command.
/docs/2.x/en/console/definition.html
progress-bar