options-to-args
transform an options object into an array of args
Last updated 3 years ago by rogerbf .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install options-to-args 
SYNC missed versions from official npm registry.

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 = require(`options-to-args`)
    const legacy = args.behaviour(args.behaviours.legacy)
    

usage

const args = require(`options-to-args`).prefix(`-`)

args({
  model: `ncc-1701`,
  crew: [ `kirk`, `spock` ]
})
// [ '-model', 'ncc-1701', '-crew', 'kirk', '-crew', 'spock' ]

default behaviour

args({ option: `value` })        // [ 'option', 'value' ]
args({ option: 10 })             // [ 'option', 10 ]
args({ option: [ `a`, `b` ] })   // [ 'option', 'a', 'option', 'b' ]
args({ option: { k: `v` } })     // [ 'option', 'k', 'v' ]
args({ option: true })           // [ 'option' ]
args({ option: false })          // []
args({ option: null })           // [ 'option' ]
args({ option: undefined })      // [ '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.alias(`version`, `v`)({ version: true })
// [ `v` ]

.alias(mappings)

.alias can also consume an object containing multiple mappings.

example

args.alias({ version: `v`, longterm: `lts` })

.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 = require(`options-to-args`).behaviour(
  Object.assign(
    {},
    args.behaviours.default,
    require(`./customArrayBehaviour`) // module.exports = { Array: fn }
  )
)

.behaviours.default

Object containing all the default behaviours

.behaviours.legacy

Object containing all the legacy behaviours (used in v1)

Current Tags

  • 2.0.1                                ...           latest (3 years ago)

6 Versions

  • 2.0.1                                ...           3 years ago
  • 2.0.0                                ...           3 years ago
  • 1.0.3                                ...           3 years ago
  • 1.0.2                                ...           3 years ago
  • 1.0.1                                ...           3 years ago
  • 0.0.1                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (2)
Dev Dependencies (14)

Copyright 2014 - 2016 © taobao.org |