From 9a5cf82acddecf3bc3ee4acce0166eae5dd18630 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Mon, 6 Jul 2020 00:28:28 +0300 Subject: [PATCH] fixes and tweaks... Signed-off-by: Alex A. Naanou --- README.md | 8 +++----- argv.js | 45 +++++++++++++++++++-------------------------- package.json | 2 +- test.js | 6 ++++++ 4 files changed, 29 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index c63eac0..803cd95 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,6 @@ This code is an evolution of that parser. - `-help` – generate and print help, - `-version` – print version, - `-` – stop argument processing, - - common option aliases - Extensible: - Hooks for option value conversion _(XXX should this be implemented???)_ - Hooks for dynamic option/command handling @@ -114,11 +113,10 @@ var parser = argv.Parser({ '-full': { doc: 'Option help', // option value to be displayed in help (optional) - arg: 'VALUE', + // NOTE: "attr" is used as a key to set the value if .handler + // was not defined and is ingored in all other cases... + arg: 'VALUE | attr', - // value key (optional) - // NOTE: if .handler(..) is defined this is ignored. - key: 'fullValue', // envioroment value (optional) env: 'VALUE', // default value (optional) diff --git a/argv.js b/argv.js index 6c7d75d..96074b0 100644 --- a/argv.js +++ b/argv.js @@ -159,8 +159,6 @@ var afterCallback = function(name){ // // // -// XXX unify handler.arg and handler.key... -// ...syntax: "" | "|" // XXX add -about flag??? // XXX we should be able to set .scriptName by hand... // XXX might be a good idea to read metadata from package.json @@ -195,6 +193,7 @@ object.Constructor('Parser', { // output... + // // XXX is this the right way to go??? print: function(...args){ console.log(...args) @@ -236,8 +235,8 @@ object.Constructor('Parser', { [opt], h.arg && h.arg - .split(/|/) - .pop() + .split(/\|/) + .shift() .trim(), h.doc || k.slice(1), h ])) }) @@ -297,8 +296,6 @@ object.Constructor('Parser', { [] : ['dead-end'])] }, - // XXX need to test option definitions... (???) - // i.e. report loops and dead ends... // Builtin options/commands and their configuration... @@ -322,7 +319,7 @@ object.Constructor('Parser', { // XXX test this with string value... examples: undefined, // XXX add license and version info... - //footer: '$SCRIPTNAME v:$VERSION', + //footer: '$SCRIPTNAME ($VERSION) by $AUTHOR', footer: undefined, // XXX should wrap long lines... @@ -475,28 +472,23 @@ object.Constructor('Parser', { return module.THEN }, }, - // common short-hands... - // - // NOTE: defining this as a loop will enable the user to define any - // of the aliases as the handler and thus breaking the loop... - // NOTE: unless the loop is broken this set of options is not usable. - //'-v': '-verbose', - //'-verbose': '-v', - - // Default handler action... // // This is called when .handler is not set... + // handlerDefault: function(handler, rest, key, value){ key = (handler.arg && handler.arg - .split(/|/) + .split(/\|/) .pop() .trim()) // get the final key... || this.handler(key)[0].slice(1) - this[key] = value === undefined ? - true + this[key] = + arguments.length < 4 ? + true + : value === undefined ? + handler.default || true : value return this }, @@ -598,7 +590,6 @@ object.Constructor('Parser', { // all the parse data... // NOTE: this (i.e. parser) can be used as a nested command/option // handler... - // __call__: function(context, argv, main, root_value){ var parsed = Object.create(this) var nested = parsed.nested = false @@ -636,9 +627,11 @@ object.Constructor('Parser', { && parsed.handleErrorExit(arg, reason) } var defaultHandler = function(handler){ return function(rest, arg, value) { - return parsed.handlerDefault(handler, rest, arg, value) } } + return parsed.handlerDefault(handler, ...arguments) } } var runHandler = function(handler, arg, rest){ - var [arg, value] = arg.split(/=/) + var [arg, value] = arg instanceof Array ? + arg + : arg.split(/=/) // get option value... value = value == null ? ((handler.arg && !opt_pattern.test(rest[0])) ? @@ -693,6 +686,7 @@ object.Constructor('Parser', { return handler && [a, handler] } + // parse the arguments and call handlers... var values = new Set() var seen = new Set() var unhandled = [] @@ -748,7 +742,9 @@ object.Constructor('Parser', { && handler.env in process.env) || handler.default) && seen.add(handler) - && runHandler(handler, a || k[0], rest)) }) + && runHandler(handler, + [k[0], handler.default], + rest)) }) // check required options... var missing = parsed @@ -767,9 +763,6 @@ object.Constructor('Parser', { parsed.handleArgumentValue(parsed, root_value) : root_value parsed.then(unhandled, root_value, rest) - // XXX should we detach parsed from this??? - // i.e. set: - // parsed.__proto__ = {}.__proto__ return parsed }, // NOTE: see general doc... diff --git a/package.json b/package.json index 8246144..bb8f08a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-argv", - "version": "2.1.0", + "version": "2.1.1", "description": "simple argv parser", "main": "argv.js", "scripts": { diff --git a/test.js b/test.js index 6fbb716..513ed72 100644 --- a/test.js +++ b/test.js @@ -46,6 +46,12 @@ argv.Parser({ required: true, }, + '-value': { + doc: 'Value option', + arg: 'VALUE | valueValue', + default: 333, + }, + '-test': argv.Parser({ env: 'TEST', arg: 'TEST',