argparse

中文文档
3.0.4发布于 2026/10/24PSF-2.0TypeScriptCommonJS零依赖近 30 天下载 10.8亿

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

原始使用文档

argparse

CI NPM version

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

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().

See the complete list of differences from Python. Users upgrading from v2 should read the migration guide.

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

options:
  -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. Differences from Python.
  4. Migration from v2 to v3.

版本与发布记录

argparse 共发布 35 个版本,首次发布于 2012/05/17;过去 12 个月发布了 5 个版本。

版本发布日期距今
3.0.4latest2026/10/2412 天前
3.0.32026/10/1621 天前
3.0.22026/09/111 个月前
3.0.12026/08/262 个月前
3.0.02026/07/143 个月前
2.0.12020/08/296 年前

关于 argparse 的常见问题

argparse 是什么?

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

如何安装 argparse?

在项目目录运行 npm install argparse,或使用 pnpm add / yarn add / bun add。

argparse 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

argparse 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

argparse 可以商用吗?

argparse 使用 PSF-2.0 许可。声明的许可为 PSF-2.0,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

argparse 的下载量有多少?

根据 npm 官方下载统计,argparse 在 2026/10/05 – 2026/11/03 这 30 天内被下载 1,078,899,617 次,日均约 35,963,321 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

argparse 还在维护吗?

最新版本 3.0.4 发布于 2026/10/24(12 天前);至今共发布 35 个版本,其中过去 12 个月发布了 5 个;首次发布于 2012/05/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。