@sovpro/zclopts
v1.0.0
Published
Zero-configuration command-line option parsing
Downloads
4
Maintainers
Readme
zclopts
Zero-configuration command-line option parsing
Example
Example script:
// show-options.js
import zclopts from '@sovpro/zclopts'
const argv = process.argv.slice (2)
const opts = zclopts (argv)
console.log (opts)
Example execution:
node ./show-options.js -abc yes -no-xyz no --param one two -vvv --param three
Example output:
Map {
'a' => true,
'b' => true,
'c' => 'yes',
'x' => false,
'y' => false,
'z' => 'no',
'param' => [ 'one two', 'three' ],
'v' => 3 }
Flags
node my-script.js -f
Map { 'f' => true }
node my-script.js -no-f
Map { 'f' => false }
Double Flag
node my-script.js -ff
Map { 'f' => 2 }
node my-script.js -no-ff
Map { 'f' => false }
Flag with value
node my-script.js -f value
Map { 'f' => 'value' }
Flag with sequential values
node my-script.js -f value-1 value-2
Map { 'f' => 'value-1 value-2' }
Flag occurring more than once
node my-script.js -f value-1 value-2 -f value-3 value-4
Map { 'f' => 'value-3 value-4' }
Flag group
node my-script.js -abc
Map { 'a' => true, 'b' => true, 'c' => true }
node my-script.js -no-abc
Map { 'a' => false, 'b' => false, 'c' => false }
Flag group with trailing value
node my-script.js -abc value
Map { 'a' => true, 'b' => true, 'c' => 'value' }
node my-script.js -no-abc value
Map { 'a' => false, 'b' => false, 'c' => 'value' }
Flag as a parameter
node my-script.js -f=value
Map { 'f' => 'value' }
Params
node my-script.js --param
Map { 'param' => undefined }
Param with value
node my-script.js --param value
Map { 'param' => 'value' }
node my-script.js --param=value
Map { 'param' => 'value' }
Param with sequential values
node my-script.js --param value-1 value-2
Map { 'param' => 'value-1 value-2' }
Param occurring more than once
node my-script.js --param value-1 value-2 --param value-3 value-4
Map { 'param' => ['value-1 value-2', 'value-3 value-4'] }