mirror of
https://github.com/flynx/argv.js.git
synced 2025-10-28 18:30:07 +00:00
added unknown option delegation up (EXPERIMENTAL)
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
72103ea846
commit
cfaa901611
13
README.md
13
README.md
@ -17,9 +17,6 @@ This code is an evolution of that parser.
|
||||
|
||||
- Simple / well documented
|
||||
- Supports both the _option_ (a-la `ls`) and _command_ (a-la `git`) paradigms
|
||||
- Nestable
|
||||
parsers can be nested as option/command handlers defining independent
|
||||
nested contexts
|
||||
- Option expansion
|
||||
`-abc` expands to `-a -b -c` if `-abc` is not defined
|
||||
- Option/command value assignment
|
||||
@ -37,6 +34,12 @@ This code is an evolution of that parser.
|
||||
- `-version` – print version
|
||||
- `-quiet` – suppress printing
|
||||
- `-` – stop argument processing
|
||||
- Nestable
|
||||
parsers can be nested as option/command handlers defining independent
|
||||
nested contexts
|
||||
- Option delegation
|
||||
options not handled by the current nested parser will be automatically
|
||||
delegated back to parent parser
|
||||
- Extensible and self-applicable
|
||||
|
||||
|
||||
@ -454,6 +457,10 @@ external command.
|
||||
When a nested parser is started it will consume subsequent arguments until it
|
||||
exits, then the parent parser will pick up where it left.
|
||||
|
||||
When a nested parser encounters an unknown option/command it will stop and
|
||||
the option will be delegated to the parent parser. This can be disabled by
|
||||
setting `<parser>.delegateUnknownToParent` to `false`.
|
||||
|
||||
Externally it is treated in exactly the same way as a normal _function_ handler,
|
||||
essentially, the parent parser does not know the difference between the two.
|
||||
|
||||
|
||||
27
argv.js
27
argv.js
@ -52,8 +52,9 @@ var COMMAND_PREFIX = '@'
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
|
||||
module.STOP = object.STOP
|
||||
|| {doc: 'Stop option processing, triggers .stop(..) handlers'}
|
||||
module.STOP =
|
||||
object.STOP
|
||||
|| {doc: 'Stop option processing, triggers .stop(..) handlers'}
|
||||
|
||||
module.THEN =
|
||||
{doc: 'Break option processing, triggers .then(..) handlers'}
|
||||
@ -1071,12 +1072,30 @@ object.Constructor('Parser', {
|
||||
//
|
||||
// NOTE: to explicitly handle '-*' option or '*' command define handlers
|
||||
// for them under '-\\*' and '@\\*' respectively.
|
||||
|
||||
// Handle unknown otions...
|
||||
// - deligate to parent if .delegateUnknownToParent is true
|
||||
// - thrwo error
|
||||
delegateUnknownToParent: true,
|
||||
'-*': {
|
||||
doc: false,
|
||||
//section_doc: ...,
|
||||
handler: function(_, key){
|
||||
handler: function(_, key, value){
|
||||
// delegate to parent...
|
||||
if(this.delegateUnknownToParent
|
||||
&& this.parent){
|
||||
this.parent.rest.unshift(
|
||||
value === undefined ?
|
||||
key
|
||||
: key+'='+value)
|
||||
return module.THEN }
|
||||
// error...
|
||||
throw module.ParserError(
|
||||
`Unknown ${key.startsWith('-') ? 'option:' : 'command:'} $ARG`) } },
|
||||
`Unknown ${
|
||||
key.startsWith('-') ?
|
||||
'option:'
|
||||
: 'command:'
|
||||
} $ARG`) } },
|
||||
'@*': '-*',
|
||||
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ig-argv",
|
||||
"version": "2.16.10",
|
||||
"version": "2.17.0",
|
||||
"description": "simple argv parser",
|
||||
"main": "argv.js",
|
||||
"scripts": {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user