refactoring and simplification...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-06-29 14:05:55 +03:00
parent 0385d36f39
commit cd3da11274
3 changed files with 36 additions and 32 deletions

56
argv.js
View File

@ -97,6 +97,9 @@ var afterCallbackCall = function(name, context, ...args){
// } // }
// //
// //
// XXX --help should work for any command and not just for the nested
// parser commands...
// ...not sure how to implement this...
// XXX should .options(..), .commands(..) and .handler(..) be: // XXX should .options(..), .commands(..) and .handler(..) be:
// .getOptions(..), .getCommands(..) and .getHandler(..) respectively??? // .getOptions(..), .getCommands(..) and .getHandler(..) respectively???
// XXX should we handle <scriptName>-<command> script calls??? // XXX should we handle <scriptName>-<command> script calls???
@ -119,6 +122,7 @@ object.Constructor('Parser', {
// instance stuff... // instance stuff...
// XXX revise... // XXX revise...
argv: null, argv: null,
pre_argv: null,
rest: null, rest: null,
scriptNmae: null, scriptNmae: null,
scriptPath: null, scriptPath: null,
@ -360,7 +364,7 @@ object.Constructor('Parser', {
// post parsing callbacks... // post parsing callbacks...
// //
// .then(callback(unhandleed)) // .then(callback(unhandleed, root_value))
// //
// .stop(callback(arg)) // .stop(callback(arg))
// //
@ -372,44 +376,38 @@ object.Constructor('Parser', {
// //
// parser(argv) // parser(argv)
// -> unprocessed // -> parser
//
// parser(argv, main)
// -> parser
// //
// NOTE: this (i.e. parser) can be used as a nested command/option // NOTE: this (i.e. parser) can be used as a nested command/option
// handler... // handler...
// __call__: function(context, argv, main, root_value){
// XXX ARGV: need to normalize argv -- strip out the interpreter if it is given...
__call__: function(context, argv){
var that = this var that = this
var nested = false var nested = false
var rest = this.rest =
// default argv... argv == null ?
argv = (argv == null ?
process.argv process.argv
: argv) : argv
.slice() argv = rest.slice()
var rest = this.rest = argv.slice() main = main
|| require.main.filename
// XXX ARGV: strip out the interpreter if it is given... (???)
// nested command handler... // nested command handler...
// XXX the condition is a bit too strong...
if(context instanceof Parser){ if(context instanceof Parser){
this.script = this.scriptName =
context.scriptName +' '+ arguments[2]
this.argv = [context.scriptName, this.scriptName, ...argv]
nested = true nested = true
main = context.scriptName +' '+ main
rest.unshift(main) }
// normalize the argv...
if(main != null){
this.pre_argv = rest.splice(0, rest.indexOf(main))
rest.includes(main)
|| rest.unshift(main) }
// root parser...
} else {
this.argv = argv.slice()
// XXX ARGV: revise this...
// - when run from node -- [<node>, <script>, ...]
// - when run from electron -- [<electron>, ...]
// require('electron').remove.process.argv
this.interpreter = rest.shift()
this.script = rest[0] this.script = rest[0]
this.scriptName = rest.shift().split(/[\\\/]/).pop() this.scriptName = rest.shift().split(/[\\\/]/).pop()
}
var opt_pattern = this.optionInputPattern var opt_pattern = this.optionInputPattern
@ -459,7 +457,11 @@ object.Constructor('Parser', {
unhandled.push(arg) } unhandled.push(arg) }
// post handlers... // post handlers...
afterCallbackCall('parsing', this, unhandled) root_value = root_value && this.handleArgumentValue ?
this.handleArgumentValue(this, root_value)
: root_value
afterCallbackCall('parsing', this, unhandled, root_value)
return this }, return this },
// NOTE: see general doc... // NOTE: see general doc...

View File

@ -1,6 +1,6 @@
{ {
"name": "ig-argv", "name": "ig-argv",
"version": "2.0.1", "version": "2.0.2",
"description": "simple argv parser", "description": "simple argv parser",
"main": "argv.js", "main": "argv.js",
"scripts": { "scripts": {

View File

@ -48,8 +48,10 @@ argv.Parser({
'@nested': argv.Parser({ '@nested': argv.Parser({
doc: 'nested nested parser.', doc: 'nested nested parser.',
}), }).then(function(){
}), console.log('NESTED NESTED DONE', ...arguments)}),
}).then(function(){
console.log('NESTED DONE', ...arguments) }),
// these aliases will not get shown... // these aliases will not get shown...