CLI arguments parser. Native port of python's argparse.
The npm package "argparse" is an essential tool for developers looking to efficiently handle command-line arguments within their Node.js applications. It serves as a native port of Python's argparse module, offering a robust solution for argument parsing. By utilizing argparse, developers can easily design user-friendly command-line interfaces (CLIs) that can interpret an array of input formats, thereby enhancing the flexibility and usability of applications. The argparse module simplifies the process of writing command-line scripts and managing them effectively, making it a vital tool for any developer's toolkit.
To get started with using argparse in your Node.js projects, you can easily install it via npm. Simply run `npm install argparse` in your project directory. This command fetches the latest version of argparse from the npm registry and installs it locally, ready for use in your application. With npm install argparse, setting up the module is straightforward and quick, allowing developers to immediately dive into creating more complex, parameter-driven CLI applications without the hassle of handling argument parsing manually.
Moreover, the benefits of using argparse extend beyond just simplifying command-line argument parsing. It supports features like sub-commands, which are perfect for complex scripts that perform multiple functions under a single umbrella command. It also allows for customizable help messages, which are crucial for end-user support, and automatically generates usage messages to prevent common user errors. By integrating argparse, Node.js developers can not only boost their productivity but also enhance the overall user experience, making their applications more intuitive and accessible.
Core dependencies of this npm package and its dev dependencies.
@babel/eslint-parser, @babel/plugin-syntax-class-properties, eslint, mocha, nyc
A README file for the argparse code repository. View Code
CLI arguments parser for node.js, with sub-commands support. Port of python's argparse (version 3.9.0).
Difference with original.
new ArgumentParser({ description: 'example', add_help: true })
.int
, float
, ....add_argument('-b', { type: 'int', help: 'help' })
.%r
format specifier uses require('util').inspect()
.More details in doc.
Following code is a JS program that takes a list of integers and produces either the sum or the max:
const { ArgumentParser } = require('argparse')
const parser = new ArgumentParser({ description: 'Process some integers.' })
let sum = ints => ints.reduce((a, b) => a + b)
let max = ints => ints.reduce((a, b) => a > b ? a : b)
parser.add_argument('integers', { metavar: 'N', type: 'int', nargs: '+',
help: 'an integer for the accumulator' })
parser.add_argument('--sum', { dest: 'accumulate', action: 'store_const',
const: sum, default: max,
help: 'sum the integers (default: find the max)' });
let args = parser.parse_args()
console.log(args.accumulate(args.integers))
Assuming the JS code above is saved into a file called prog.js, it can be run at the command line and provides useful help messages:
$ node prog.js -h
usage: prog.js [-h] [--sum] N [N ...]
Process some integers.
positional arguments:
N an integer for the accumulator
optional arguments:
-h, --help show this help message and exit
--sum sum the integers (default: find the max)
When run with the appropriate arguments, it prints either the sum or the max of the command-line integers:
$ node prog.js 1 2 3 4
4
$ node prog.js 1 2 3 4 --sum
10
If invalid arguments are passed in, it will issue an error:
$ node prog.js a b c
usage: prog.js [-h] [--sum] N [N ...]
prog.js: error: argument N: invalid 'int' value: 'a'
This is an example ported from Python. You can find detailed explanation here.
Since this is a port with minimal divergence, there's no separate documentation. Use original one instead, with notes about difference.
Available as part of the Tidelift Subscription
The maintainers of argparse and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.