Skip to content

Instantly share code, notes, and snippets.

@andineck
Created September 4, 2015 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andineck/0d1a0a546e660a96b215 to your computer and use it in GitHub Desktop.
Save andineck/0d1a0a546e660a96b215 to your computer and use it in GitHub Desktop.
Argument Parsing with node.js

Argument Parsing with node.js

and the great modules: minimist and subarg

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