argparse
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
underscore.string | 2.1.1 | 84.18 kB | MIT | prod | 1 |
underscore | 1.3.3 | 57.32 kB | UNKNOWN | prod | 2 |
Argparse is a Command Line Interface (CLI) argument parser for Node.js, and is a native port of Python's argparse module. This package is designed to facilitate the parsing of command line arguments in your Node.js applications. Argparse supports sub-commands, similar to its original Python counterpart, and conveniently offers similar functionality in a JavaScript environment.
Utilizing argparse in your Node.js application requires importing the ArgumentParser object from the 'argparse' module and using its methods to define the arguments your application expects. Here's a basic usage example:
#!/usr/bin/env node
'use strict';
const { ArgumentParser } = require('argparse');
const { version } = require('./package.json');
const parser = new ArgumentParser({
description: 'Argparse example'
});
parser.add_argument('-v', '--version', { action: 'version', version });
parser.add_argument('-f', '--foo', { help: 'foo bar' });
parser.add_argument('-b', '--bar', { help: 'bar foo' });
parser.add_argument('--baz', { help: 'baz bar' });
console.dir(parser.parse_args());
In this example, the script accepts several optional arguments. Users can invoke the script with these flags followed by their respective values from the command line as follows:
$ ./test.js -f=3 --bar=4 --baz 5
{ foo: '3', bar: '4', baz: '5' }
The parser.parse_args()
method parses the arguments and returns an object mapping argument names to their given values.
The Argparse documentation can be found referenced in the original Python's argparse documentation and the original Python argparse tutorial. Additionally, you can also find more details including the difference with Python's argparse in the 'doc' directory of the argparse GitHub repository.