fixes and tweaks...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-07-06 00:28:28 +03:00
parent 210efdaf58
commit 9a5cf82acd
4 changed files with 29 additions and 32 deletions

View File

@ -31,7 +31,6 @@ This code is an evolution of that parser.
- `-help` &ndash; generate and print help,
- `-version` &ndash; print version,
- `-` &ndash; 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)

45
argv.js
View File

@ -159,8 +159,6 @@ var afterCallback = function(name){
//
//
//
// XXX unify handler.arg and handler.key...
// ...syntax: "<arg>" | "<arg>|<key>"
// 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...

View File

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

View File

@ -46,6 +46,12 @@ argv.Parser({
required: true,
},
'-value': {
doc: 'Value option',
arg: 'VALUE | valueValue',
default: 333,
},
'-test': argv.Parser({
env: 'TEST',
arg: 'TEST',