refactoring...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-06-03 04:11:49 +03:00
parent 86cade01eb
commit 27d03a53f8

78
test.js
View File

@ -94,6 +94,7 @@ var deepKeys = function(obj, stop){
// ... // ...
// } // }
// //
// XXX add option groups -- nested specs...
// XXX do we handle = for options with values??? // XXX do we handle = for options with values???
// XXX move this to it's own lib... // XXX move this to it's own lib...
// XXX need better test processing: // XXX need better test processing:
@ -106,36 +107,78 @@ var ArgvParser = function(spec){
spec = Object.assign({ spec = Object.assign({
// builtin options... // builtin options...
h: 'help', h: 'help',
// XXX revise...
help: { help: {
doc: 'print this message and exit.', doc: 'print this message and exit.',
handler: function(){ handler: function(){
var opts_width = this.spec.__opts_width__ || 4 var spec = this.spec
var that = this
console.log([ console.log([
`Usage: ${ `Usage: ${
typeof(spec.__usage__) == 'function' ? typeof(spec.__usage__) == 'function' ?
spec.__usage__.call(this) spec.__usage__.call(this)
: spec.__usage__ }`, : spec.__usage__ }`,
// doc...
...(spec.__doc__ ?
['', typeof(spec.__doc__) == 'function' ?
spec.__doc__()
: spec.__doc__]
: []),
// options...
'', '',
'Options:', 'Options:',
'\t'+ (spec.__getoptions__() ...(spec.__getoptions__()
.map(function([opts, arg, doc]){ .map(function([opts, arg, doc]){
opts = '-'+ opts.join(' | -') +' '+ (arg || '') return ['-'+opts.join(' | -') +' '+ (arg || ''), doc] })),
// format options and docs... // examples...
return opts.length < opts_width*8 ? ...(this.spec.__examples__ ?
opts +'\t'.repeat(opts_width - Math.floor(opts.length/8))+ doc ['', 'Examples:', ...(
: [opts, '\t'.repeat(opts_width)+ doc] }) this.spec.__examples__ instanceof Array ?
.flat() spec.__examples__
.join('\n\t')), .map(function(e){
// XXX links/license/... return e instanceof Array ? e : [e] })
].join('\n')) : spec.__examples__.call(this) )]
: []),
// footer...
...(this.spec.__footer__?
['', typeof(this.spec.__footer__) == 'function' ?
spec.__footer__.call(this)
: spec.__footer__]
: []) ]
.map(function(e){
return e instanceof Array ?
spec.__align__(...e
.map(function(s){
return s.replace(/\$scriptname/g, that.scriptname) }))
// indent lists...
.map(function(s){
return '\t'+ s })
: e })
.flat()
.join('\n')
.replace(/\$scriptname/g, this.scriptname))
process.exit() }}, process.exit() }},
// special values and methods... // special values and methods...
__opts_width__: 3, __opts_width__: 3,
__doc_prefix__: '- ',
// these is run in the same context as the handlers... (XXX ???) // these is run in the same context as the handlers... (XXX ???)
__align__: function(a, b){
var opts_width = this.__opts_width__ || 4
var prefix = this.__doc_prefix__ || ''
return b ?
(a.length < opts_width*8 ?
[a +'\t'.repeat(opts_width - Math.floor(a.length/8))+ prefix + b]
: [a, '\t'.repeat(opts_width)+ prefix + b])
: [a] },
__usage__: function(){ __usage__: function(){
return `${ this.scriptname } [OPTIONS]` }, return `${ this.scriptname } [OPTIONS]` },
__examples__: undefined,
__footer__: undefined,
__unknown__: function(key){ __unknown__: function(key){
console.error('Unknown option:', key) console.error('Unknown option:', key)
process.exit(1) }, process.exit(1) },
@ -596,8 +639,15 @@ if(typeof(__filename) != 'undefined'
// parse args... // parse args...
var chains = var chains =
ArgvParser({ ArgvParser({
__usage__: function(){ // doc...
return `Usage: ${ this.scriptname } [OPTIONS] [CHAIN] ...` }, __usage__: `$scriptname [OPTIONS] [CHAIN] ...`,
__doc__: 'Run tests on object.js module.',
__examples__: [
['$ $scriptname', 'run all tests.'],
['$ $scriptname basic:*:*', 'run all tests and modifiers on "basic" setup.'],
['$ $scriptname -v example', 'run "example" test in verbose mode.'],
['$ $scriptname native:gen3:methods init:gen3:methods', 'run two tests/patterns.'],
],
// options... // options...
l: 'list', l: 'list',
list: { list: {
@ -623,7 +673,7 @@ if(typeof(__filename) != 'undefined'
process.exit() }}, process.exit() }},
v: 'verbose', v: 'verbose',
verbose: { verbose: {
doc: 'verbose mode.', doc: 'verbose mode (defaults to: $VERBOSE).',
handler: function(){ handler: function(){
module.VERBOSE = true }}, module.VERBOSE = true }},
})(process.argv) })(process.argv)