arg
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
"arg" is a lightweight, unopinionated, and straightforward command-line argument parser for Node.js applications. It does not enforce any particular way of handling command-line options, instead offering a flexible and efficient method for reading command-line arguments passed to the application.
To use the "arg" package in your project, first, you need to install it using npm:
npm install arg
Then, require the package in your script, and call the "arg" function, passing it an object that defines the command-line options and flags that your script expects:
const arg = require('arg');
const args = arg({
'--help': Boolean, // a Boolean option (--help)
'--version': Boolean, // a Boolean option (--version)
'--verbose': arg.COUNT, // a count option (--verbose), counts the number of times it is passed
'--port': Number, // a number option (--port), expects a number as argument
'--name': String, // a string option (--name), expects a string as argument
'--tag': [String], // an array option (--tag), can be passed multiple times with different strings
'-v': '--verbose', // aliases -v to --verbose
'-n': '--name', // aliases -n to --name
'--label': '--name', // aliases --label to --name
});
This will parse the command-line arguments passed to your script and return an object where the keys are the options and flags and the values are their arguments or counts. All extra parameters not consumed by options are added to result._
.
For comprehensive customization, you can use second optional argument that provides parsing options to modify the behavior of arg()
, like argv
, permissive
, and stopAtPositional
.
The documentation for "arg" is primarily contained in the README.md file found at the package's GitHub repository, which is git+https://github.com/vercel/arg.git. The README provides detailed descriptions of how to use the package, including how to define specifications for command-line options and flags, how to create aliases, handle unknown or multiple arguments, and set parsing options. It also includes comprehensive examples of its usage. The source code itself is also well-commented, providing further insight into its functionality.