started work on argv parser to support nested commands...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-06-13 05:13:58 +03:00
parent 2a3134ad4c
commit 63c8e7447e

31
test.js
View File

@ -157,13 +157,14 @@ var ArgvParser = function(spec){
// NOTE: this is intentionally not dynamic... // NOTE: this is intentionally not dynamic...
spec = Object.assign({ spec = Object.assign({
// builtin options... // builtin options...
h: 'help', '-h': '-help',
// XXX revise... // XXX revise...
help: { '-help': {
doc: 'print this message and exit.', doc: 'print this message and exit.',
handler: function(){ handler: function(){
var spec = this.spec var spec = this.spec
var that = this var that = this
var x
console.log([ console.log([
`Usage: ${ `Usage: ${
typeof(spec.__usage__) == 'function' ? typeof(spec.__usage__) == 'function' ?
@ -180,7 +181,11 @@ var ArgvParser = function(spec){
'Options:', 'Options:',
...(spec.__getoptions__() ...(spec.__getoptions__()
.map(function([opts, arg, doc]){ .map(function([opts, arg, doc]){
return ['-'+opts.join(' | -') +' '+ (arg || ''), doc] })), return [opts.join(' | -') +' '+ (arg || ''), doc] })),
// commands...
...(((x = spec.__getcommands__()) && x.length > 0) ?
['', 'Commands:', ...x]
: []),
// examples... // examples...
...(this.spec.__examples__ ? ...(this.spec.__examples__ ?
['', 'Examples:', ...( ['', 'Examples:', ...(
@ -188,12 +193,12 @@ var ArgvParser = function(spec){
spec.__examples__ spec.__examples__
.map(function(e){ .map(function(e){
return e instanceof Array ? e : [e] }) return e instanceof Array ? e : [e] })
: spec.__examples__.call(this) )] : spec.__examples__(this) )]
: []), : []),
// footer... // footer...
...(this.spec.__footer__? ...(this.spec.__footer__?
['', typeof(this.spec.__footer__) == 'function' ? ['', typeof(this.spec.__footer__) == 'function' ?
spec.__footer__.call(this) spec.__footer__(this)
: spec.__footer__] : spec.__footer__]
: []) ] : []) ]
.map(function(e){ .map(function(e){
@ -212,6 +217,7 @@ var ArgvParser = function(spec){
process.exit() }}, process.exit() }},
// special values and methods... // special values and methods...
__opt_pattern__: /^--?/,
__opts_width__: 3, __opts_width__: 3,
__doc_prefix__: '- ', __doc_prefix__: '- ',
@ -249,7 +255,12 @@ var ArgvParser = function(spec){
handlers[k][0].push(opt) handlers[k][0].push(opt)
: (handlers[k] = [[opt], h.arg, h.doc || k, h]) }) : (handlers[k] = [[opt], h.arg, h.doc || k, h]) })
return Object.values(handlers) }, return Object.values(handlers) },
// XXX get all instances of ArgvParser in spec...
__getcommands__: function(){
return []
},
__gethandler__: function(key){ __gethandler__: function(key){
key = key.replace(this.__opt_pattern__ || /^--?/, '-')
var seen = new Set([key]) var seen = new Set([key])
while(key in this while(key in this
&& typeof(this[key]) == typeof('str')){ && typeof(this[key]) == typeof('str')){
@ -282,7 +293,7 @@ var ArgvParser = function(spec){
var arg = argv.shift() var arg = argv.shift()
// options... // options...
if(pattern.test(arg)){ if(pattern.test(arg)){
var handler = spec.__gethandler__(arg.replace(/^--?/, '')).pop() var handler = spec.__gethandler__(arg).pop()
|| spec.__unknown__ || spec.__unknown__
// get option value... // get option value...
var value = (handler.arg && !pattern.test(argv[0])) ? var value = (handler.arg && !pattern.test(argv[0])) ?
@ -1044,8 +1055,8 @@ if(typeof(__filename) != 'undefined'
'set verbose mode globally and run tests.'.gray], 'set verbose mode globally and run tests.'.gray],
], ],
// options... // options...
l: 'list', '-l': '-list',
list: { '-list': {
doc: 'list available tests.', doc: 'list available tests.',
handler: function(){ handler: function(){
console.log(object.normalizeTextIndent( console.log(object.normalizeTextIndent(
@ -1072,8 +1083,8 @@ if(typeof(__filename) != 'undefined'
') } ') }
`), this.scriptname) `), this.scriptname)
process.exit() }}, process.exit() }},
v: 'verbose', '-v': '-verbose',
verbose: { '-verbose': {
doc: 'verbose mode (defaults to: $VERBOSE).', doc: 'verbose mode (defaults to: $VERBOSE).',
handler: function(){ handler: function(){
module.VERBOSE = true }}, module.VERBOSE = true }},