包详细信息

argparse

nodeca466.2mPython-2.02.0.1

CLI arguments parser. Native port of python's argparse.

cli, parser, argparse, option

自述文件

argparse

Build Status NPM version

CLI arguments parser for node.js, with sub-commands support. Port of python's argparse (version 3.9.0).

Difference with original.

  • JS has no keyword arguments support.
    • Pass options instead: new ArgumentParser({ description: 'example', add_help: true }).
  • JS has no python's types int, float, ...
    • Use string-typed names: .add_argument('-b', { type: 'int', help: 'help' }).
  • %r format specifier uses require('util').inspect().

More details in doc.

Example

test.js file:

#!/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());

Display help:

$ ./test.js -h
usage: test.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' }

API docs

Since this is a port with minimal divergence, there's no separate documentation. Use original one instead, with notes about difference.

  1. Original doc.
  2. Original tutorial.
  3. Difference with python.

argparse for enterprise

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.

更新日志

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

2.0.1 - 2020-08-29

Fixed

  • Fix issue with process.argv when used with interpreters (coffee, ts-node, etc.), #150.

2.0.0 - 2020-08-14

Changed

  • Full rewrite. Now port from python 3.9.0 & more precise following. See doc for difference and migration info.
  • node.js 10+ required
  • Removed most of local docs in favour of original ones.

1.0.10 - 2018-02-15

Fixed

  • Use .concat instead of + for arrays, #122.

1.0.9 - 2016-09-29

Changed

  • Rerelease after 1.0.8 - deps cleanup.

1.0.8 - 2016-09-29

Changed

  • Maintenance (deps bump, fix node 6.5+ tests, coverage report).

1.0.7 - 2016-03-17

Changed

  • Teach addArgument to accept string arg names. #97, @tomxtobin.

1.0.6 - 2016-02-06

Changed

  • Maintenance: moved to eslint & updated CS.

1.0.5 - 2016-02-05

Changed

  • Removed lodash dependency to significantly reduce install size. Thanks to @mourner.

1.0.4 - 2016-01-17

Changed

  • Maintenance: lodash update to 4.0.0.

1.0.3 - 2015-10-27

Fixed

  • Fix parse = in args: --examplepath="C:\myfolder\env=x64". #84, @CatWithApple.

1.0.2 - 2015-03-22

Changed

  • Relaxed lodash version dependency.

1.0.1 - 2015-02-20

Changed

  • Changed dependencies to be compatible with ancient nodejs.

1.0.0 - 2015-02-19

Changed

  • Maintenance release.
  • Replaced underscore with lodash.
  • Bumped version to 1.0.0 to better reflect semver meaning.
  • HISTORY.md -> CHANGELOG.md

0.1.16 - 2013-12-01

Changed

  • Maintenance release. Updated dependencies and docs.

0.1.15 - 2013-05-13

Fixed

  • Fixed #55, @trebor89

0.1.14 - 2013-05-12

Fixed

  • Fixed #62, @maxtaco

0.1.13 - 2013-04-08

Changed

  • Added .npmignore to reduce package size

0.1.12 - 2013-02-10

Fixed

  • Fixed conflictHandler (#46), @hpaulj

0.1.11 - 2013-02-07

Added

  • Added 70+ tests (ported from python), @hpaulj
  • Added conflictHandler, @applepicke
  • Added fromfilePrefixChar, @hpaulj

Fixed

  • Multiple bugfixes, @hpaulj

0.1.10 - 2012-12-30

Added

Fixed

  • Fixed options check for storeConst & appendConst actions, thanks to @hpaulj

0.1.9 - 2012-12-27

Fixed

  • Fixed option dest interferens with other options (issue #23), thanks to @hpaulj
  • Fixed default value behavior with * positionals, thanks to @hpaulj
  • Improve getDefault() behavior, thanks to @hpaulj
  • Improve negative argument parsing, thanks to @hpaulj

0.1.8 - 2012-12-01

Fixed

  • Fixed parser parents (issue #19), thanks to @hpaulj
  • Fixed negative argument parse (issue #20), thanks to @hpaulj

0.1.7 - 2012-10-14

Fixed

  • Fixed 'choices' argument parse (issue #16)
  • Fixed stderr output (issue #15)

0.1.6 - 2012-09-09

Fixed

  • Fixed check for conflict of options (thanks to @tomxtobin)

0.1.5 - 2012-09-03

Fixed

  • Fix parser #setDefaults method (thanks to @tomxtobin)

0.1.4 - 2012-07-30

Fixed

  • Fixed pseudo-argument support (thanks to @CGamesPlay)
  • Fixed addHelp default (should be true), if not set (thanks to @benblank)

0.1.3 - 2012-06-27

Fixed

  • Fixed formatter api name: Formatter -> HelpFormatter

0.1.2 - 2012-05-29

Fixed

  • Removed excess whitespace in help
  • Fixed error reporting, when parcer with subcommands called with empty arguments

Added

  • Added basic tests

0.1.1 - 2012-05-23

Fixed

  • Fixed line wrapping in help formatter
  • Added better error reporting on invalid arguments

0.1.0 - 2012-05-16

Added

  • First release.