Have ideas to improve npm?Join in the discussion! »

object-to-spawn-args

2.0.0 • Public • Published

view on npm npm module downloads Build Status Dependency 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-19 Lloyd Brookes <75pound@gmail.com>.

Install

npm i object-to-spawn-args

DownloadsWeekly Downloads

72,414

Version

2.0.0

License

MIT

Unpacked Size

5.08 kB

Total Files

4

Last publish

Collaborators

  • avatar