object-to-spawn-args
v2.0.1
Published
Converts an object to a child_process.spawn args array
Downloads
1,139,870
Readme
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 <[email protected]>.