0.0.7 • Published 4 years ago

parse-node-argv v0.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

Parse arguments

npm i parse-node-argv

Usage

// $ node example/run/app.js -h -bar=xtt

const options = {
  alias: [
    ['--help', '-h'],
    ['--foo', '-bar', 'baz']
  ]
}
// optional`argv` - default `process.argv.slice(2)`
const args = require('parse-node-argv')(options[, argv])
// return object
{
  args:   ['-h', '-bar'],
  values: [ null, 'xtt'],
  alias: Map,
  is: (arg) => {...} : boolean,
  get: (arg) => {...} : boolean|null|string
}

// '--help' alias '-h'
args.is('--help') // true

// 'baz' alias '-bar'
args.get('baz') // 'xtt'

User arguments vs process.argv

// $ node app.js 1 2 3

const customArgv = process.argv
require('parse-node-argv')(null, customArgv)
// args:['node', 'app.js', '1', '2', '3']

// by default delete the first 2 items
require('parse-node-argv')()
// args:['1', '2', '3']

Options

include {array}

// $ node app.js -foo -bar -baz 1 2 null

options = {
  include: ['-foo', 1, '2', null, {}]
}
// return args:['-foo', '2'] - string
//                 -bar -baz - excluded
//               1, null, {} - not valid

exclude {array}

// $ node app.js -foo=123 -bar -baz

options = {
  include: ['-foo', '-bar'],
  exclude: ['-foo']
}
// return args:['-bar']
//               -foo   - excluded

alias {array} > example [ [1,2,3], [4,5,6] ]

// $ node app.js foo bar baz box ball

options = {
  include: ['foo', 'bar', 'baz', 'box'],
  exclude: ['bar'],
  alias:[
    ['foo', 'bar', 'baz']
  ]
}
// return args:['box']
//               ball  - excluded
//            foo baz  - alias -> exclude: ['bar']

Note

$ node app.js --foo
$ node app.js --foo=
$ node app.js --foo=""

args.is('--foo')  // true
args.get('--foo') // null
$ node app.js --foo=aaa -F=xxx

// options.alias:[['--foo', '-F']]
args.is('--foo')  // true
args.is('-F')     // true
args.get('--foo') // 'xxx' overwrite

Bomb

bomb

options.alias = [['--foo', '-f'], ['--foo']]
try {
  require('parse-node-argv')(options)
} catch (error) {
  // Error(`Aliases intersection`)
}
0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago