What is arg?
The arg npm package is a simple argument parsing library. It allows developers to parse command-line arguments in Node.js applications into a structured format that is easy to work with. It supports various types of flags and options, including boolean flags, string options, and number options.
What are arg's main functionalities?
Parsing command-line flags and options
This feature allows the parsing of command-line arguments with various types of expected values. The example code demonstrates how to define the expected types for each flag and option, including boolean, count, number, and string types, as well as how to set up aliases for shorthand notation.
{"const arg = require('arg');\n\nconst args = arg({\n // Types\n '--help': Boolean,\n '--version': Boolean,\n '--verbose': arg.COUNT, // A count of how many times the flag was set\n '--port': Number, // A port number\n '--name': String, // A string name\n // Aliases\n '-h': '--help',\n '-v': '--version',\n '-n': '--name'\n});\n\nconsole.log(args); // Output the parsed arguments"}
Handling default values
This feature allows developers to specify default values for command-line options that are not provided by the user. The example code shows how to set default values for a port and host.
{"const arg = require('arg');\n\nconst args = arg({\n '--port': Number,\n '--host': String\n}, {\n // Default values\n '--port': 8080,\n '--host': 'localhost'\n});\n\nconsole.log(args); // Output the parsed arguments with defaults"}
Permissive parsing
This feature enables permissive parsing, which means that the parser will ignore any flags that are not explicitly defined in the schema. The example code shows how to enable permissive parsing.
{"const arg = require('arg');\n\nconst args = arg({\n '--port': Number,\n '--host': String\n}, {\n permissive: true\n});\n\nconsole.log(args); // Output the parsed arguments, ignoring any non-specified flags"}
Other packages similar to arg
yargs
Yargs is a more feature-rich command-line argument parsing library. It provides a fluent API, built-in help, command handling, and more. It is often preferred for complex CLI applications that require detailed configurations and command structures.
commander
Commander is another popular npm package for command-line interfaces. It includes support for subcommands, custom help, auto-versioning, and is used by many large projects. It is more object-oriented compared to arg and is suitable for applications with a variety of commands and options.
minimist
Minimist is a minimalistic argument parsing library. It is lightweight and straightforward, with fewer features than arg. It is a good choice for simple command-line applications or for those who prefer a minimalistic approach.
meow
Meow is a CLI app helper built on top of minimist. It provides a higher-level API with features like help text generation and input normalization. It is designed to create simple CLI tools quickly and with less boilerplate.
Arg
arg
is yet another command line option parser.
Installation
Use Yarn or NPM to install.
$ yarn add arg
or
$ npm install arg
Usage
arg()
takes 1 or 2 arguments:
- An array of CLI arguments (Optional, defaults to
process.argv.slice(2)
) - Options argument (see below)
It returns an object with any values present on the command-line (missing options are thus
missing from the resulting object). Arg performs no validation/requirement checking - we
leave that up to the application.
All parameters that aren't consumed by options (commonly referred to as "extra" parameters)
are added to result._
, which is always an array (even if no extra parameters are passed,
in which case an empty array is returned).
const arg = require('arg');
const args = arg([argument_array,] options);
For example:
$ node ./hello.js --port=1234 -n 'My name' foo bar --tag qux --tag=qix -- --foobar
const arg = require('arg');
const args = arg({
'--help': Boolean,
'--version': Boolean,
'--port': Number,
'--name': String,
'--tag': [String],
'-v': '--version',
'-n': '--name',
'--label': '--name'
});
console.log(args);
The values for each key=>value pair is either a type (function or [function]) or a string (indicating an alias).
-
In the case of a function, the string value of the argument's value is passed to it,
and the return value is used as the ultimate value.
-
In the case of an array, the only element must be a type function. Array types indicate
that the argument may be passed multiple times, and as such the resulting value in the returned
object is an array with all of the values that were passed using the specified flag.
-
In the case of a string, an alias is established. If a flag is passed that matches the key,
then the value is substituted in its place.
Type functions are passed three arguments:
- The parameter value (always a string)
- The parameter name (e.g.
--label
) - The previous value for the destination (useful for reduce-like operatons or for supporting
-v
multiple times, etc.)
This means the built-in String
, Number
, and Boolean
type constructors "just work" as type functions.
License
Copyright © 2017 by ZEIT, Inc. Released under the MIT License.