argparse

Build Status NPM version

CLI arguments parser for node.js. Javascript port of python's argparse module (original version 3.2). That's a full port, except some very rare options, recorded in issue tracker.

NB. Difference with original.

Example

test.js file:

```javascript

!/usr/bin/env node

'use strict';

var ArgumentParser = require('../lib/argparse').ArgumentParser; var parser = new ArgumentParser({ version: '0.0.1', addHelp:true, description: 'Argparse example' }); parser.addArgument( [ '-f', '--foo' ], { help: 'foo bar' } ); parser.addArgument( [ '-b', '--bar' ], { help: 'bar foo' } ); parser.addArgument( '--baz', { help: 'baz bar' } ); var args = parser.parseArgs(); console.dir(args); ```

Display help:

``` $ ./test.js -h usage: example.js [-h] [-v] [-f FOO] [-b BAR] [--baz BAZ]

Argparse example

Optional arguments: -h, --help Show this help message and exit. -v, --version Show program's version number and exit. -f FOO, --foo FOO foo bar -b BAR, --bar BAR bar foo --baz BAZ baz bar ```

Parse arguments:

$ ./test.js -f=3 --bar=4 --baz 5 { foo: '3', bar: '4', baz: '5' }

More examples.

ArgumentParser objects

new ArgumentParser({parameters hash});

Creates a new ArgumentParser object.

Supported params:

Not supported yet

Details in original ArgumentParser guide

addArgument() method

ArgumentParser.addArgument(name or flag or [name] or [flags...], {options})

Defines how a single command-line argument should be parsed.

Options:

Details in original add_argument guide

Action (some details)

ArgumentParser objects associate command-line arguments with actions. These actions can do just about anything with the command-line arguments associated with them, though most actions simply add an attribute to the object returned by parseArgs(). The action keyword argument specifies how the command-line arguments should be handled. The supported actions are:

Details in original action guide

Sub-commands

ArgumentParser.addSubparsers()

Many programs split their functionality into a number of sub-commands, for example, the svn program can invoke sub-commands like svn checkout, svn update, and svn commit. Splitting up functionality this way can be a particularly good idea when a program performs several different functions which require different kinds of command-line arguments. ArgumentParser supports creation of such sub-commands with addSubparsers() method. The addSubparsers() method is normally called with no arguments and returns an special action object. This object has a single method addParser(), which takes a command name and any ArgumentParser constructor arguments, and returns an ArgumentParser object that can be modified as usual.

Example:

sub_commands.js ```javascript

!/usr/bin/env node

'use strict';

var ArgumentParser = require('../lib/argparse').ArgumentParser; var parser = new ArgumentParser({ version: '0.0.1', addHelp:true, description: 'Argparse examples: sub-commands', });

var subparsers = parser.addSubparsers({ title:'subcommands', dest:"subcommand_name" });

var bar = subparsers.addParser('c1', {addHelp:true}); bar.addArgument( [ '-f', '--foo' ], { action: 'store', help: 'foo3 bar3' } ); var bar = subparsers.addParser( 'c2', {aliases:['co'], addHelp:true} ); bar.addArgument( [ '-b', '--bar' ], { action: 'store', type: 'int', help: 'foo3 bar3' } );

var args = parser.parseArgs(); console.dir(args);

```

Details in original sub-commands guide

Contributors

others

License

Copyright (c) 2012 Vitaly Puzrin. Released under the MIT license. See LICENSE for details.