Args
Module | ejs |
Definition | class Args |
Inheritance | Args Object |
Specified | ejscript-2.5 |
Stability | Prototype. |
The Args class parses the Application's command line.
A template of permissible command line options
is passed to the Args constructor. The Args class supports command options that begin with "-" or "--" and parses
option arguments of the forms: "-flag x" and "-flag=x". An option may have aliased forms (i.e. -v for --verbose).
The command template contains properties for options, usage message, and error handling policy should
invalid options or arguments be submitted. The template can provide a range of valid values for a command option.
The option range may be an Ejscript type, regular expression or set of values that constrains the permissible
option argument values. A default value may be provided for each option argument.
Once parsed, the arguments are accessible via args.options. The remaining command line arguments are available
in args.rest.
Properties
Qualifiers | Property | Type | Description |
| options | Object | User supplied command line options. |
| program | Path | null |
| rest | Array | null |
| template | Object | null |
Inherited Properties
Args Class Methods
Qualifiers | Method |
(No own class methods defined)
Inherited Methods
Args Instance Methods
Qualifiers | Method |
| Args(template: Object, argv: Array = expression) |
| The Args constructor creates a new instance of the Args. |
Inherited Methods
Method Detail
- Description
- The Args constructor creates a new instance of the Args. It parses the command line
and stores the parsed options in the options and args properties.
Args supports command options that begin with "-" or "--" and parses option arguments of the forms:
"-flag x" and "-flag=x".
- Parameters
template: Object | Command argument template. The template is an object with option properties for: options, usage, onerror ad silent. |
argv: Array | Array of command arguments to parse. Defaults to App.args. [default: expression] |
- Options
options | This is an object with properties for each command line option. The value of each property is an object with properties: 'alias', 'range', 'value' and 'separator'. The 'alias' property defines a String alias for the option. This is typically used to define a single character alias for the full command line option name. The 'range' property defines what permissible values an option parameter may take. It may be set to either an Ejscript type, a Regular Expression or it may be set to an array of values. The 'value' property may define a default value for the option if it is not defined. This is the value that args.options[OPTION] will be set to when the command line option is absent. If an option without an argument is specified by the user, its value in options[NAME] will be set to true. If the 'separator' property is defined, multiple command line options of this name are permitted. If 'separator' is set to a string, then the multiple command line option values are catenated using the 'separator'. If set to array, the option values are stored in an array. |
usage | Function to invoke for argument parse errors. |
onerror | If set to 'throw', an exception will be thrown for argument parse errors. The usage function will not be called and no message will be written to the application log. If set to 'exit', a message will be written to the console for argument parse errors and any usage function will be invoked before exiting the application with a non-zero exit status. |
silent | Don't emit any message on argument parse errors. |
unknown | Callback to invoke for unknown arguments. |
- Example
let args = Args({
options: {
depth: { range: Number },
quiet: { },
verbose: { alias: 'v', value: true, },
log: { range: /\w+(:\d)/, value: 'stderr:4' },
mode: { range: ['low', 'medium', 'high'], value: 'high' },
with: { range: String, separator: ',' },
},
usage: usage,
onerror: 'exit|throw',
}, [])
let options = args.options
if (options.verbose) { }
for each (file in args.rest) {
...
}