• Stars
    star
    484
  • Rank 87,884 (Top 2 %)
  • Language
    JavaScript
  • License
    Other
  • Created about 12 years ago
  • Updated 6 months 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,185
star
2

pako

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

pica

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

probe-image-size

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

babelfish

human friendly i18n for javascript (node.js + browser)
JavaScript
250
star
6

image-blob-reduce

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

tabex

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

url-unshort

Short links expander for node.js
JavaScript
112
star
9

ndoc

js port of pdoc, with extentions
JavaScript
100
star
10

charlatan

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

nodeca

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

bag.js

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

idoit

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

glur

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

multimath

WebAssembly wrapper to simplify fast math coding
JavaScript
66
star
16

embedza

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

promise-memoize

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

navit

Simple client testing from your scripts
JavaScript
47
star
19

unhomoglyph

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

nntp-server

NNTP server for readers
JavaScript
26
star
21

puncher

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

eslint-plugin-nodeca

Indentation check rule for ESLint
JavaScript
12
star
23

nodeca.core

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

types

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

nodeca.users

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

event-wire

Mediator with dynamic responsibility chains
JavaScript
9
star
27

hike-js

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

plurals-cldr

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

relimit

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

js-yaml-js-types

Extra js types for js-yaml
JavaScript
7
star
31

mimoza

Simple mime-type tools library
JavaScript
7
star
32

pointer

router for node.js / client
JavaScript
6
star
33

nodeca.forum

Nodeca forum app
JavaScript
3
star
34

redis-if

JavaScript
3
star
35

nodeca-design

Nodeca interface mockups
HTML
3
star
36

fontomas

2
star
37

nodeca.blogs

Blogs component for Nodeca
JavaScript
2
star
38

nodeca.clubs

Clubs component for Nodeca
JavaScript
2
star
39

nodeca.market

Market component for Nodeca
JavaScript
1
star
40

charcount

Count visual length of javascript string
JavaScript
1
star