Skip to content

paulcpederson/argv-parse

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Parse command line arguments in Node.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published