包详细信息

argv-parse

paulcpederson2.9kISC1.0.1

Parse command line arguments.

args, argv, cli, parse

自述文件

argv-parse

npm travis standard

Parse command line arguments. This is a lot like yargs or optimist but not as feature rich.

You can specify flags, aliases, or not. Can pass arrays, strings, and booleans. No dependencies.

If you don't need array arguments, I'd recommend using minimist which is very good.

Install

npm install argv-parse

Usage

Say you had a file like foo-bar-qux.js which needed to parse arguments:

var argv = require('argv-parse')

// accepts a definition object and args
// if args is undefined, defaults to process.argv.slice(2)
var args = argv({
  foo: {
    type: 'boolean',
    alias: 'f'
  },
  bar: {
    type: 'string',
    alias: 'b'
  },
  qux: {
    type: 'array',
    alias: 'q'
  },
  norf: {
    type: 'boolean',
    alias: 'n'
  }
})
console.dir(args)
$ node foo-bar-qux.js --foo
{
  foo: true
}

$ node foo-bar-qux.js --bar hey
{
  bar: 'hey'
}

$ node foo-bar-qux.js --qux thing1 thing2 thing3
{
  qux: ['thing1', 'thing2', 'thing3']
}

$ node foo-bar-qux.js -f -b hey -q thing1 thing2 thing3
{
  foo: true,
  bar: 'hey',
  qux: ['thing1', 'thing2', 'thing3']
}

$ node foo-bar-qux.js -fn
{
  foo: true,
  norf: true
}

$ node foo-bar-qux.js not specified
{
  _: ['not', 'specified']
}

$ node foo-bar-qux.js --unknown surprise
{
  unknown: 'surprise'
}

If you don't care about aliases or types, you don't actually need to configure anything, argv-parse will intelligently guess how the unknown flag should be interpreted:

simple.js

var argv = require('argv-parse')
var args = argv()
console.dir(args)
$ node simple.js --unknown
{
  unknown: true
}

$ node simple.js --unknown hey
{
  unknown: 'hey'
}

$ node simple.js --unknown hey you
{
  unknown: ['hey', 'you']
}

$ node simple.js first second --unknown hey you
{
  _: ['first', 'second'],
  unknown: ['hey', 'you']
}

License

ISC

更新日志

argv-parse change log

All notable changes to this project will be documented in this file. This project adheres to Semantic Versioning.

v1.0.1

  • fixed bug where if first arguments weren't flags they were skipped

v1.0.0

  • parses booleans, strings, arrays
  • collects extra arguments as _
  • aliases
  • grouped aliases
  • intelligent parsing of unlisted flags