<译文内容>
Command Line Options¶
This document describes command-line options.
In the Windows command line, you can view the dir command as follows:
There are so many options. In the following, we will explain how to define command-line options in ICmd.
Command-line options in ICore are a type of command-line parameter, starting with - or --, followed by an option name, and optionally a parameter value.
They provide guidance for command execution, making it more flexible and controllable.
Example¶
Here, we define a dir command as an example to illustrate how to define command-line options. Note that I will not implement specific functionality here; this is only to explain the tool for defining command-line options.
We directly list their output
Here, note the last request where we input an undefined option. ICmd will display an error message. This means that users can only use options that have been defined.
$CmdOption¶
The $CmdOption macro is used to define command-line options. This macro annotation has only one parameter, which is the option name. Note that the option name here is the full name, and an abbreviation can be defined separately.
The reason for not defining the option data type is simple: an option has only two states, existence or non-existence, no Schrödinger's cat issue. Therefore, we do not need to define a data type, and the data type is directly a boolean. This parameter defaults to non-existence, with a value of false. If the user uses this parameter in the command line, its value will change to true.
Options can be followed by option parameters, which will be explained in the next document.
$CmdOptionShortName¶
An option's name can be long to fully describe its meaning, so users don't need to guess or rely on the Memo. However, a long option name can make user input difficult. Therefore, we use $CmdOptionShortName to define a short name for the option.
The $CmdOptionShortName macro has two parameters: the first is the full option name, and the second is the short name for the option.
For example, let's modify the previous example:
In the above example, we defined abbreviations r and t for the recurse and time options, respectively.
Now, users can input the options using the short names.
The help information also changes at this point.
Comparing the two, the ShortName field is now filled.
$CmdOptionMemo¶
The $CmdOptionMemo macro is used to define the description of an option. This macro annotation has two parameters: the first is the option name, and the second is the option's description. The option's description must be enclosed in double quotes.
$CmdOptionRequired¶
The $CmdOptionRequired macro has one parameter, which is the option name.
It is used to define an option as a required parameter. If the user does not input this option, the command line will display an error.
This option is usually followed by corresponding parameters. If an option is required and no parameter is provided, the value is fixed, so why define the parameter? For example, if we are doing authentication work, options like --name and --password must be required to ensure the user provides a username and password.
For option parameters, refer to the next document.
$CmdOptionNoValue¶
This annotation has one parameter, which is the option name.
The purpose of this annotation is to inform ICmd that this option does not take any parameters. If parameters are provided, the program should display an error message to the user.
For example, in the mydir command, we define an option --recurse that should not take any parameters. If the user inputs a parameter after --recurse, the program should display an error message.