CLI command-line generator for development & debugging tools. This module is used to execute command lines containing command line arguments. Command line strings are quoted similarly to shell quotes in Unix and Windows command lines. Unlike Unix, single quoting is supported. The command line string supports these environment variables:
%PATH%- calling module in the Lua stack.%OS%- Lua version (5.1 for the foreseeable future). This module is supplied arguments in the Scribunto console. The general command synopsis (in man command format) is:
p "<command> [<arg>] [--flag [ <value>]]"
The help command will output a command synopsis in the man command format. This module supports the following syntax:
- Positional arguments (space-delimited):
one two three - Boolean command flags:
-one,--one - Variable command flags:
--one "numero uno" - Quoted arguments:
one "numero dos" - Variable assignments:
one="one" - Escapes: quotes as
\\"or\\'and spaces as\\The first positional argument is recognised as a command word and is executed within the command object. The command sends its output to the Scribunto console usingmw.log. Note that command flags must be placed after positional arguments to work. The command API prints the CLI usage if thehelpcommand word or--help/-helpflag is supplied. When a command is combined with--helpor-help, or a command word is defined as a second positional argument, the help statement is tailored to the specific command, its usage and options.
Documentation
Package items
CommandLine:new(config)(function)- Command line function generator.
- Parameters:
configCommand line configuration. (table)config.descriptionInterfaced module description. (string)config.commandsObject containing command methods. (table)config.wordsConfiguration object with command methods as keys. Each table contains the following configuration table fields:descriptionDescription of command. (string)optionsDependent options for command. (table)
- (table)
config.optionsCommand line options configuration. This option is defined as an array with option configuration tables as elements. Each table contains the following configuration table fields:nameName of option. Accepts number when the option is a positional argument. (string or number)aliasAlias for option. (string; optional)descriptionDescription of option. (string)typeType of option. Omitted for boolean flags. (optional)requiredWhether the argument or flag is not optional. Default:false. (boolean; optional)
- (table; optional)
- Returns: Command line handler. Help commands will produce a command synopsis detailing usage, a list of commands and a list of options. Other commands will be executed if a corresponding method is present in
config.commands. (function) CommandLine:parse(command, config)(function)- Command line shell quote parser.
- Parameters:
commandCommand line to be parsed. (string)configCommand line configuration. (table; optional)
- Errors:
- Returns:
- Map of processed arguments. The array, or sequential keys in the map list positional arguments in the command line. The flags in the command line are converted into named arguments. Metadata fields:
0The CLI module name or context."PATH"The CLI module name or context.-1The Lua interpreter Scribunto uses.-2The Lua version Scribunto runs."OS"The Lua version Scribunto runs.
- (table)
- Array of processed arguments. Includes the metadata fields listed above. (table)
- Number of arguments passed by the user, counted like the C or C++
argcconvention. (table)
- Map of processed arguments. The array, or sequential keys in the map list positional arguments in the command line. The flags in the command line are converted into named arguments. Metadata fields: