-
Notifications
You must be signed in to change notification settings - Fork 0
examples
James edited this page Apr 28, 2017
·
1 revision
- Numeric-looking arguments will be returned as numbers unless
opts.string
oropts.boolean
is set for that argument name.
var argv = fwf(['--cow', '--honk=eh', '--meep=mahna'], {boolean: ['cow', 'honk']})
// {
// _: [],
// cow: true,
// honk: true,
// meep: 'mahna'
// }
var argv = fwf(['-moo', '--honk', 'cow'], {boolean: true})
// {
// honk: true,
// _: ['moo', 'cow'],
// }
ensure a value is a string, even when number-like
var argv = fwf(['--nan', '99'], {string: ['nan']})
// {nan: '99'}
var argv = fwf(['--specificallytrue'], {
boolean: ['eh', 'canada'],
default: {
eh: true,
canada: false,
unlesstrue: false,
maybe: null,
},
})
// {
// _: [],
// unlesstrue: true,
// sometrue: true,
// somefalse: false,
// maybe: null
// }
returns the FunWithFlags instance rather than argv
var obj = fwf(['-a'], {obj: true})
var aliased = ['-h', 'herpde', '--derp', 'derpde']
var opts = {
alias: {h: 'herp'},
}
var argv = fwf(aliased, opts)
// {
// _: [],
// h: 'herpde',
// herp: 'herpde',
// derp: 'derpde',
// }
fwf(['--a.b', '22'], {
string: 's',
alias: {'s': 'str', 'a.b': 'aa.bb'},
default: {'a.b': 11},
})
fwf(['-mo', '--honk', 'cow'], {boolean: true})
// {m: true, o: true, honk: true}
var FunWithFlags = require('funwithflags')
class Eh extends FunWithFlags {
varructor() {
super()
console.log('eh!', this)
}
}
var eh = new Eh()
var fwf = new FunWithFlags()
console.log(fwf)
convert arguments without dashes to dashed
fwf(['globbing', `"**/*"`], {vars: true})
// {globbing: `"**/*"`}
captures the unknown args, similar to how
vars
does automatically
var unknown = []
function unknownFn(arg) {
unknown.push(arg)
return true
}
fwf(['--proper-flag', 'globbing', `"**/*"`], {
unknown: unknownFn,
boolean: 'proper-flag',
})
console.log(unknown) // ['globbing', `"**/*"`]
Note that with
opts['--']
set, parsing for arguments still stops after the--
.
-
argv._
contains all the arguments that didn't have an option associated with them. - Any arguments after
'--'
will not be parsed and will end up inargv._
. -
opts['--']
- when true:- populate
argv._
with everything before the--
- populate
argv['--']
with everything after the--
.
- populate
require('funwithflags')('one two three -- four five --six'.split(' '), { '--': true })
// { _: [ 'one', 'two', 'three' ], '--': [ 'four', 'five', '--six' ] }
$ node example/parse.js -a beep -b boop
var argv = require('funwithflags')(process.argv.slice(2))
argv === { _: [], a: 'beep', b: 'boop' }
$ node example/parse.js -x 3 -y 4 -n5 -abc --beep=boop foo bar baz
var argv = require('funwithflags')(process.argv.slice(2))
argv === {
_: [ 'foo', 'bar', 'baz' ],
x: 3,
y: 4,
n: 5,
a: true,
b: true,
c: true,
beep: 'boop'
}