• Stars
    star
    489
  • Rank 89,990 (Top 2 %)
  • Language
    JavaScript
  • License
    Other
  • Created over 12 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

CLI arguments parser for node.js. JS port of python's argparse module.

argparse

CI 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

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.

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.

More Repositories

1

js-yaml

JavaScript YAML parser and dumper. Very fast.
JavaScript
6,289
star
2

pako

high speed zlib port to javascript, works in browser & node.js
JavaScript
5,550
star
3

pica

Resize image in browser with high quality and high speed
JavaScript
3,759
star
4

probe-image-size

Get image size without full download. Supported image types: JPG, GIF, PNG, WebP, BMP, TIFF, SVG, PSD, ICO.
JavaScript
984
star
5

image-blob-reduce

Resize image blobs with high quality. Pica's wrapper to work with file inputs.
JavaScript
272
star
6

babelfish

human friendly i18n for javascript (node.js + browser)
JavaScript
255
star
7

tabex

Cross-tab message bus for browsers.
JavaScript
219
star
8

url-unshort

Short links expander for node.js
JavaScript
115
star
9

ndoc

js port of pdoc, with extentions
JavaScript
101
star
10

charlatan

Fake identities generator for node.js (names, addresses, phones, IPs and others). Supports multiple languages.
JavaScript
96
star
11

nodeca

Forums / Blogs / Groups / Classfields / ... platform. Fork this to make your own config.
JavaScript
96
star
12

bag.js

JS / CSS / files loader + key/value storage
JavaScript
90
star
13

idoit

Redis-backed task queue engine with advanced task control and eventual consistency
JavaScript
74
star
14

glur

Fast gaussian blur in pure JavaScript via IIR filer
JavaScript
72
star
15

multimath

WebAssembly wrapper to simplify fast math coding
JavaScript
70
star
16

embedza

Create HTML snippets/embeds from URLs using info from oEmbed, Open Graph, meta tags.
JavaScript
64
star
17

promise-memoize

Memoize promise-returning functions. Includes cache expire and prefetch.
JavaScript
59
star
18

navit

Simple client testing from your scripts
JavaScript
48
star
19

unhomoglyph

Replace all homoglyphs with base characters. Useful to detect similar strings.
JavaScript
39
star
20

nntp-server

NNTP server for readers
JavaScript
27
star
21

puncher

Library to set timestamps in your application requests & genegate profiling tree.
JavaScript
14
star
22

eslint-plugin-nodeca

Indentation check rule for ESLint
JavaScript
13
star
23

nodeca.core

Nodeca core app (admin panel, loader, bundler)
JavaScript
12
star
24

types

Collection of extra types (structures, classes) for JavaScript.
JavaScript
12
star
25

nodeca.users

Nodeca user app (login / register / albums / profiles)
JavaScript
11
star
26

event-wire

Mediator with dynamic responsibility chains
JavaScript
10
star
27

hike-js

Javascript port of Hike (Ruby) - a library for finding files in a set of paths
JavaScript
10
star
28

plurals-cldr

Plurals suport for JS, autogenerated from CLDR.
JavaScript
9
star
29

relimit

Rate limiter with tuneable scheduler and distributed run support
JavaScript
9
star
30

js-yaml-js-types

Extra js types for js-yaml
JavaScript
8
star
31

mimoza

Simple mime-type tools library
JavaScript
8
star
32

pointer

router for node.js / client
JavaScript
7
star
33

nodeca.forum

Nodeca forum app
JavaScript
4
star
34

redis-if

JavaScript
4
star
35

nodeca-design

Nodeca interface mockups
HTML
4
star
36

fontomas

3
star
37

nodeca.blogs

Blogs component for Nodeca
JavaScript
3
star
38

nodeca.market

Market component for Nodeca
JavaScript
2
star
39

nodeca.clubs

Clubs component for Nodeca
JavaScript
2
star
40

charcount

Count visual length of javascript string
JavaScript
2
star
41

bkv

JavaScript
1
star
42

nodeca.nntp

JavaScript
1
star
43

nodeca.vbconvert

vBulletin -> Nodeca convertor
JavaScript
1
star
44

nodeca-doc

Nodeca internals documentation
CSS
1
star
45

nodeca.search

JavaScript
1
star
46

.github

1
star
47

nodeca.site

Custom configuration for nodeca
JavaScript
1
star