mirror of
https://github.com/flynx/argv.js.git
synced 2025-10-29 02:40:07 +00:00
reafctoring and tweaking...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
5aaae8493d
commit
2a8f7eaf1c
84
argv.js
84
argv.js
@ -41,21 +41,40 @@ module.ERROR =
|
|||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
// helpers...
|
// helpers...
|
||||||
|
|
||||||
// XXX does this need to be an event???
|
//
|
||||||
// XXX doc...
|
// afterCallback(name)
|
||||||
var afterCallback = function(name){
|
// -> func
|
||||||
|
//
|
||||||
|
// afterCallback(name, pre-action, post-action)
|
||||||
|
// -> func
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// func(..)
|
||||||
|
// -> this
|
||||||
|
// -> res
|
||||||
|
//
|
||||||
|
var afterCallback = function(name, pre, post){
|
||||||
var attr = '__after_'+ name
|
var attr = '__after_'+ name
|
||||||
return function(func){
|
return function(...args){
|
||||||
var that = this
|
var that = this
|
||||||
var args = [...arguments]
|
// bind...
|
||||||
;(args.length == 1 && typeof(func) == 'function') ?
|
if(args.length == 1 && typeof(args[0]) == 'function'){
|
||||||
// add handler...
|
(this[attr] = this[attr] || []).push(args[0])
|
||||||
(this[attr] = this[attr] || []).push(func)
|
return this }
|
||||||
|
// pre callback...
|
||||||
|
var call = pre ?
|
||||||
|
(pre.call(this, ...args) !== false)
|
||||||
|
: true
|
||||||
|
return ((call && this[attr] || [])
|
||||||
// call handlers...
|
// call handlers...
|
||||||
: (this[attr] || [])
|
.map(function(func){
|
||||||
.forEach(function(func){
|
return func.call(that, ...args) })
|
||||||
func.call(that, ...args) })
|
// stop if module.STOP is returned and return this...
|
||||||
return this } }
|
.includes(false) && this)
|
||||||
|
// post callback...
|
||||||
|
|| (post ?
|
||||||
|
post.call(this, ...args)
|
||||||
|
: this) } }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -160,11 +179,11 @@ var afterCallback = function(name){
|
|||||||
// NOTE: essentially this parser is a very basic stack language...
|
// NOTE: essentially this parser is a very basic stack language...
|
||||||
// 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 prefixes, like '+' and the like???
|
||||||
// ...add prefix handlers???
|
// ...add prefix handlers???
|
||||||
// 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...
|
||||||
|
// XXX might be a good idea to read metadata from package.json
|
||||||
// XXX should we handle <scriptName>-<command> script calls???
|
// XXX should we handle <scriptName>-<command> script calls???
|
||||||
// XXX might be a good idea to use exceptions for ERROR...
|
// XXX might be a good idea to use exceptions for ERROR...
|
||||||
var Parser =
|
var Parser =
|
||||||
@ -189,23 +208,27 @@ object.Constructor('Parser', {
|
|||||||
optionInputPattern: /^--?(.*)$/,
|
optionInputPattern: /^--?(.*)$/,
|
||||||
commandInputPattern: /^([a-zA-Z*].*)$/,
|
commandInputPattern: /^([a-zA-Z*].*)$/,
|
||||||
|
|
||||||
// instance stuff...
|
|
||||||
argv: null,
|
|
||||||
rest: null,
|
|
||||||
rootValue: null,
|
|
||||||
|
|
||||||
|
// instance stuff...
|
||||||
|
// XXX dp we need all three???
|
||||||
|
script: null,
|
||||||
scriptNmae: null,
|
scriptNmae: null,
|
||||||
scriptPath: null,
|
scriptPath: null,
|
||||||
|
|
||||||
|
argv: null,
|
||||||
|
rest: null,
|
||||||
|
unhandled: null,
|
||||||
|
rootValue: null,
|
||||||
|
|
||||||
|
|
||||||
// output...
|
// output...
|
||||||
//
|
//
|
||||||
print: function(...args){
|
print: afterCallback('print', null, function(...args){
|
||||||
console.log(...args)
|
console.log(...args)
|
||||||
return this },
|
return this }),
|
||||||
printError: function(...args){
|
printError: afterCallback('print_error', null, function(...args){
|
||||||
console.error(this.scriptName+': Error:', ...args)
|
console.error(this.scriptName+': Error:', ...args)
|
||||||
return this },
|
return this }),
|
||||||
|
|
||||||
|
|
||||||
// Handler API...
|
// Handler API...
|
||||||
@ -357,7 +380,7 @@ object.Constructor('Parser', {
|
|||||||
[`Env: \$${handler.env}`]
|
[`Env: \$${handler.env}`]
|
||||||
: []),
|
: []),
|
||||||
].join(', ')
|
].join(', ')
|
||||||
return [doc,
|
return [doc.replace(/\\\*/g, '*'),
|
||||||
...(info.length > 0 ?
|
...(info.length > 0 ?
|
||||||
['('+ info +')']
|
['('+ info +')']
|
||||||
: [])] }
|
: [])] }
|
||||||
@ -395,8 +418,12 @@ object.Constructor('Parser', {
|
|||||||
opts = opts instanceof Array ? opts : [opts]
|
opts = opts instanceof Array ? opts : [opts]
|
||||||
return [
|
return [
|
||||||
[opts
|
[opts
|
||||||
|
// unquote...
|
||||||
|
.map(function(o){
|
||||||
|
return o.replace(/\\\*/g, '*') })
|
||||||
.sort(function(a, b){
|
.sort(function(a, b){
|
||||||
return a.length - b.length})
|
return a.length - b.length})
|
||||||
|
// form: "-x, --xx"
|
||||||
.map(function(o, i){
|
.map(function(o, i){
|
||||||
return o.length <= 2 ?
|
return o.length <= 2 ?
|
||||||
o
|
o
|
||||||
@ -498,7 +525,6 @@ object.Constructor('Parser', {
|
|||||||
// NOTE: to explicitly handle '-*' option or '*' command define handlers
|
// NOTE: to explicitly handle '-*' option or '*' command define handlers
|
||||||
// for them under '-\\*' and '@\\*' respectively.
|
// for them under '-\\*' and '@\\*' respectively.
|
||||||
'-*': {
|
'-*': {
|
||||||
//key: '-*',
|
|
||||||
doc: false,
|
doc: false,
|
||||||
//section_doc: ...,
|
//section_doc: ...,
|
||||||
handler: function(_, key){
|
handler: function(_, key){
|
||||||
@ -676,7 +702,7 @@ object.Constructor('Parser', {
|
|||||||
// skip single letter unknown 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 [arg, undefined] }
|
||||||
// split and normalize...
|
// split and normalize...
|
||||||
var [a, ...r] =
|
var [a, ...r] =
|
||||||
[...arg.slice(1)]
|
[...arg.slice(1)]
|
||||||
@ -687,8 +713,7 @@ object.Constructor('Parser', {
|
|||||||
// push new options back to option "stack"...
|
// push new options back to option "stack"...
|
||||||
rest.splice(0, 0, ...r)
|
rest.splice(0, 0, ...r)
|
||||||
var handler = parsed.handler(a)[1]
|
var handler = parsed.handler(a)[1]
|
||||||
return handler
|
return [a, handler] }
|
||||||
&& [a, handler] }
|
|
||||||
|
|
||||||
// parse the arguments and call handlers...
|
// parse the arguments and call handlers...
|
||||||
var values = new Set()
|
var values = new Set()
|
||||||
@ -711,11 +736,10 @@ object.Constructor('Parser', {
|
|||||||
// get handler...
|
// get handler...
|
||||||
var handler = parsed.handler(arg)[1]
|
var handler = parsed.handler(arg)[1]
|
||||||
// handle merged options...
|
// handle merged options...
|
||||||
// NOTE: we replace arg here...
|
|
||||||
|| (type == 'opt'
|
|| (type == 'opt'
|
||||||
&& parsed.splitOptions
|
&& parsed.splitOptions
|
||||||
// XXX a tad ugly...
|
// NOTE: we set arg here...
|
||||||
&& (([arg, handler] = splitArgs(arg, rest)), handler))
|
&& ([arg, handler] = splitArgs(arg, rest))[1] )
|
||||||
// dynamic or error...
|
// dynamic or error...
|
||||||
|| parsed.handler(dfl)[1]
|
|| parsed.handler(dfl)[1]
|
||||||
// no handler found and '-*' or '@*' not defined...
|
// no handler found and '-*' or '@*' not defined...
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "ig-argv",
|
"name": "ig-argv",
|
||||||
"version": "2.2.4",
|
"version": "2.2.6",
|
||||||
"description": "simple argv parser",
|
"description": "simple argv parser",
|
||||||
"main": "argv.js",
|
"main": "argv.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
11
test.js
11
test.js
@ -52,7 +52,6 @@ argv.Parser({
|
|||||||
default: 333,
|
default: 333,
|
||||||
},
|
},
|
||||||
|
|
||||||
// XXX need to handle value correctly...
|
|
||||||
'-test': argv.Parser({
|
'-test': argv.Parser({
|
||||||
env: 'TEST',
|
env: 'TEST',
|
||||||
arg: 'TEST',
|
arg: 'TEST',
|
||||||
@ -60,6 +59,7 @@ argv.Parser({
|
|||||||
}).then(function(){
|
}).then(function(){
|
||||||
console.log('TEST', ...arguments) }),
|
console.log('TEST', ...arguments) }),
|
||||||
|
|
||||||
|
'-i': '-int',
|
||||||
'-int': {
|
'-int': {
|
||||||
arg: 'INT|int',
|
arg: 'INT|int',
|
||||||
type: 'int',
|
type: 'int',
|
||||||
@ -93,10 +93,13 @@ argv.Parser({
|
|||||||
'-x': '-y',
|
'-x': '-y',
|
||||||
'-y': '-x',
|
'-y': '-x',
|
||||||
|
|
||||||
'-a': '-b',
|
'-k': '-l',
|
||||||
'-b': '-c',
|
'-l': '-m',
|
||||||
'-c': '-a',
|
'-m': '-k',
|
||||||
})
|
})
|
||||||
|
//.print(function(...args){
|
||||||
|
// console.log('----\n', ...args)
|
||||||
|
// return argv.STOP })
|
||||||
.then(function(){
|
.then(function(){
|
||||||
console.log('DONE', ...arguments) })
|
console.log('DONE', ...arguments) })
|
||||||
.stop(function(){
|
.stop(function(){
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user