argp
Command-line option parser
Inspired by the extremly well-known argp C library, this module parses GNU-style command-line options. Help, usage and version messages are automatically generated and line-wrapped at 80 columns. The module checks for errors, can be easily adapted to your needs thanks to its evented system and it also works when Node.js is in debug mode. The module is uncached and each property is deleted once all the input parameters have been parsed, so there's no memory footprint.
This module it's made for you if you want:
- Robust solution that reads GNU-style options.
- Command configuration.
- Basic error checking.
- Nice help messages without caring about indentation, multilines, etc.
- Zero memory footprint.
A common configuration looks like this:
//If you're not going to use multiple parser instances or you don't need to//reuse a parser, don't cache the module, this will guarantee a zero memory//footprintvar argv = description "Sample app." body //The object and argument definitions and the text of the --help message //are configured at the same time text " Arguments:" text "\n Options:" version "v1.2.3" ; console; /*$ node script.js { opt: false, help: false, version: false, arg: false} $ node script.js --help Usage: script [options] [arguments] Sample app. Arguments: arg Sample argument Options: -o, --opt Sample option -h, --help Display this help message and exit -v, --version Output version information and exit Report bugs to <a@b.c>.*/
If you have a package.json
file you can take from it the description, email and version using the readPackage()
function. Take into account that this function calls a synchronous fs
operation. Doesn't really matter because this module is one of the first things you're going to execute in your program.
var argv = //If no path is provided, it tries to read the "./package.json" file body text " Arguments:" text "\n Options:" ;
Documentation
- What's new in v1?
- Quick examples with no configuration
- Configuring options
- Configuring arguments
- Configuring commands
- Real examples
Functions
Objects
Two things will break your code:
-
Parser instances are introduced. To create one you need to call to createParser(). In most cases you only need to append
.createParser ({ once: true })
, for example:var argv =...;The
once
option will uncache the module when argv() is called. -
The second argument from the end event is removed.
Before:
After:
Quick examples with no configuration
If an option has not been defined the type of the value is converted automatically from a string to a number, boolean, null or undefined.
By default the parser doesn't allow undefined arguments and options because you typically want to have an absolute control over the calls to your program in order to avoid unexpected behaviours. Allowing undefined arguments and options is as simple as this:
var argv = ;
$ node script.js -a -b -c
Example: options.js.
Considerations:
- By default the options are flags. If the option requires a value, the
metavar
property must be defined. This property is a string and can be seen when the --help and --usage messages are printed.
$ node script.js --help... o, --opt=STR Sample option...
Where STR
is the metavar
property.
- By default, the value of the options is a string. Configure the
type
property if the value is a number, boolean (rarely used, use a flag instead) or array (comma-separated values and multiple assignments). - Each option has an id which is used to store the value in the final object. This id is the long name. If the long name has not been defined then the id is the short name.
//{ aa: false }//{ aa: false }//{ a: false }
- Mandatory options (aka
required
) are not implemented because options are optional. Use a command if you need mandatory parameters.
Common properties between flags and options with a value:
- description - String
The description. - hidden - Boolean
If true, the option is not displayed in the --help and --usage messages. Default is false. - long - String
The long name. Cannot contain white spaces. - short - String
The short name. It must be an alphanumeric character.
Flags:
-
negate - Boolean
If true, the flag is negated. The default value is true and it becomes false when the short name or the negated long name (eg. --no-flag) is present.$ node script.js
Options with a value:
-
aliases - Array
An alias it's a long-name option that points to another option.body$ node script.js --fooThe
options()
function returns an object with all the configured options:name: ...foo: ...bar: ...help: ...usage: ...Where
name
,foo
andbar
point to the same object: -
choices - Array
The input value must be one of these choices. If the option isoptional
, thechoices
property is ignored.$ node script.js --opt=1When
default
andchoices
are defined in the same option the default value doesn't need to match any choice:$ node script.js -
default - Object
The default value.$ node script.js -
metavar - String
Must be defined if the option requires a value. Ifmetavar
is not defined, the option is a flag. The string is used when the --help and --usage messages are printed.$ node script.js --help...--name=STR... -
optional - Boolean
If true, the value is optional. Default is false. If the option doesn't receive any value the default value is set and it depends on thedefault
andtype
properties.Types and default values:
- String:
null
- Number:
0
- Array:
[]
- Boolean:
false
//Boolean type is rarely used, use a flag instead$ node script.js --name1 --name2 --name3 --name4 --name5$ node script.js --name1 foo --name2 bar --name3 12 --name4 -12.34,foo,true --name4 false --name5 true - String:
-
reviver - Function
The function is executed when the option is parsed. It is similar to the reviver parameter of theJSON.parse()
function. This is the right place where you can validate the input data andfail()
if it's not valid. For example, if the option requires a number you can validate the range here.$ node script.js --name foo -
type - String | Number | Boolean | Array
The type of the value. Default is String.If the type is an Array, comma-separated values are automatically stored in an array and each element is converted to the type it represents. Multiple assignments are also supported.
$ node script.js --name 1,true,foo
Example: arguments.js.
An argument is an individual name like login
, reset
, build
, etc. They are basically flags.
Properties:
- description - String
The description. - hidden - Boolean
If true, the option is not displayed in the --help and --usage messages. Default is false.
Note: synopsis
and trailing
properties can be also configured but they have meaning only with commands.
$ node script.js arg1
Note that an argument is just a flag but it can be present without any prefixed hyphen. It is a flag with more weight, it is used to denote important actions.
As you can see, the arguments are also stored in a hash like regular options, undefined arguments inclusive. For example:
var argv = ; console;
$ node script.js a b c
This feature is different from other cli parsers that store the arguments in an array. If you need to read undefined arguments and save them in an array you can use the events. For more details: to-upper-case.js
A command is an argument followed by other arguments and options. NPM is an example:
npm config set <key> [<value>]
npm install [<package>...] -g
config
is a command and set
an argument with 2 trailing arguments: minimum 1, maximum 2.
install
is a command with infinite trailing arguments: minimum 0, maximum Infinity. -g
is an option which only applies to the install
command.
If you have a very few commands you can configure them in the same file (commands.js example), but you typically want to modularize them, one command per file. Then you should check the npm.js example.
The commands are configured exactly the same way as the Argp
instance with only one difference: argument()
accepts 2 new properties:
-
synopsis - String
The string replaces the argument name in the --help and --usage messages.;/*...set Sample argument...*/;/*...set <key> [<value>] Sample argument...*/ -
trailing - Object
Configures how many trailing arguments must follow this argument.There are 3 properties:
eq
,min
andmax
.eq
cannot be used withmin
ormax
. Ifmin
andmax
are used, by defaultmin
is 0 andmax
is Infinity. Atrailing
object without any of these 3 properties defaults tomin
0 andmax
Infinity.Some examples:
-
2 trailing arguments required:
cmd arg <x> <x>
. -
1 required, 1 optional:
cmd arg <x> [<x>]
. -
1 optional:
cmd arg [<x>]
. -
1 required, infinite optional:
cmd arg <x> [<x>...]
. -
Infinite:
cmd arg [<x>...]
.//Same as trailing: { min: 0, max: Infinity } -
No trailing arguments:
cmd arg
. -
Multiple arguments with trailing in the same line. Argument
arg1
with 1 required, and argumentarg2
with infinite trailing arguments:cmd arg1 <x> arg2 [<y>...]
. Note that writingcmd arg1 <x> arg2 [<y>...]
is not the same ascmd arg2 [<y>...] arg1 <x>
. In the latter case,arg1 <x>
will be eaten by the trailing arguments ofarg2
.
-
- brainfuck: Interpreter for the Brainfuck esoteric language.
- ntftp: Streaming TFTP client and server.
module.createParser([options]) : Argp
Returns a new Argp instance.
Options:
- once - Boolean
Set it to true if you want to uncache the whole module when argv() finishes. This will guarantee a zero memory footprint. Default is false.
The module returns an instance of Argp
. It inherits from an EventEmitter.
The parser follows the GNU-style rules: -a
, -abc
, --a
, --no-a
, --a=b
, --
, etc. Long-option abbreviation is also supported.
If you don't want to configure anything simply require the module, allow undefined arguments and options and call to argv()
.
var argv = ;
Note: If no configuration is provided you cannot join a short name with its value in the same token, eg: -Dvalue
, all the characters following a hyhen are interpreted as individual flags.
Events
With the event system you can fully adapt this module to yours needs. Examples: to-upper-case.js, mkdir.js.
Methods
- Argp#allowUndefinedArguments() : Argp
- Argp#allowUndefinedOptions() : Argp
- Argp#arguments() : Object
- Argp#argv([input]) : Object
- Argp#body() : Body
- Argp#columns(columns) : Argp
- Argp#command(name[, configuration]) : Command
- Argp#commands() : Argp
- Argp#description(str) : Argp
- Argp#email(str) : Argp
- Argp#exitStatus(code) : Argp
- Argp#fail(str[, code]) : undefined
- Argp#main() : Argp
- Argp#options([filter]) : Object
- Argp#printHelp([code]) : undefined
- Argp#printUsage([code]) : undefined
- Argp#printVersion([code]) : undefined
- Argp#readPackage([path]) : Argp
- Argp#sort() : Argp
- Argp#usages(usages) : Argp
Objects
Emitted when an argument is found.
Parameters:
- argv - Object
The final object. - argument - String
The name of the argument. - ignore - Function
When the function is called the parser ignores the argument, hence it isn't stored in the final object.
Emitted when all the options and arguments have been parsed.
Parameters:
- argv - Object
The final object.
Emitted when an error occurs. If you don't listen for error
events, the message will be printed to stderr and the process will exit. If you attach an error handler and an error occurs, argv() returns null.
You typically want to listen for error
events when you need to do something with the error and then quit the process, or simply because you want to continue executing the process (maybe because you are executing a shell prompt) and display the error in the console.
For example, the ntftp module uses two parsers. One is the main parser. If something is not correct, it prints the error to stderr and finishes. The second parser is used when the program is executing a shell prompt. It is being reused and the errors are simply printed to console.
Emitted when an option is found.
Parameters:
- argv - Object
The final object. - option - String
The name of the option. - value - String
The value of the option after calling the reviver, if any. - long - Boolean
True if the option is a long name, otherwise false. - ignore - Function
When the function is called the parser ignores the argument, hence it isn't stored in the final object.
Emitted just before the parser begins to read the cli parameters.
Parameters:
- argv - Object
The final object. The default values are already set.
Argp#allowUndefinedArguments() : Argp
Allows undefined arguments.
Argp#allowUndefinedOptions() : Argp
Allows undefined options.
Returns the configured arguments. Look at the internal-data.js example for further details.
Argp#argv([input]) : Object | null
Parses the process.argv
array or the given input array.
If you don't need to reuse the parser and want a zero memory footprint, you shouldn't cache the module and the parser instance. Remember to also set once
to true.
var argv = ... ;
If you need to reuse a parser, you probably want to listen for error events. If an error occurs, argv() returns null.
var argp = ; //Configurationvar parser = argp ...; var argv;argv = parser;argv = parser;
If you pass an input array, then the functions printHelp(), printUsage() and printVersion() will print the message but won't terminate the process.
Returns a Body
instance.
Sets the maximum line length. By default lines are wrapped at 80 columns.
Argp#command(name[, configuration]) : Command
Configures a command. A command it's like a new fresh cli program. It behaves exactly like an Argp
. See Configuring commands.
Returns the configured commands.
Look at the internal-data.js example for further details.
Sets a description. The description is printed at the start of the --help message, after the "Usage" lines.
Sets a contact email. The email is printed at the end of the --help message.
Sets the exit code that will be used is some methods. Default is 1.
Argp#fail(str[, code]) : undefined
Prints a message to the stderr and exits with the given code number or if not given, uses the code configured with exitStatus() or if not configured, exits with code 1.
Returns de main Argp
instance. It's a no-op function, just for a better visual organization when configuring commands.
Look at the npm.js example for further details.
Argp#options([filter]) : Object
Returns the configured options. filter
is an object which can be used to return the options that have a short name or a long name.
options options short: true options long: true
Look at the internal-data.js example for further details.
Argp#printHelp([code]) : undefined
Prints the help message and exits with the given code number or if not given, uses the code configured with exitStatus() or if not configured, exits with code 0.
If argv() is called with an input array, the process doesn't exit.
Argp#printUsage([code]) : undefined
Prints the usage message and exits with the given code number or if not given, uses the code configured with exitStatus() or if not configured, exits with code 0.
If argv() is called with an input array, the process doesn't exit.
Argp#printVersion([code]) : undefined
Prints the version message (if it was configured) and exits with the given code number or if not given, uses the code configured with exitStatus() or if not configured, exits with code 0.
If argv() is called with an input array, the process doesn't exit.
Argp#readPackage([path][, options]) : Argp
Reads a package.json
file and configures the parser with the description, email and version. If no path is provided it tries to read the ./package.json
path. The description, email and version labels can be ignored individually with the options
parameter. For example, if you only want to ignore the email and read the description and version:
Options are: description
, version
, email
. By default they are true. Set them to false to ignore them.
Changes the "Usage" line from the --help and --usage messages. usages
is an array of strings.
Look at the custom-usages.js example for further details.
If sort()
is enabled, the options are parsed before the arguments, if not, the options and arguments are parsed in the same order they come.
The Body
instance is returned by calling Argp#body(). All the following functions (except argv()
, command()
and main()
) print a message in the same order they are configured, this allows you to fully customize the --help message very easily.
Look at fully-descriptive-help.js for further details.
Methods
- Body#argument(name[, configuration]) : Body
- Body#argv([input]) : Object
- Body#columns(column1, column2) : Body
- Body#command(name[, configuration]) : Command
- Body#help([options]) : Body
- Body#main() : Argp
- Body#option(o) : Body
- Body#text(str[, prefix]) : Body
- Body#usage() : Body
- Body#version(str[, options]) : Body
Body#argument(name[, configuration]) : Body
Defines an argument. See Configuring arguments.
Same as Argp#argv().
Body#columns(column1, column2) : Body
Prints a line with 2 columns. The first column shouldn't contain line breaks (\n
). This functionality is used to print the options and arguments.
Body#command(name[, configuration]) : Command
Same as Argp#command().
Enables the -h, --help
option. The short option can be disabled using the options
parameter.
$ node script.js --help... --help Display this help message and exit...
Same as Argp#main().
Defines an option. See Configuring options.
Body#text(str[, prefix]) : Body
Prints a text message. By default it's line-wrapped at 80 columns and supports multilines (line breaks, \n
). The prefix
is a string that is printed before each line. It's mainly used to indent the text with some spaces.
Enables the --usage
option.
Body#version(str[, options]) : Body
Enables the -v, --version
option. str
is the text to print when the option is called. The short option can be disabled using the options
parameter.
version "v1.2.3" short: false
$ node script.js --help... --version Output version information and exit...