2.0.1 • Published 3 years ago

object-to-spawn-args v2.0.1

Weekly downloads
80,070
License
MIT
Repository
github
Last release
3 years ago

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\.