object-to-spawn-args

2.0.1 • Public • Published

view on npm npm module downloads Gihub repo dependents Gihub package dependents Build Status js-standard-style

object-to-spawn-args

Converts an options object to an array suitable for passing to child_process.spawn().

Single letter object properties (e.g. c: 'red') convert to short-option args (e.g. -c red). Longer object properties (e.g. colour: 'red') convert to long-option args (e.g. --colour red). Object property values equalling true convert to flags (e.g. -l).

Synopsis

Simple usage:

> const objectToSpawnArgs = require('object-to-spawn-args')

> const spawnArgs = objectToSpawnArgs({
  l: true,
  c: 'red',
  name: 'pete',
  tramp: true
})

> console.log(spawnArgs)
[ '-l', '-c', 'red', '--name', 'pete', '--tramp' ]

Alternatively, convert to --object=value notation.

> const options = {
  l: true,
  c: 'red',
  name: 'pete',
  tramp: true
}
> const spawnArgs = objectToSpawnArgs(options, { optionEqualsValue: true })

> console.log(spawnArgs)
[ '-l', '-c=red', '--name=pete', '--tramp' ]

Typical real-life example.

const objectToSpawnArgs = require('object-to-spawn-args')
const spawn = require('child_process').spawn

const options = {
  l: true,
  a: true
}

spawn('ls', objectToSpawnArgs(options), { stdio: 'inherit' })

Installation

$ npm install object-to-spawn-args

© 2014-21 Lloyd Brookes <75pound@gmail.com>.

Readme

Keywords

Package Sidebar

Install

npm i object-to-spawn-args

Weekly Downloads

213,289

Version

2.0.1

License

MIT

Unpacked Size

5.32 kB

Total Files

4

Last publish

Collaborators

  • 75lb