updated docs after fdd's input (thanks)...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-08-02 16:18:14 +03:00
parent 2a269b3f1c
commit f54e555abd
4 changed files with 63 additions and 19 deletions

View File

@ -218,20 +218,33 @@ for example:
These, if encountered, simply assign a value to an attribute on the parsed object. These, if encountered, simply assign a value to an attribute on the parsed object.
If no value is given `true` is assigned to indicate that the option/command is Any option/command can be passed a value, either explicitly (e.g. `-opt=123`) or
present in the command-line. implicitly by first setting `.arg` (see examples below) and and then passing `-opt 123`.
If no value is given `true` is assigned to option attribute on the parsed object
to indicate that the option/command is present in the command-line.
Note that repeating a basic option/command will overwrite the previous value
unless `.collect` is set (see `-push` example below).
Note that in general case option order in the command-line is not critical,
but option context can matter (see: [Active options/commands](#active-optionscommands) and [Nested parsers](#nested-parsers))
```javascript ```javascript
'-bool': { '-bool': {
doc: 'if given set .bool to true' }, doc: 'if given, set .bool to true' },
// option with a value... // option with a value...
'-value': { '-value': {
doc: 'set .x to X', doc: 'set .x to X',
// 'X' (VALUE) is used for -help while 'x' (key) is where the // 'X' (i.e. VALUE) is used to indicate the option value in -help
// value will be written... // while 'x' (key) is the attribute where the value will be written...
//
// NOTE: if no .arg is set option attribute name is used.
//
// See the first example in "Calling the script" section below for output.
arg: 'X | x', arg: 'X | x',
// the value is optional by default but we can make it required... // the value is optional by default but we can make it required...
@ -240,6 +253,9 @@ present in the command-line.
// setup an alias -r -> -required // setup an alias -r -> -required
//
// NOTE: aliases are used only for referencing, all naming is done via the
// actual option/command name.
'-r': '-required', '-r': '-required',
// a required option... // a required option...
@ -257,7 +273,7 @@ present in the command-line.
}, },
'-int': { '-i': {
doc: 'pass an integer value', doc: 'pass an integer value',
// NOTE: if not key is given the VALUE name is used as a key, so the // NOTE: if not key is given the VALUE name is used as a key, so the
@ -282,7 +298,7 @@ present in the command-line.
'-home': { '-home': {
doc: 'set home path', doc: 'set home path',
arg: 'HOME | home', arg: 'PATH | home_path',
// get the default value from the environment variable $HOME... // get the default value from the environment variable $HOME...
env: 'HOME', env: 'HOME',
@ -300,6 +316,10 @@ present in the command-line.
}, },
``` ```
For more info on available `.type` and `.collect` handlers see:
[`<option>.type`](#optiontype) and [`<option>.collect`](#optioncollect)
respectively.
### Commands ### Commands
@ -338,6 +358,11 @@ And for quick-n-dirty hacking stuff together, a shorthand (_not for production u
}, },
``` ```
Option's `.handler(..)` only sees the `args` that follow it in the command line,
thus anything it may expect/get from the arguments must follow it (in the manner
it expects), `argv.js` poses no restrictions on full or partial manual handling
of arguments by options/commands.
### Nested parsers ### Nested parsers
@ -364,6 +389,10 @@ exits, then the parent parser will pick up where it left.
Externally it is treated in exactly the same way as a normal _function_ handler, 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. essentially, the parent parser does not know the difference between the two.
As with [Active options/commands](#active-optionscommands) the nested
parser (essentially an active option itself) only sees the arguments that
follow it in the command-line and may have arbitrary expectations of them.
For more detail see the [Nested parsers](./ADVANCED.md#nested-parsers) section For more detail see the [Nested parsers](./ADVANCED.md#nested-parsers) section
in detailed docs. in detailed docs.
@ -471,8 +500,8 @@ Options:
--bool - if given set .bool to true --bool - if given set .bool to true
--value=X - set .x to X --value=X - set .x to X
(required value) (required value)
--int=INT - pass an integer value -i=INT - pass an integer value
--home=HOME - set home path --home=PATH - set home path
(env: $HOME) (env: $HOME)
-p, --push=ELEM - push elements to a .list -p, --push=ELEM - push elements to a .list
-c - command -c - command
@ -512,7 +541,7 @@ Parser {
required_option_given: true, required_option_given: true,
... ...
default: 'some value', default: 'some value',
home: '...' home_path: '...'
} }
``` ```
Notice the default values are set in the output above (output partially truncated Notice the default values are set in the output above (output partially truncated
@ -604,4 +633,4 @@ Copyright (c) 2016-2020, Alex A. Naanou,
All rights reserved. All rights reserved.
<!-- vim:set ts=4 sw=4 spell : --> <!-- vim:set ts=4 sw=4 spell : -->

View File

@ -266,6 +266,14 @@ function(attr, func){
// - isolate parsed from parser // - isolate parsed from parser
// - isolate option data from parser // - isolate option data from parser
// - ... // - ...
// XXX for this option:
// '-verbose': {
// env: 'VERBOSE',
// handler: function(){
// // ...
// } },
// need to be able to tell if -verbose was passed explicitly or not
// in the handler...
// XXX should -help work for any command? ..not just nested parsers? // XXX should -help work for any command? ..not just nested parsers?
// ...should we indicate which thinks have more "-help"?? // ...should we indicate which thinks have more "-help"??
var Parser = var Parser =

View File

@ -13,18 +13,23 @@ argv.Parser({
author: 'John Smith <j.smith@some-mail.com>', author: 'John Smith <j.smith@some-mail.com>',
license: 'BSD-3-Clause', license: 'BSD-3-Clause',
footer: 'Written by $AUTHOR ($VERSION / $LICENSE).', footer: 'Written by: $AUTHOR\nVersion: $VERSION / License: $LICENSE',
'-bool': { '-bool': {
doc: 'if given set .bool to true' }, doc: 'if given, set .bool to true' },
// option with a value... // option with a value...
'-value': { '-value': {
doc: 'set .x to X', doc: 'set .x to X',
// 'X' (VALUE) is used for -help while 'x' (key) is where the // 'X' (i.e. VALUE) is used to indicate the option value in -help
// value will be written... // while 'x' (key) is the attribute where the value will be written...
//
// NOTE: if no .arg is set option attribute name is used.
//
// See the first example in "Calling the script" section below for output.
arg: 'X | x', arg: 'X | x',
// the value is optional by default but we can make it required... // the value is optional by default but we can make it required...
@ -50,7 +55,7 @@ argv.Parser({
}, },
'-int': { '-i': {
doc: 'pass an integer value', doc: 'pass an integer value',
// NOTE: if not key is given the VALUE name is used as a key, so the // NOTE: if not key is given the VALUE name is used as a key, so the
@ -73,10 +78,9 @@ argv.Parser({
}, },
// XXX this is misbehaving -- setting true instead of $HOME
'-home': { '-home': {
doc: 'set home path', doc: 'set home path',
arg: 'HOME | home', arg: 'PATH | home_path',
// get the default value from the environment variable $HOME... // get the default value from the environment variable $HOME...
env: 'HOME', env: 'HOME',
@ -112,6 +116,7 @@ argv.Parser({
// ... // ...
}, },
'@nested': argv.Parser({ '@nested': argv.Parser({
// ... // ...
}).then(function(){ }).then(function(){
@ -120,6 +125,7 @@ argv.Parser({
'@bare': require('./bare').parser, '@bare': require('./bare').parser,
'-then': { '-then': {
handler: function(){ handler: function(){
return argv.THEN } }, return argv.THEN } },
@ -127,6 +133,7 @@ argv.Parser({
handler: function(){ handler: function(){
return argv.STOP } }, return argv.STOP } },
'-error': { '-error': {
handler: function(){ handler: function(){
throw argv.ParserError('something went wrong.') } }, throw argv.ParserError('something went wrong.') } },

View File

@ -1,6 +1,6 @@
{ {
"name": "ig-argv", "name": "ig-argv",
"version": "2.10.3", "version": "2.10.4",
"description": "simple argv parser", "description": "simple argv parser",
"main": "argv.js", "main": "argv.js",
"scripts": { "scripts": {