several minor fixes...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-06-29 02:51:06 +03:00
parent 2a564e1280
commit 5ca866263a
3 changed files with 24 additions and 13 deletions

33
argv.js
View File

@ -349,6 +349,16 @@ object.Constructor('Parser', {
// post parsing callbacks... // post parsing callbacks...
// //
// .then(callback(unhandleed))
//
// .stop(callback(arg))
//
// .error(callback(arg))
//
//
// XXX need to document the arguments to each handler/callback...
// XXX .then(..) passes the full list of unhandleed args including
// argv[0] and argv[1]...
then: afterCallback('parsing'), then: afterCallback('parsing'),
stop: afterCallback('stop'), stop: afterCallback('stop'),
error: afterCallback('error'), error: afterCallback('error'),
@ -366,15 +376,17 @@ object.Constructor('Parser', {
var nested = false var nested = false
// default argv... // default argv...
argv = argv == null ? argv = (argv == null ?
process.argv.slice() process.argv
: argv : argv)
// XXX ARGV: strip out the interpreter if it is given... .slice()
var rest = this.rest = argv.slice()
// XXX ARGV: strip out the interpreter if it is given... (???)
// nested command handler... // nested command handler...
// XXX the condition is a bit too strong... // XXX the condition is a bit too strong...
if(context instanceof Parser){ if(context instanceof Parser){
var rest = this.rest = argv.slice()
this.script = this.scriptName = this.script = this.scriptName =
context.scriptName +' '+ arguments[2] context.scriptName +' '+ arguments[2]
this.argv = [context.scriptName, this.scriptName, ...argv] this.argv = [context.scriptName, this.scriptName, ...argv]
@ -382,7 +394,6 @@ object.Constructor('Parser', {
// root parser... // root parser...
} else { } else {
var rest = this.rest = argv.slice()
this.argv = argv.slice() this.argv = argv.slice()
// XXX ARGV: revise this... // XXX ARGV: revise this...
// - when run from node -- [<node>, <script>, ...] // - when run from node -- [<node>, <script>, ...]
@ -396,8 +407,8 @@ object.Constructor('Parser', {
var opt_pattern = this.optionInputPattern var opt_pattern = this.optionInputPattern
var unhandled = [] var unhandled = []
while(argv.length > 0){ while(rest.length > 0){
var [arg, value] = argv.shift().split(/=/) var [arg, value] = rest.shift().split(/=/)
var type = opt_pattern.test(arg) ? var type = opt_pattern.test(arg) ?
'opt' 'opt'
: this.isCommand(arg) ? : this.isCommand(arg) ?
@ -410,8 +421,8 @@ object.Constructor('Parser', {
|| this.handleArgument || this.handleArgument
// get option value... // get option value...
value = value value = value
|| ((handler.arg && !opt_pattern.test(argv[0])) ? || ((handler.arg && !opt_pattern.test(rest[0])) ?
argv.shift() rest.shift()
: undefined) : undefined)
// value conversion... // value conversion...
value = value && this.handleArgumentValue ? value = value && this.handleArgumentValue ?
@ -422,7 +433,7 @@ object.Constructor('Parser', {
handler handler
: handler.handler) : handler.handler)
.call(this, .call(this,
argv, rest,
arg, arg,
...(value ? [value] : [])) ...(value ? [value] : []))
// handle .STOP / .ERROR // handle .STOP / .ERROR

View File

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

View File

@ -67,7 +67,7 @@ argv.Parser({
'-c': '-a', '-c': '-a',
}) })
.then(function(){ .then(function(){
console.log('DONE') }) console.log('DONE', ...arguments) })
.stop(function(){ .stop(function(){
console.log('STOP') }) console.log('STOP') })
.error(function(){ .error(function(){