mirror of
https://github.com/flynx/argv.js.git
synced 2025-10-29 10:50:06 +00:00
added env handlers...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
390204cb35
commit
cf29608c6a
95
argv.js
95
argv.js
@ -111,11 +111,6 @@ var afterCallbackCall = function(name, context, ...args){
|
|||||||
// .....or should it be the responsibility of the user defining
|
// .....or should it be the responsibility of the user defining
|
||||||
// the command???
|
// the command???
|
||||||
// XXX should we handle <scriptName>-<command> script calls???
|
// XXX should we handle <scriptName>-<command> script calls???
|
||||||
// XXX ENV might be a good idea to add handler.env = x to use the environment
|
|
||||||
// variable x as the default value for option...
|
|
||||||
// ...would also need to add this to -help as '(default: $VARIABLE_NAME)'
|
|
||||||
// .....for this we'll need to set all the env options regardless
|
|
||||||
// if they were passed by user or not...
|
|
||||||
// XXX should .options(..), .commands(..) and .handler(..) be:
|
// XXX should .options(..), .commands(..) and .handler(..) be:
|
||||||
// .getOptions(..), .getCommands(..) and .getHandler(..) respectively???
|
// .getOptions(..), .getCommands(..) and .getHandler(..) respectively???
|
||||||
var Parser =
|
var Parser =
|
||||||
@ -146,7 +141,7 @@ object.Constructor('Parser', {
|
|||||||
// ...
|
// ...
|
||||||
// ]
|
// ]
|
||||||
//
|
//
|
||||||
// XXX add option groups...
|
// XXX add option groups... (???)
|
||||||
options: function(...prefix){
|
options: function(...prefix){
|
||||||
var that = this
|
var that = this
|
||||||
prefix = prefix.length == 0 ?
|
prefix = prefix.length == 0 ?
|
||||||
@ -179,6 +174,10 @@ object.Constructor('Parser', {
|
|||||||
1
|
1
|
||||||
: ai - bi })
|
: ai - bi })
|
||||||
.map(function([e, _]){ return e }) },
|
.map(function([e, _]){ return e }) },
|
||||||
|
envOptions: function(){
|
||||||
|
return this.options()
|
||||||
|
.filter(function([k, a, d, handler]){
|
||||||
|
return !!handler.env }) },
|
||||||
commands: function(){
|
commands: function(){
|
||||||
return this.options(this.commandPrefix) },
|
return this.options(this.commandPrefix) },
|
||||||
isCommand: function(str){
|
isCommand: function(str){
|
||||||
@ -274,9 +273,7 @@ object.Constructor('Parser', {
|
|||||||
return a.length - b.length})
|
return a.length - b.length})
|
||||||
.join(' | -')
|
.join(' | -')
|
||||||
+' '+ (arg || ''),
|
+' '+ (arg || ''),
|
||||||
// XXX ENV
|
...formDoc(doc, handler.env) ] })),
|
||||||
//...formDoc(doc, handler.env) ] })),
|
|
||||||
...formDoc(doc) ] })),
|
|
||||||
// dynamic options...
|
// dynamic options...
|
||||||
...section('Dynamic options',
|
...section('Dynamic options',
|
||||||
this.handleArgument ?
|
this.handleArgument ?
|
||||||
@ -291,9 +288,7 @@ object.Constructor('Parser', {
|
|||||||
.map(function(cmd){ return cmd.slice(1)})
|
.map(function(cmd){ return cmd.slice(1)})
|
||||||
.join(' | ')
|
.join(' | ')
|
||||||
+' '+ (arg || ''),
|
+' '+ (arg || ''),
|
||||||
// XXX ENV
|
...formDoc(doc, handler.env) ] })),
|
||||||
//...formDoc(doc, handler.env) ] })),
|
|
||||||
...formDoc(doc) ] })),
|
|
||||||
// examples (optional)...
|
// examples (optional)...
|
||||||
...section('Examples',
|
...section('Examples',
|
||||||
this.examples instanceof Array ?
|
this.examples instanceof Array ?
|
||||||
@ -432,6 +427,37 @@ object.Constructor('Parser', {
|
|||||||
|
|
||||||
var opt_pattern = this.optionInputPattern
|
var opt_pattern = this.optionInputPattern
|
||||||
|
|
||||||
|
var runHandler = function(handler, arg, value, rest){
|
||||||
|
// get option value...
|
||||||
|
value = value
|
||||||
|
|| ((handler.arg && !opt_pattern.test(rest[0])) ?
|
||||||
|
rest.shift()
|
||||||
|
: handler.env ?
|
||||||
|
process.env[handler.env]
|
||||||
|
: undefined)
|
||||||
|
// value conversion...
|
||||||
|
value = (value && that.handleArgumentValue) ?
|
||||||
|
that.handleArgumentValue(handler, value)
|
||||||
|
: value
|
||||||
|
// run handler...
|
||||||
|
var res = (typeof(handler) == 'function' ?
|
||||||
|
handler
|
||||||
|
: handler.handler)
|
||||||
|
.call(that,
|
||||||
|
rest,
|
||||||
|
arg,
|
||||||
|
...(value ? [value] : []))
|
||||||
|
// handle .STOP / .ERROR
|
||||||
|
if(res === module.STOP || res === module.ERROR){
|
||||||
|
afterCallbackCall(
|
||||||
|
res === module.STOP ? 'stop' : 'error',
|
||||||
|
this, arg)
|
||||||
|
res === module.ERROR
|
||||||
|
&& this.handleErrorExit
|
||||||
|
&& this.handleErrorExit(arg) }
|
||||||
|
return res }
|
||||||
|
|
||||||
|
var env = new Set()
|
||||||
var unhandled = []
|
var unhandled = []
|
||||||
while(rest.length > 0){
|
while(rest.length > 0){
|
||||||
var [arg, value] = rest.shift().split(/=/)
|
var [arg, value] = rest.shift().split(/=/)
|
||||||
@ -445,41 +471,26 @@ object.Constructor('Parser', {
|
|||||||
// get handler...
|
// get handler...
|
||||||
var handler = this.handler(arg)[1]
|
var handler = this.handler(arg)[1]
|
||||||
|| this.handleArgument
|
|| this.handleArgument
|
||||||
// get option value...
|
// env handler called...
|
||||||
value = value
|
handler.env
|
||||||
|| ((handler.arg && !opt_pattern.test(rest[0])) ?
|
&& env.add(handler)
|
||||||
rest.shift()
|
|
||||||
// XXX ENV
|
var res = runHandler(handler, arg, value, rest)
|
||||||
//: handler.env ?
|
|
||||||
// process.env[handler.env]
|
// handle stop conditions...
|
||||||
: undefined)
|
|
||||||
// value conversion...
|
|
||||||
value = (value && this.handleArgumentValue) ?
|
|
||||||
this.handleArgumentValue(handler, value)
|
|
||||||
: value
|
|
||||||
// run handler...
|
|
||||||
var res = (typeof(handler) == 'function' ?
|
|
||||||
handler
|
|
||||||
: handler.handler)
|
|
||||||
.call(this,
|
|
||||||
rest,
|
|
||||||
arg,
|
|
||||||
...(value ? [value] : []))
|
|
||||||
// handle .STOP / .ERROR
|
|
||||||
if(res === module.STOP || res === module.ERROR){
|
if(res === module.STOP || res === module.ERROR){
|
||||||
afterCallbackCall(
|
return nested ?
|
||||||
res === module.STOP ? 'stop' : 'error',
|
|
||||||
this, arg)
|
|
||||||
res === module.ERROR
|
|
||||||
&& this.handleErrorExit
|
|
||||||
&& this.handleErrorExit(arg)
|
|
||||||
return nested ?
|
|
||||||
res
|
res
|
||||||
: this }
|
: this }
|
||||||
continue }
|
continue }
|
||||||
// unhandled...
|
// unhandled...
|
||||||
unhandled.push(arg) }
|
unhandled.push(arg) }
|
||||||
|
// call env handlers that were not explicitly called yet...
|
||||||
|
this.envOptions()
|
||||||
|
.forEach(function([k, a, d, handler]){
|
||||||
|
env.has(handler)
|
||||||
|
|| (handler.env in process.env
|
||||||
|
&& runHandler(handler, a, null, rest)) })
|
||||||
// post handlers...
|
// post handlers...
|
||||||
root_value = root_value && this.handleArgumentValue ?
|
root_value = root_value && this.handleArgumentValue ?
|
||||||
this.handleArgumentValue(this, root_value)
|
this.handleArgumentValue(this, root_value)
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "ig-argv",
|
"name": "ig-argv",
|
||||||
"version": "2.0.3",
|
"version": "2.0.4",
|
||||||
"description": "simple argv parser",
|
"description": "simple argv parser",
|
||||||
"main": "argv.js",
|
"main": "argv.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
7
test.js
7
test.js
@ -40,10 +40,11 @@ argv.Parser({
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
'@test': argv.Parser({
|
'-test': argv.Parser({
|
||||||
// XXX ENV
|
// XXX ENV
|
||||||
//env: 'TEST',
|
env: 'TEST',
|
||||||
}),
|
}).then(function(){
|
||||||
|
console.log('TEST', ...arguments) }),
|
||||||
|
|
||||||
'@nested': argv.Parser({
|
'@nested': argv.Parser({
|
||||||
doc: 'nested parser.',
|
doc: 'nested parser.',
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user