and the great modules:
minimist
andsubarg
Created
September 4, 2015 06:42
-
-
Save andineck/0d1a0a546e660a96b215 to your computer and use it in GitHub Desktop.
Argument Parsing with node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var chalk = require('chalk'); | |
if (require.main === module) { | |
console.log(chalk.inverse("process.argv(slice2);"), process.argv.slice(2)); | |
console.log(chalk.inverse("require('minimist')(process.argv.slice(2));"), require('minimist')(process.argv.slice(2))); | |
console.log(chalk.inverse("require('subarg')(process.argv.slice(2));"), require('subarg')(process.argv.slice(2))); | |
} | |
/* | |
Output: | |
io:glint-tasks andineck$ node arguments.js --hello world -abc c1 nothing new here --sub [stuff -xyz q] | |
process.argv(slice2); [ '--hello', | |
'world', | |
'-abc', | |
'c1', | |
'nothing', | |
'new', | |
'here', | |
'--sub', | |
'[stuff', | |
'-xyz', | |
'q]' ] | |
require('minimist')(process.argv.slice(2)); { _: [ 'nothing', 'new', 'here' ], | |
hello: 'world', | |
a: true, | |
b: true, | |
c: 'c1', | |
sub: '[stuff', | |
x: true, | |
y: true, | |
z: 'q]' } | |
require('subarg')(process.argv.slice(2)); { _: [ 'nothing', 'new', 'here' ], | |
hello: 'world', | |
a: true, | |
b: true, | |
c: 'c1', | |
sub: { _: [ 'stuff' ], x: true, y: true, z: 'q' } } | |
io:glint-tasks andineck$ | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment