Function
func parseArguments(Array<String>, Array<ArgumentSpec>)
public func parseArguments(args: Array<String>, specs: Array<ArgumentSpec>): ParsedArguments
Description: Parses the command line parameters args based on the provided parameter specifications specs and returns a structured object that contains option and non-option parameters after parsing.
This function matches each parameter in args with the options defined in specs. For the options that are successfully matched, the option names and corresponding values are added to options. The unmatched parameters are processed as non-option parameters and added to nonOptions. In addition, when -- is parsed, the option scanning is terminated in advance, and all parameters following -- are regarded as non-options.
This function supports the parsing and processing of short options, long options, short-prefix long options, short option combinations, non-options, and invalid options.
ArgumentMode held by each ArgumentSpec in specs determines the processing mode of the parameters.
-
For long options, only the following formats are supported for processing depending on ArgumentMode:
- RequiredValue:
--option=valueor--option value - OptionalValue:
--option=valueor--option - NoValue:
--option
- RequiredValue:
-
For short options, only the following formats are supported for processing depending on ArgumentMode:
- RequiredValue:
-ovor-o v - OptionalValue:
-ovor-o - NoValue:
-o
- RequiredValue:
For the scenario in which short options are combined:
- When the first option that is not NoValue is parsed:
- If the option is OptionalValue, the content (if it exists) following the option is used as the value of the option for parsing.
- If the option is RequiredValue, the content following the option is used as the value of the option for parsing.
- If a group of short options can be combined into the literal value of a long option, the group of short options is considered as a long option rather than a combination of short options. For example, if -abc has defined the long option
abcand three short optionsa,b, andc, it is considered as a long option.
If ArgumentSpec provides a lambda callback function, the callback function is called after the parsing is successful to process the parsed parameter value.
If the passed args contains multiple values assigned to an option, the last value is used as the option value.
Parameters:
-
specs: Array<ArgumentSpec>: parameter specifications
Returns:
- ParsedArguments: parameter parsing result
Throws:
-
ArgumentParseException: If the parameters fail to be parsed or an
invalid optionis obtained after parsing, this exception is thrown. -
IllegalArgumentException: If ArgumentSpec instances share the same
nameis defined, this exception is thrown.