mirror of
https://github.com/flynx/argv.js.git
synced 2025-10-28 10:20:09 +00:00
minor tweaks...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
be48c11535
commit
870411ecef
190
README.md
190
README.md
@ -117,12 +117,12 @@ Let us populate the option definitions splitting the job into sections...
|
||||
|
||||
```javascript
|
||||
var parser = argv.Parser({
|
||||
// doc sections...
|
||||
varsion: '0.0.1',
|
||||
doc: 'Example script options',
|
||||
author: 'John Smith <j.smith@some-mail.com>',
|
||||
footer: 'Written by $AUTHOR ($VERSION / $LICENSE).',
|
||||
license: 'BSD-3-Clause',
|
||||
// doc sections...
|
||||
varsion: '0.0.1',
|
||||
doc: 'Example script options',
|
||||
author: 'John Smith <j.smith@some-mail.com>',
|
||||
footer: 'Written by $AUTHOR ($VERSION / $LICENSE).',
|
||||
license: 'BSD-3-Clause',
|
||||
```
|
||||
|
||||
|
||||
@ -134,79 +134,79 @@ If no value is given `true` is assigned to indicate that the option/command is
|
||||
present in the command-line.
|
||||
|
||||
```javascript
|
||||
'-bool': {
|
||||
doc: 'if given set .bool to true' },
|
||||
'-bool': {
|
||||
doc: 'if given set .bool to true' },
|
||||
|
||||
|
||||
// option with a value...
|
||||
'-value': {
|
||||
doc: 'set .x to X',
|
||||
// option with a value...
|
||||
'-value': {
|
||||
doc: 'set .x to X',
|
||||
|
||||
// 'X' (VALUE) is used for -help while 'x' (key) is where the
|
||||
// value will be written...
|
||||
arg: 'X | x',
|
||||
// 'X' (VALUE) is used for -help while 'x' (key) is where the
|
||||
// value will be written...
|
||||
arg: 'X | x',
|
||||
|
||||
// the value is optional by default but we can make it required...
|
||||
valueRequired: true,
|
||||
},
|
||||
// the value is optional by default but we can make it required...
|
||||
valueRequired: true,
|
||||
},
|
||||
|
||||
|
||||
// setup an alias -r -> -required
|
||||
'-r': '-required',
|
||||
// setup an alias -r -> -required
|
||||
'-r': '-required',
|
||||
|
||||
// a required option...
|
||||
'-required': {
|
||||
doc: 'set .required_option_given to true'
|
||||
// a required option...
|
||||
'-required': {
|
||||
doc: 'set .required_option_given to true'
|
||||
|
||||
// NOTE: we can omit the VALUE part to not require a value...
|
||||
// NOTE: of no attr is specified in arg option name is used.
|
||||
arg: '| required_option_given',
|
||||
// NOTE: we can omit the VALUE part to not require a value...
|
||||
// NOTE: of no attr is specified in arg option name is used.
|
||||
arg: '| required_option_given',
|
||||
|
||||
required: true,
|
||||
required: true,
|
||||
|
||||
// keep this near the top of the options list in -help...
|
||||
priority: 80,
|
||||
},
|
||||
// keep this near the top of the options list in -help...
|
||||
priority: 80,
|
||||
},
|
||||
|
||||
|
||||
'-int': {
|
||||
doc: 'pass an integer value',
|
||||
'-int': {
|
||||
doc: 'pass an integer value',
|
||||
|
||||
// NOTE: if not key is given the VALUE name is used as a key, so the
|
||||
// value here is assigned to .INT...
|
||||
arg: 'INT',
|
||||
// NOTE: if not key is given the VALUE name is used as a key, so the
|
||||
// value here is assigned to .INT...
|
||||
arg: 'INT',
|
||||
|
||||
// convert the input value to int...
|
||||
type: 'int',
|
||||
},
|
||||
|
||||
// convert the input value to int...
|
||||
type: 'int',
|
||||
},
|
||||
|
||||
|
||||
'-default': {
|
||||
doc: 'option with default value',
|
||||
arg: 'VALUE | default',
|
||||
'-default': {
|
||||
doc: 'option with default value',
|
||||
arg: 'VALUE | default',
|
||||
|
||||
default: 'some value',
|
||||
},
|
||||
default: 'some value',
|
||||
},
|
||||
|
||||
|
||||
'-home': {
|
||||
doc: 'set home path',
|
||||
arg: 'HOME | home',
|
||||
'-home': {
|
||||
doc: 'set home path',
|
||||
arg: 'HOME | home',
|
||||
|
||||
// get the default value from the environment variable $HOME...
|
||||
env: 'HOME',
|
||||
},
|
||||
// get the default value from the environment variable $HOME...
|
||||
env: 'HOME',
|
||||
},
|
||||
|
||||
|
||||
// collecting values...
|
||||
'-p': '-push',
|
||||
'-push': {
|
||||
doc: 'push elements to a .list',
|
||||
arg: 'ELEM | list',
|
||||
|
||||
// collecting values...
|
||||
'-p': '-push',
|
||||
'-push': {
|
||||
doc: 'push elements to a .list',
|
||||
arg: 'ELEM | list',
|
||||
|
||||
// this will add each argument to a -push option to a list...
|
||||
collect: 'list',
|
||||
},
|
||||
// this will add each argument to a -push option to a list...
|
||||
collect: 'list',
|
||||
},
|
||||
```
|
||||
|
||||
|
||||
@ -217,13 +217,13 @@ that determines how it is parsed, otherwise they are identical and everything
|
||||
above applies here too.
|
||||
|
||||
```javascript
|
||||
'@command': {
|
||||
// ...
|
||||
},
|
||||
'@command': {
|
||||
// ...
|
||||
},
|
||||
|
||||
// Since options and commands are identical, aliases from one to the
|
||||
// other work as expected...
|
||||
'-c': '@command',
|
||||
// Since options and commands are identical, aliases from one to the
|
||||
// other work as expected...
|
||||
'-c': '@command',
|
||||
```
|
||||
|
||||
|
||||
@ -232,19 +232,19 @@ above applies here too.
|
||||
These define `.handler`s which are executed when the option is encountered
|
||||
by the parser
|
||||
```javascript
|
||||
'-active': {
|
||||
doc: 'basic active option',
|
||||
handler: function(args, key, value){
|
||||
// ...
|
||||
} },
|
||||
'-active': {
|
||||
doc: 'basic active option',
|
||||
handler: function(args, key, value){
|
||||
// ...
|
||||
} },
|
||||
```
|
||||
|
||||
And for quick-n-dirty hacking stuff together, a shorthand (_not for production use_):
|
||||
```javascript
|
||||
'-s': '-shorthand-active',
|
||||
'-shorthand-active': function(args, key, value){
|
||||
// ...
|
||||
},
|
||||
'-s': '-shorthand-active',
|
||||
'-shorthand-active': function(args, key, value){
|
||||
// ...
|
||||
},
|
||||
```
|
||||
|
||||
|
||||
@ -253,11 +253,11 @@ And for quick-n-dirty hacking stuff together, a shorthand (_not for production u
|
||||
An options/command handler can also be a full fledged parser.
|
||||
|
||||
```javascript
|
||||
'@nested': argv.Parser({
|
||||
// ...
|
||||
}).then(function(){
|
||||
// ...
|
||||
}),
|
||||
'@nested': argv.Parser({
|
||||
// ...
|
||||
}).then(function(){
|
||||
// ...
|
||||
}),
|
||||
```
|
||||
|
||||
This can be useful when there is a need to define a sub-context with it's own
|
||||
@ -280,16 +280,16 @@ To stop option processing either return `STOP` or `THEN` from the handler.
|
||||
|
||||
- `THEN` is the normal case, stop processing and trigger [`<parser>.then(..)`](./ADVANCED.md#parserthen):
|
||||
```javascript
|
||||
'-then': {
|
||||
handler: function(){
|
||||
return argv.THEN } },
|
||||
'-then': {
|
||||
handler: function(){
|
||||
return argv.THEN } },
|
||||
```
|
||||
|
||||
- `STOP` will stop processing and trigger [`<parser>.stop(..)`](./ADVANCED.md#parserstop):
|
||||
```javascript
|
||||
'-stop': {
|
||||
handler: function(){
|
||||
return argv.STOP } },
|
||||
'-stop': {
|
||||
handler: function(){
|
||||
return argv.STOP } },
|
||||
```
|
||||
|
||||
|
||||
@ -299,18 +299,18 @@ There are three ways to stop and/or report errors:
|
||||
|
||||
- Simply `throw` a `ParserError(..)` instance:
|
||||
```javascript
|
||||
'-error': {
|
||||
handler: function(){
|
||||
throw argv.ParserError('something went wrong.') } },
|
||||
'-error': {
|
||||
handler: function(){
|
||||
throw argv.ParserError('something went wrong.') } },
|
||||
```
|
||||
Here processing will stop and the error will be reported automatically
|
||||
before [`<parser>.error(..)`](./ADVANCED.md#parsererror-1) is triggered.
|
||||
|
||||
- _Silently_ `return` a `ParserError(..)` instance:
|
||||
```javascript
|
||||
'-silent-error': {
|
||||
handler: function(){
|
||||
return argv.ParserError('something went wrong.') } },
|
||||
'-silent-error': {
|
||||
handler: function(){
|
||||
return argv.ParserError('something went wrong.') } },
|
||||
```
|
||||
This will _not_ report the error but will stop processing and trigger
|
||||
[`<parser>.error(..)`](./ADVANCED.md#parsererror-1), so the user can either
|
||||
@ -318,12 +318,12 @@ There are three ways to stop and/or report errors:
|
||||
|
||||
- For a critical error simply `throw` any other JavaScript error/exception:
|
||||
```javascript
|
||||
'-critical-error': {
|
||||
handler: function(){
|
||||
throw 'something went really wrong.' } },
|
||||
'-critical-error': {
|
||||
handler: function(){
|
||||
throw 'something went really wrong.' } },
|
||||
|
||||
// and to close things off ;)
|
||||
})
|
||||
// and to close things off ;)
|
||||
})
|
||||
```
|
||||
|
||||
Note that [`<parser>.then(..)`](./ADVANCED.md#parserthen) will not be triggered
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user