minimist vs caporal vs commander vs yargs
Node.js 命令行解析库
minimistcaporalcommanderyargs类似的npm包:

Node.js 命令行解析库

命令行解析库用于处理 Node.js 应用程序的命令行参数,提供用户友好的接口,帮助开发者轻松创建命令行工具。这些库可以解析输入参数、生成帮助信息、处理命令和选项,并提供灵活的方式来定义命令行行为。选择合适的库可以提高开发效率和用户体验。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
minimist69,538,96066354.5 kB143 年前MIT
caporal03,461-286 年前MIT
commander028,020209 kB92 个月前MIT
yargs011,458231 kB31010 个月前MIT

功能对比: minimist vs caporal vs commander vs yargs

功能丰富性

  • minimist:

    Minimist 主要专注于参数解析,功能相对简单,适合快速解析命令行参数。它不提供额外的功能,如帮助信息或命令管理,适合小型项目。

  • caporal:

    Caporal 提供了丰富的功能,包括命令和选项的定义、参数验证、自动生成帮助信息和版本信息等。它支持子命令和多种参数类型,使得构建复杂的命令行工具变得简单。

  • commander:

    Commander 提供了基本的命令和选项解析功能,适合构建简单的命令行工具。虽然功能不如 Caporal 丰富,但它的 API 清晰易用,适合快速开发。

  • yargs:

    Yargs 提供了强大的功能,包括命令和选项解析、参数验证、自动生成帮助信息和支持子命令等。它非常灵活,适合需要复杂命令行解析的应用。

学习曲线

  • minimist:

    Minimist 的学习曲线非常低,因为它的功能简单明了,适合需要快速解析参数的开发者。

  • caporal:

    Caporal 的学习曲线相对平缓,提供了清晰的文档和示例,适合初学者和希望快速上手的开发者。

  • commander:

    Commander 的学习曲线也很平滑,API 简洁明了,适合快速上手,特别是对于有 JavaScript 基础的开发者。

  • yargs:

    Yargs 的学习曲线相对较陡,尽管功能强大,但需要更多的时间去理解其复杂的 API 和用法。

扩展性

  • minimist:

    Minimist 的扩展性有限,主要用于简单的参数解析,不适合需要复杂功能的场景。

  • caporal:

    Caporal 允许开发者通过插件和自定义命令扩展功能,适合需要高度自定义的命令行工具。

  • commander:

    Commander 提供了一定的扩展性,允许开发者添加自定义命令和选项,但不如 Caporal 灵活。

  • yargs:

    Yargs 提供了良好的扩展性,支持自定义命令和参数验证,适合需要复杂逻辑的命令行应用。

性能

  • minimist:

    Minimist 性能极佳,因其功能简单,适合快速解析命令行参数,几乎没有性能开销。

  • caporal:

    Caporal 在处理复杂命令和选项时性能良好,但由于功能丰富,可能在处理极大量参数时稍显缓慢。

  • commander:

    Commander 性能优越,适合处理简单到中等复杂度的命令行工具,响应速度快。

  • yargs:

    Yargs 性能良好,能够处理复杂的命令行解析,但在极端情况下可能会受到性能影响。

社区支持与维护

  • minimist:

    Minimist 是一个简单的库,社区支持相对较少,但因其简单性,维护成本低。

  • caporal:

    Caporal 拥有活跃的社区支持和定期更新,适合希望获得持续支持的开发者。

  • commander:

    Commander 是一个成熟的库,拥有广泛的社区支持和丰富的文档,适合长期项目。

  • yargs:

    Yargs 拥有活跃的社区和良好的文档支持,适合需要长期维护和更新的项目。

如何选择: minimist vs caporal vs commander vs yargs

  • minimist:

    选择 Minimist 如果你只需要一个简单的参数解析器,适合快速解析命令行参数而不需要额外的功能。它非常轻量,适合小型项目或脚本。

  • caporal:

    选择 Caporal 如果你需要一个功能丰富且易于使用的命令行工具,支持复杂的命令和选项,并且希望有内置的帮助和版本管理功能。它适合需要快速开发命令行应用的场景。

  • commander:

    选择 Commander 如果你需要一个轻量级且灵活的解决方案,适合构建简单到中等复杂度的命令行工具。它提供了清晰的 API 和良好的文档,适合快速上手。

  • yargs:

    选择 Yargs 如果你需要一个功能强大且灵活的库,支持复杂的命令行解析和丰富的功能,如子命令、参数验证和自动生成帮助信息。它适合需要高度自定义的命令行工具。

minimist的README

minimist Version Badge

github actions coverage License Downloads

npm badge

parse argument options

This module is the guts of optimist's argument parser without all the fanciful decoration.

example

var argv = require('minimist')(process.argv.slice(2));
console.log(argv);
$ node example/parse.js -a beep -b boop
{ _: [], a: 'beep', b: 'boop' }
$ node example/parse.js -x 3 -y 4 -n5 -abc --beep=boop foo bar baz
{
	_: ['foo', 'bar', 'baz'],
	x: 3,
	y: 4,
	n: 5,
	a: true,
	b: true,
	c: true,
	beep: 'boop'
}

security

Previous versions had a prototype pollution bug that could cause privilege escalation in some circumstances when handling untrusted user input.

Please use version 1.2.6 or later:

methods

var parseArgs = require('minimist')

var argv = parseArgs(args, opts={})

Return an argument object argv populated with the array arguments from args.

argv._ contains all the arguments that didn't have an option associated with them.

Numeric-looking arguments will be returned as numbers unless opts.string or opts.boolean is set for that argument name.

Any arguments after '--' will not be parsed and will end up in argv._.

options can be:

  • opts.string - a string or array of strings argument names to always treat as strings

  • opts.boolean - a boolean, string or array of strings to always treat as booleans. if true will treat all double hyphenated arguments without equal signs as boolean (e.g. affects --foo, not -f or --foo=bar)

  • opts.alias - an object mapping string names to strings or arrays of string argument names to use as aliases

  • opts.default - an object mapping string argument names to default values

  • opts.stopEarly - when true, populate argv._ with everything after the first non-option

  • opts['--'] - when true, populate argv._ with everything before the -- and argv['--'] with everything after the --. Here's an example:

    > require('./')('one two three -- four five --six'.split(' '), { '--': true })
    {
      _: ['one', 'two', 'three'],
      '--': ['four', 'five', '--six']
    }
    

    Note that with opts['--'] set, parsing for arguments still stops after the --.

  • opts.unknown - a function which is invoked with a command line parameter not defined in the opts configuration object. If the function returns false, the unknown option is not added to argv.

install

With npm do:

npm install minimist

license

MIT