LineParser.js
LineParser is a meta-data driven command line parser.
Features
- Command line specification with meta-data in JSON
- Usage pattern matching
- Subcommand
- Multiple occurrences of an option, e.g. grep -e foo -e bar
- Help info
Sample
#!/usr/bin/env node // use meta-data in JSON to define the command line specificationvar meta = program : 'adb' name : 'Android Debug Bridge' version : '1.0.3' subcommands : 'connect' 'disconnect' 'shell' 'push' 'install' options : flags : /* short_name, name, description */ 'h' 'help' 'print program usage' 'r' 'reinstall' 'reinstall package' 'l' 'localhost' 'localhost' parameters : /* short_name, name, description, default_value */ null 'host' 'adb server hostname or IP address' null 'p' 'port' 'adb server port' 5037 usages : /* subcommand, options, positional-arguments, description, handler */ 'connect' 'host' '[port]' null 'connect to adb server' adb_connect 'connect' 'l' null 'connect to the local adb server' adb_connect 'disconnect' null null 'disconnect from adb server' adb_disconnect 'shell' null '[cmd]' 'run shell commands' adb_shell 'push' null 'src' 'dest' 'push file to adb server' adb_push 'install' 'r' 'package' 'install package' adb_install null 'h' null 'help' adb_help null null null 'help' adb_help ; // parse command line args based on the meta data try var lineparser = ; var parser = lineparser; // print help var help = parser; console; // the handler adb_connect will be invoked parser; // the handler adb_install will be invoked parser; // the handler adb_push will be invoked parser; // the handler adb_shell will be invoked parser;catch e // exception will be thrown if there's an error with the meta data console; // callbacks for various usages { console;} { if rflagsl console; else console; console;} { console; } { if 0 == rargs console; else var cmd = 'Run command: ' for var i = 0; i < rargslength; ++i cmd += ' ' + rargsi; console; } { console; } { console; }