Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

nodeca/argparse

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CINPM version

CLI arguments parser for node.js, withsub-commands support. Port of python'sargparse (version3.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 typesint,float, ...
    • Use string-typed names:.add_argument('-b', { type: 'int', help: 'help' }).
  • %r format specifier usesrequire('util').inspect().

More details indoc.

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')constparser=newArgumentParser({description:'Process some integers.'})letsum=ints=>ints.reduce((a,b)=>a+b)letmax=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)'});letargs=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 -husage: prog.js [-h] [--sum] N [N ...]Process some integers.positional arguments:  N           an integer for the accumulatoroptional 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 44$ node prog.js 1 2 3 4 --sum10

If invalid arguments are passed in, it will issue an error:

$ node prog.js a b cusage: 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 explanationhere.

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.

About

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

Resources

License

Security policy

Stars

Watchers

Forks


[8]ページ先頭

©2009-2025 Movatter.jp