mirror of
https://github.com/flynx/argv.js.git
synced 2025-10-29 10:50:06 +00:00
fixes and tweaks...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
210efdaf58
commit
9a5cf82acd
@ -31,7 +31,6 @@ This code is an evolution of that parser.
|
|||||||
- `-help` – generate and print help,
|
- `-help` – generate and print help,
|
||||||
- `-version` – print version,
|
- `-version` – print version,
|
||||||
- `-` – stop argument processing,
|
- `-` – stop argument processing,
|
||||||
- common option aliases
|
|
||||||
- Extensible:
|
- Extensible:
|
||||||
- Hooks for option value conversion _(XXX should this be implemented???)_
|
- Hooks for option value conversion _(XXX should this be implemented???)_
|
||||||
- Hooks for dynamic option/command handling
|
- Hooks for dynamic option/command handling
|
||||||
@ -114,11 +113,10 @@ var parser = argv.Parser({
|
|||||||
'-full': {
|
'-full': {
|
||||||
doc: 'Option help',
|
doc: 'Option help',
|
||||||
// option value to be displayed in help (optional)
|
// 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)
|
// envioroment value (optional)
|
||||||
env: 'VALUE',
|
env: 'VALUE',
|
||||||
// default value (optional)
|
// default value (optional)
|
||||||
|
|||||||
45
argv.js
45
argv.js
@ -159,8 +159,6 @@ var afterCallback = function(name){
|
|||||||
//
|
//
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// XXX unify handler.arg and handler.key...
|
|
||||||
// ...syntax: "<arg>" | "<arg>|<key>"
|
|
||||||
// XXX add -about flag???
|
// XXX add -about flag???
|
||||||
// XXX we should be able to set .scriptName by hand...
|
// XXX we should be able to set .scriptName by hand...
|
||||||
// XXX might be a good idea to read metadata from package.json
|
// XXX might be a good idea to read metadata from package.json
|
||||||
@ -195,6 +193,7 @@ object.Constructor('Parser', {
|
|||||||
|
|
||||||
|
|
||||||
// output...
|
// output...
|
||||||
|
//
|
||||||
// XXX is this the right way to go???
|
// XXX is this the right way to go???
|
||||||
print: function(...args){
|
print: function(...args){
|
||||||
console.log(...args)
|
console.log(...args)
|
||||||
@ -236,8 +235,8 @@ object.Constructor('Parser', {
|
|||||||
[opt],
|
[opt],
|
||||||
h.arg
|
h.arg
|
||||||
&& h.arg
|
&& h.arg
|
||||||
.split(/|/)
|
.split(/\|/)
|
||||||
.pop()
|
.shift()
|
||||||
.trim(),
|
.trim(),
|
||||||
h.doc || k.slice(1),
|
h.doc || k.slice(1),
|
||||||
h ])) })
|
h ])) })
|
||||||
@ -297,8 +296,6 @@ object.Constructor('Parser', {
|
|||||||
[]
|
[]
|
||||||
: ['dead-end'])] },
|
: ['dead-end'])] },
|
||||||
|
|
||||||
// XXX need to test option definitions... (???)
|
|
||||||
// i.e. report loops and dead ends...
|
|
||||||
|
|
||||||
// Builtin options/commands and their configuration...
|
// Builtin options/commands and their configuration...
|
||||||
|
|
||||||
@ -322,7 +319,7 @@ object.Constructor('Parser', {
|
|||||||
// XXX test this with string value...
|
// XXX test this with string value...
|
||||||
examples: undefined,
|
examples: undefined,
|
||||||
// XXX add license and version info...
|
// XXX add license and version info...
|
||||||
//footer: '$SCRIPTNAME v:$VERSION',
|
//footer: '$SCRIPTNAME ($VERSION) by $AUTHOR',
|
||||||
footer: undefined,
|
footer: undefined,
|
||||||
|
|
||||||
// XXX should wrap long lines...
|
// XXX should wrap long lines...
|
||||||
@ -475,28 +472,23 @@ object.Constructor('Parser', {
|
|||||||
return module.THEN }, },
|
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...
|
// Default handler action...
|
||||||
//
|
//
|
||||||
// This is called when .handler is not set...
|
// This is called when .handler is not set...
|
||||||
|
//
|
||||||
handlerDefault: function(handler, rest, key, value){
|
handlerDefault: function(handler, rest, key, value){
|
||||||
key = (handler.arg
|
key = (handler.arg
|
||||||
&& handler.arg
|
&& handler.arg
|
||||||
.split(/|/)
|
.split(/\|/)
|
||||||
.pop()
|
.pop()
|
||||||
.trim())
|
.trim())
|
||||||
// get the final key...
|
// get the final key...
|
||||||
|| this.handler(key)[0].slice(1)
|
|| this.handler(key)[0].slice(1)
|
||||||
this[key] = value === undefined ?
|
this[key] =
|
||||||
true
|
arguments.length < 4 ?
|
||||||
|
true
|
||||||
|
: value === undefined ?
|
||||||
|
handler.default || true
|
||||||
: value
|
: value
|
||||||
return this },
|
return this },
|
||||||
|
|
||||||
@ -598,7 +590,6 @@ object.Constructor('Parser', {
|
|||||||
// all the parse data...
|
// all the parse data...
|
||||||
// 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){
|
__call__: function(context, argv, main, root_value){
|
||||||
var parsed = Object.create(this)
|
var parsed = Object.create(this)
|
||||||
var nested = parsed.nested = false
|
var nested = parsed.nested = false
|
||||||
@ -636,9 +627,11 @@ object.Constructor('Parser', {
|
|||||||
&& parsed.handleErrorExit(arg, reason) }
|
&& parsed.handleErrorExit(arg, reason) }
|
||||||
var defaultHandler = function(handler){
|
var defaultHandler = function(handler){
|
||||||
return function(rest, arg, value) {
|
return function(rest, arg, value) {
|
||||||
return parsed.handlerDefault(handler, rest, arg, value) } }
|
return parsed.handlerDefault(handler, ...arguments) } }
|
||||||
var runHandler = function(handler, arg, rest){
|
var runHandler = function(handler, arg, rest){
|
||||||
var [arg, value] = arg.split(/=/)
|
var [arg, value] = arg instanceof Array ?
|
||||||
|
arg
|
||||||
|
: arg.split(/=/)
|
||||||
// get option value...
|
// get option value...
|
||||||
value = value == null ?
|
value = value == null ?
|
||||||
((handler.arg && !opt_pattern.test(rest[0])) ?
|
((handler.arg && !opt_pattern.test(rest[0])) ?
|
||||||
@ -693,6 +686,7 @@ object.Constructor('Parser', {
|
|||||||
return handler
|
return handler
|
||||||
&& [a, handler] }
|
&& [a, handler] }
|
||||||
|
|
||||||
|
// 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 = []
|
||||||
@ -748,7 +742,9 @@ object.Constructor('Parser', {
|
|||||||
&& handler.env in process.env)
|
&& handler.env in process.env)
|
||||||
|| handler.default)
|
|| handler.default)
|
||||||
&& seen.add(handler)
|
&& seen.add(handler)
|
||||||
&& runHandler(handler, a || k[0], rest)) })
|
&& runHandler(handler,
|
||||||
|
[k[0], handler.default],
|
||||||
|
rest)) })
|
||||||
|
|
||||||
// check required options...
|
// check required options...
|
||||||
var missing = parsed
|
var missing = parsed
|
||||||
@ -767,9 +763,6 @@ object.Constructor('Parser', {
|
|||||||
parsed.handleArgumentValue(parsed, root_value)
|
parsed.handleArgumentValue(parsed, root_value)
|
||||||
: root_value
|
: root_value
|
||||||
parsed.then(unhandled, root_value, rest)
|
parsed.then(unhandled, root_value, rest)
|
||||||
// XXX should we detach parsed from this???
|
|
||||||
// i.e. set:
|
|
||||||
// parsed.__proto__ = {}.__proto__
|
|
||||||
return parsed },
|
return parsed },
|
||||||
|
|
||||||
// NOTE: see general doc...
|
// NOTE: see general doc...
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "ig-argv",
|
"name": "ig-argv",
|
||||||
"version": "2.1.0",
|
"version": "2.1.1",
|
||||||
"description": "simple argv parser",
|
"description": "simple argv parser",
|
||||||
"main": "argv.js",
|
"main": "argv.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user