options-to-args
Transform an options object into an array of args.
Suitable for use with spawn
and execFile
.
Differences from v1
-
There is no default prefix
-
New default behaviour, to use the old behaviour:
const args =const legacy = args
usage
const args = prefix`-` // [ '-model', 'ncc-1701', '-crew', 'kirk', '-crew', 'spock' ]
default behaviour
// [ 'option', 'value' ] // [ 'option', 10 ] // [ 'option', 'a', 'option', 'b' ] // [ 'option', 'k', 'v' ] // [ 'option' ] // [] // [ 'option' ] // [ 'option' ]
api
All methods will return a new instance with the updated configuration.
args(options)
Parses an options
object into an array and returns it.
.alias(from, to)
Add a mapping of one option name to another.
example
args version: true // [ `v` ]
.alias(mappings)
.alias
can also consume an object containing multiple mappings.
example
args
.prefix(prefix)
Add a command prefix.
.behaviour(type, function)
Override the default behaviour.
type
string representation of a type i.e. 'string'
. type-detect handles type detection.
function
type specific parser, called with an object containing:
parse // main parser (useful for recursive parsing) state // { alias, behaviour, prefix } option // option name value // option value
.behaviour(behaviours)
Replace all behaviours. behaviours
is an object with keys mapping to functions as described above.
example
const args =
.behaviours.default
Object containing all the default behaviours
.behaviours.legacy
Object containing all the legacy behaviours (used in v1)