bebopt
v0.2.4
Published
a more powerful, option parser for node.js
Downloads
28
Maintainers
Readme
Bebopt
a more powerful option parser for node.js
NOTE
If you're looking for documentation, please refer to the wiki.
Why another option parser?
Because I'm tired of using option parsers that don't do their jobs very well. Nearly every parser I've used has required either a workaround to actually make it work, or boilerplate code that's off-putting (at least, for some developers). In short: untraditional implementations and poor abstractions.
Some examples
Take optimist and yargs, for example: both of them are great parsers in their own right, but they provide no method or abstraction for handling arguments in the order in which they appear on the command-line.
Instead, the way I've seen most people use both of these excellent parsers inherently makes their programs handle args in the order in which they are handled:
#!/usr/bin/env node
// ex.js
// an example of optimist in action
var args = require('optimist')
.usage('Do stuff')
.describe('h', 'print this message and exit')
.describe('v', 'print program version and exit')
.argv;
if (args.v)
console.log('1.0');
process.exit(0);
if (args.h)
args.showHelp();
process.exit(0);
Because -v
is handled before -h
; so even if you run node ex.js -h -v
, the version string will always get printed instead of the expected usage information.
In optimist, the problem can sort of be resolved by looping over the resulting args
object using Object.key
like so:
#!/usr/bin/env node
// ex.js
// an example of optimist in action
var args = require('optimist')
.usage('Do stuff')
.describe('h', 'print this message and exit')
.describe('v', 'print program version and exit')
.argv;
// doesn't capture commands
// can also be done in a really ugly way like so:
// Object.keys(args).slice(Object.keys(args).indexOf('$0') + 1)
Object.keys(args).slice(2).forEach(function(key) {
/* process args using a switch */
});
I say sort of because the above solution doesn't capture commands, is ugly, and doesn't work with yargs!!