refactoring...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-06-25 18:31:06 +03:00
parent f270f24a82
commit 51d0aae474
2 changed files with 43 additions and 27 deletions

67
argv.js
View File

@ -314,6 +314,7 @@ object.Constructor('Parser', {
// ... // ...
// ] // ]
// //
// XXX add option groups...
// XXX should these be .getOptions(..) / .getCommands(..) ??? // XXX should these be .getOptions(..) / .getCommands(..) ???
options: function(...prefix){ options: function(...prefix){
var that = this var that = this
@ -416,10 +417,16 @@ object.Constructor('Parser', {
// doc (optional)... // doc (optional)...
...getValue('doc'), ...getValue('doc'),
// options... // options...
// XXX add option groups...
...section('Options', ...section('Options',
this.options() this.options()
.map(function([opts, arg, doc]){ .map(function([opts, arg, doc]){
return [ opts.join(' | -') +' '+ (arg || ''), doc] })), return [ opts.join(' | -') +' '+ (arg || ''), doc] })),
// dynamic options...
...section('Dynamic options',
this.handleArgument ?
this.handleArgument('doc') || []
: []),
// commands (optional)... // commands (optional)...
...section('Commands', ...section('Commands',
this.commands() this.commands()
@ -455,26 +462,44 @@ object.Constructor('Parser', {
'-v': '-verbose', '-v': '-verbose',
unknownArgument: function(_, key){ //
// Handle dynamic/unknown argument...
// .handleArgument(args, arg)
// -> module.ERROR
// -> module.STOP
// -> result
//
// Get dynamic argument doc...
// .handleArgument('doc')
// -> undefined
// -> doc
//
//
// doc format:
// [
// [<option-spec>, <doc>],
// ...
// ]
//
//
// NOTE: this is mainly needed to handle dynamic arguments...
handleArgument: function(_, key){
if(arguments.length == 1 && arguments[0] == 'doc'){
return undefined }
console.error('Unknown '+ (key.startsWith('-') ? 'option:' : 'command:'), key) console.error('Unknown '+ (key.startsWith('-') ? 'option:' : 'command:'), key)
return module.ERROR }, return module.ERROR },
// NOTE: if this is set to false Parser will not call process.exit(..)
handleErrorExit: function(arg){
process.exit(1) },
// post parsing callbacks... // post parsing callbacks...
// XXX add defaults...
// XXX add ability to clear defaults...
then: afterCallback('parsing'), then: afterCallback('parsing'),
stop: afterCallback('stop'), stop: afterCallback('stop'),
error: afterCallback('error'), error: afterCallback('error'),
// NOTE: this (i.e. parser) can be used as a nested command/option
// XXX need to unify this with handler as much as possible to make // handler...
// parsers nestable....
// there are differences that can't be avoided:
// - argv/rest -- argv includes 2 extra args:
// - interpreter
// - script
// ...these should be either avoided or "inherited"
__call__: function(context, argv){ __call__: function(context, argv){
var that = this var that = this
var nested = false var nested = false
@ -522,7 +547,7 @@ object.Constructor('Parser', {
if(type != 'unhandled'){ if(type != 'unhandled'){
// get handler... // get handler...
var handler = this.getHandler(arg).pop() var handler = this.getHandler(arg).pop()
|| this.unknownArgument || this.handleArgument
// get option value... // get option value...
var value = (handler.arg && !opt_pattern.test(argv[0])) ? var value = (handler.arg && !opt_pattern.test(argv[0])) ?
argv.shift() argv.shift()
@ -540,6 +565,9 @@ object.Constructor('Parser', {
afterCallbackCall( afterCallbackCall(
res === module.STOP ? 'stop' : 'error', res === module.STOP ? 'stop' : 'error',
this, arg) this, arg)
res === module.ERROR
&& this.handleErrorExit
&& this.handleErrorExit(arg)
return nested ? return nested ?
res res
: this } : this }
@ -560,21 +588,6 @@ object.Constructor('Parser', {
}) })
// defaults...
// XXX do we do execution order???
// ...if these will do process.exit(..) then they need to be last...
Parser.prototype
.then(function(){
// XXX
})
.stop(function(){
// XXX
})
.error(function(){
// XXX
})
/********************************************************************** /**********************************************************************

View File

@ -20,6 +20,9 @@ var argv = require('./argv')
var p = var p =
module.p = module.p =
argv.Parser({ argv.Parser({
// disable exit on error...
handleErrorExit: false,
'@help': '-help', '@help': '-help',
'-v': '-verbose', '-v': '-verbose',