# options-to-args

> transform an options object into an array of args

Latest version **2.0.1** (published 2017-03-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install options-to-args
pnpm add options-to-args
yarn add options-to-args
bun add options-to-args
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2017-03-17 |
| First published | 2017-02-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | rogerbf |
| Maintainers | rogerbf |
| Keywords | spawn, execFile, options, args, arguments, subargs |

## Links

- npm: https://www.npmjs.com/package/options-to-args
- Repository: https://github.com/rogerbf/options-to-args
- Homepage: https://github.com/rogerbf/options-to-args#readme
- Issues: https://github.com/rogerbf/options-to-args/issues
- npm.io page: https://npm.io/package/options-to-args

## Dependencies (2)

- [protostar](https://npm.io/package/protostar.md) ^0.0.1
- [type-detect](https://npm.io/package/type-detect.md) ^4.0.0

## Recent versions

- 2.0.1 (latest) — 2017-03-17
- 2.0.0 — 2017-03-17
- 1.0.3 — 2017-03-14
- 1.0.2 — 2017-03-11
- 1.0.1 — 2017-03-06
- 0.0.1 — 2017-02-23

## README

# 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:

  ```javascript
  const args = require(`options-to-args`)
  const legacy = args.behaviour(args.behaviours.legacy)
  ```

## usage

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

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

### default behaviour

```javascript
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

```javascript
args.alias(`version`, `v`)({ version: true })
// [ `v` ]
```

### `.alias(mappings)`

`.alias` can also consume an object containing multiple mappings.

#### example

```javascript
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](https://github.com/chaijs/type-detect) handles type detection.

`function` type specific parser, called with an object containing:

```javascript
{
  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

```javascript
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)

---
_Source: https://npm.io/package/options-to-args · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
