mirror of
https://github.com/flynx/argv.js.git
synced 2025-10-28 10:20:09 +00:00
notes, doces and some tweaking....
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
191790edeb
commit
13c488850c
16
ADVANCED.md
16
ADVANCED.md
@ -54,7 +54,7 @@ For basics see [README.md](./README.md)
|
|||||||
- [`<parser>.error(..)`](#parsererror-1)
|
- [`<parser>.error(..)`](#parsererror-1)
|
||||||
- [`<parser>.off(..)`](#parseroff)
|
- [`<parser>.off(..)`](#parseroff)
|
||||||
- [`<parser>(..)`](#parser-1)
|
- [`<parser>(..)`](#parser-1)
|
||||||
- [`Parser.chain(..)` (EXPERIMENTAL)](#parserchain-experimental)
|
- [`Parser.chain(..)` _(EXPERIMENTAL)_](#parserchain-experimental)
|
||||||
- [Advanced parser API](#advanced-parser-api)
|
- [Advanced parser API](#advanced-parser-api)
|
||||||
- [`<parser>.print(..)` / `<parser>.printError(..)`](#parserprint--parserprinterror)
|
- [`<parser>.print(..)` / `<parser>.printError(..)`](#parserprint--parserprinterror)
|
||||||
- [`<parser>.handlerDefault(..)`](#parserhandlerdefault)
|
- [`<parser>.handlerDefault(..)`](#parserhandlerdefault)
|
||||||
@ -764,7 +764,7 @@ be ignored, otherwise the whole list is processed as if `<main>` was
|
|||||||
its head.
|
its head.
|
||||||
|
|
||||||
|
|
||||||
### `Parser.chain(..)` (EXPERIMENTAL)
|
### `Parser.chain(..)` _(EXPERIMENTAL)_
|
||||||
|
|
||||||
Chain several parsers for staggering option/command processing.
|
Chain several parsers for staggering option/command processing.
|
||||||
```
|
```
|
||||||
@ -779,15 +779,19 @@ that need to be processed out of order and before anything else.
|
|||||||
This is similar to chaining parsers via `.then(..)` but with additional setup:
|
This is similar to chaining parsers via `.then(..)` but with additional setup:
|
||||||
- all parsers except the last will have:
|
- all parsers except the last will have:
|
||||||
- `.splitOptions` set to `false`
|
- `.splitOptions` set to `false`
|
||||||
|
- `"-"` set to `undefined`
|
||||||
- `"-help"` set to `undefined` enabling `-help` pass-through
|
- `"-help"` set to `undefined` enabling `-help` pass-through
|
||||||
- `"-*"` and `"@*"` set to `undefined` enabling arguments pass-through
|
- `"-*"` and `"@*"` set to `undefined` enabling arguments pass-through
|
||||||
- the last parser will have all the options from the other parsers merged into
|
- the last parser will have:
|
||||||
it for complete docs/`-help`
|
- all the options from the other parsers merged into it for complete
|
||||||
|
docs/`-help`
|
||||||
|
- `.splitOptions` set to `false`
|
||||||
|
|
||||||
|
|
||||||
XXX the resulting `<parsed>` object will only contain data from the last parser,
|
_XXX the resulting `<parsed>` object will only contain data from the last parser,
|
||||||
this may change in the future.
|
this may change in the future..._
|
||||||
|
|
||||||
|
_XXX not sure about the `.splitOptions` restriction on the last parser yet, but it definitely should not include the options from preceding parsers..._
|
||||||
|
|
||||||
|
|
||||||
## Advanced parser API
|
## Advanced parser API
|
||||||
|
|||||||
40
argv.js
40
argv.js
@ -320,15 +320,32 @@ object.Constructor('Parser', {
|
|||||||
},
|
},
|
||||||
|
|
||||||
// XXX this does not merge the parse results... (???)
|
// XXX this does not merge the parse results... (???)
|
||||||
|
// XXX splitting the high priority args should not work...
|
||||||
|
// XXX object.deepKeys(..) ???
|
||||||
// XXX EXPERIMENTAL...
|
// XXX EXPERIMENTAL...
|
||||||
chain: function(...parsers){
|
chain: function(...parsers){
|
||||||
var Parser = this
|
var Parser = this
|
||||||
var [post, ...pre] = parsers.reverse()
|
var [post, ...pre] = parsers.reverse()
|
||||||
pre.reverse()
|
pre.reverse()
|
||||||
|
|
||||||
|
// only update values that were not explicitly set...
|
||||||
|
var update = function(e, o){
|
||||||
|
return Object.assign(
|
||||||
|
e,
|
||||||
|
Object.fromEntries(
|
||||||
|
Object.entries(o)
|
||||||
|
.map(function([k, v]){
|
||||||
|
return [k,
|
||||||
|
e.hasOwnProperty(k) ?
|
||||||
|
e[k]
|
||||||
|
: v ] }) )) }
|
||||||
|
|
||||||
// prepare the final parser for merged doc...
|
// prepare the final parser for merged doc...
|
||||||
// XXX object.deepKeys(..) ???
|
// NOTE: pre values have priority over post values...
|
||||||
var final = Parser(Object.assign({},
|
var final = Parser(Object.assign({
|
||||||
|
// XXX can we remove this restriction???
|
||||||
|
splitOptions: false,
|
||||||
|
},
|
||||||
// set attribute order...
|
// set attribute order...
|
||||||
// NOTE: this is here to set the attribute order according
|
// NOTE: this is here to set the attribute order according
|
||||||
// to priority...
|
// to priority...
|
||||||
@ -337,26 +354,29 @@ object.Constructor('Parser', {
|
|||||||
post,
|
post,
|
||||||
...pre))
|
...pre))
|
||||||
|
|
||||||
return pre
|
// build the chain...
|
||||||
|
pre = pre
|
||||||
// setup the chain for arg pass-through...
|
// setup the chain for arg pass-through...
|
||||||
.map(function(e){
|
.map(function(e){
|
||||||
// XXX object.deepKeys(..) ???
|
|
||||||
return Parser(Object.assign({},
|
return Parser(Object.assign({},
|
||||||
e,
|
update(e, {
|
||||||
{
|
|
||||||
splitOptions: false,
|
splitOptions: false,
|
||||||
'-help': undefined,
|
'-help': undefined,
|
||||||
'-*': undefined,
|
'-*': undefined,
|
||||||
'@*': undefined,
|
'@*': undefined,
|
||||||
})) })
|
'-': undefined,
|
||||||
.concat([final])
|
}))) })
|
||||||
// chain...
|
// chain...
|
||||||
|
pre
|
||||||
.reduce(function(res, cur){
|
.reduce(function(res, cur){
|
||||||
return res ?
|
return res ?
|
||||||
// NOTE: need to call .then(..) on each of the parsers,
|
// NOTE: need to call .then(..) on each of the parsers,
|
||||||
// so we return cur to be next...
|
// so we return cur to be next...
|
||||||
(res.then(cur), cur)
|
(res.then(cur), cur)
|
||||||
: cur }, null) },
|
: cur }, null)
|
||||||
|
.then(final)
|
||||||
|
|
||||||
|
return pre[0] },
|
||||||
}, {
|
}, {
|
||||||
// config...
|
// config...
|
||||||
//
|
//
|
||||||
|
|||||||
@ -67,7 +67,7 @@ argv.Parser.chain({
|
|||||||
'-b': {
|
'-b': {
|
||||||
doc: 'medium priority option',
|
doc: 'medium priority option',
|
||||||
handler: function(){
|
handler: function(){
|
||||||
console.log('### normal priority option') }},
|
console.log('### medium priority option') }},
|
||||||
},{
|
},{
|
||||||
'-c': {
|
'-c': {
|
||||||
doc: 'normal priority option',
|
doc: 'normal priority option',
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user