fixes and cleanup...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-07-06 22:04:47 +03:00
parent e4a06832c5
commit 5c9a7be4a1
3 changed files with 17 additions and 15 deletions

28
argv.js
View File

@ -161,6 +161,7 @@ var afterCallback = function(name){
// XXX can we implement the whole thing directly as a stack language??? // XXX can we implement the whole thing directly as a stack language???
// //
// XXX can we add more prefexes, like '+' and the like??? // XXX can we add more prefexes, like '+' and the like???
// ...add prefix handlers???
// XXX might be a good idea to read metadata from package.json // XXX might be a good idea to read metadata from package.json
// XXX should -help should work for any command? // XXX should -help should work for any command?
// ...now only works for nested parsers... // ...now only works for nested parsers...
@ -186,11 +187,10 @@ object.Constructor('Parser', {
// NOTE: we only care about differentiating an option from a command // NOTE: we only care about differentiating an option from a command
// here by design... // here by design...
optionInputPattern: /^--?(.*)$/, optionInputPattern: /^--?(.*)$/,
commandInputPattern: /^([a-zA-Z].*)$/, commandInputPattern: /^([a-zA-Z*].*)$/,
// instance stuff... // instance stuff...
argv: null, argv: null,
preArgv: null,
rest: null, rest: null,
rootValue: null, rootValue: null,
@ -284,8 +284,6 @@ object.Constructor('Parser', {
key = this.optionInputPattern.test(key) ? key = this.optionInputPattern.test(key) ?
key.replace(this.optionInputPattern, this.optionPrefix+'$1') key.replace(this.optionInputPattern, this.optionPrefix+'$1')
: key.replace(this.commandInputPattern, this.commandPrefix+'$1') : key.replace(this.commandInputPattern, this.commandPrefix+'$1')
// quote '*'...
key = key.replace(/^(.)\*$/, '$1\\*')
var seen = new Set() var seen = new Set()
while(key in this while(key in this
&& typeof(this[key]) == typeof('str')){ && typeof(this[key]) == typeof('str')){
@ -520,9 +518,11 @@ object.Constructor('Parser', {
.trim()) .trim())
// get the final key... // get the final key...
|| this.handler(key)[0].slice(1) || this.handler(key)[0].slice(1)
// if value not given set true... // if value not given set true and handle...
this[key] = arguments.length < 4 ? this[key] = arguments.length < 4 ?
this.handleArgumentValue(handler, true) (this.handleArgumentValue ?
this.handleArgumentValue(handler, true)
: true)
: value : value
return this }, return this },
@ -607,7 +607,7 @@ object.Constructor('Parser', {
rest.unshift(main) } rest.unshift(main) }
// normalize the argv... // normalize the argv...
if(main != null){ if(main != null){
parsed.preArgv = rest.splice(0, rest.indexOf(main)) rest.splice(0, rest.indexOf(main))
rest.includes(main) rest.includes(main)
|| rest.unshift(main) } || rest.unshift(main) }
// script stuff... // script stuff...
@ -673,7 +673,7 @@ object.Constructor('Parser', {
// returns both the new first arg and the handler... // returns both the new first arg and the handler...
var splitArgs = function(arg, rest){ var splitArgs = function(arg, rest){
var [arg, value] = arg.split(/=/) var [arg, value] = arg.split(/=/)
// skip single letter unknown options or '--' options... // skip single letter unknown or '--' options...
if(arg.length <= 2 if(arg.length <= 2
|| arg.startsWith(parsed.optionPrefix.repeat(2))){ || arg.startsWith(parsed.optionPrefix.repeat(2))){
return undefined } return undefined }
@ -693,7 +693,7 @@ object.Constructor('Parser', {
// parse the arguments and call handlers... // parse the arguments and call handlers...
var values = new Set() var values = new Set()
var seen = new Set() var seen = new Set()
var unhandled = [] var unhandled = parsed.unhandled = []
while(rest.length > 0){ while(rest.length > 0){
var arg = rest.shift() var arg = rest.shift()
// NOTE: opts and commands do not follow the same path here // NOTE: opts and commands do not follow the same path here
@ -706,6 +706,8 @@ object.Constructor('Parser', {
: 'unhandled' : 'unhandled'
// options / commands... // options / commands...
if(type != 'unhandled'){ if(type != 'unhandled'){
// quote '-*' / '@*'...
arg = arg.replace(/^(.)\*$/, '$1\\*')
// get handler... // get handler...
var handler = parsed.handler(arg)[1] var handler = parsed.handler(arg)[1]
// handle merged options // handle merged options
@ -714,8 +716,8 @@ object.Constructor('Parser', {
&& parsed.splitOptions && parsed.splitOptions
&& splitArgs(arg, rest)) && splitArgs(arg, rest))
// dynamic or error... // dynamic or error...
|| parsed[type == 'opt' ? '-*' : '@*'] || parsed.handler(type == 'opt' ? '-*' : '@*')[1]
// in case no handler found and '-*' / '@*' not defined... // no handler found and '-*' or '@*' not defined...
if(handler == null){ if(handler == null){
handleError('unknown', arg, rest) handleError('unknown', arg, rest)
parsed.printError('unknown '+(type == 'opt' ? 'option:' : 'command:'), arg) parsed.printError('unknown '+(type == 'opt' ? 'option:' : 'command:'), arg)
@ -739,12 +741,10 @@ object.Constructor('Parser', {
: parsed } : parsed }
// finish arg processing now... // finish arg processing now...
if(res === module.THEN){ if(res === module.THEN){
arg = null
break } break }
continue } continue }
// unhandled... // unhandled...
arg unhandled.push(arg) }
&& unhandled.push(arg) }
// call value handlers with .env or .default values that were // call value handlers with .env or .default values that were
// not explicitly called yet... // not explicitly called yet...
parsed.optionsWithValue() parsed.optionsWithValue()

View File

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

View File

@ -80,6 +80,8 @@ argv.Parser({
handler: function(){ handler: function(){
console.log('-\\*:', ...arguments) } }, console.log('-\\*:', ...arguments) } },
//'@*': undefined,
// these aliases will not get shown... // these aliases will not get shown...
// dead-end alias... // dead-end alias...