From 54451e04518adf34b09b7c2a7dfe297160c332e5 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sat, 18 Jul 2020 20:17:15 +0300 Subject: [PATCH] minor tweaks and docs... Signed-off-by: Alex A. Naanou --- README.md | 40 +++++++++++++++++++--------------------- argv.js | 7 +++++-- test.js | 2 +- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 743777c..a0ca913 100644 --- a/README.md +++ b/README.md @@ -64,11 +64,7 @@ This code is an evolution of that parser. - [`.license`](#license) - [`.examples`](#examples) - [`.footer`](#footer) - - [Help formatting](#help-formatting) - - [`.helpColumnOffset`](#helpcolumnoffset) - - [`.helpColumnPrefix`](#helpcolumnprefix) - - [`.helpArgumentSeparator`](#helpargumentseparator) - - [`.helpValueSeparator`](#helpvalueseparator) + - [More control over help...](#more-control-over-help) - [Nested parsers](#nested-parsers) - [Components and API](#components-and-api) - [`THEN`, `STOP` and `ERROR`](#then-stop-and-error) @@ -84,6 +80,7 @@ This code is an evolution of that parser. - [`.handleArgument(..)`](#handleargument) - [`.handleArgumentValue(..)`](#handleargumentvalue) - [`.handleErrorExit(..)`](#handleerrorexit) + - [More...](#more) - [License](#license-1) @@ -118,14 +115,16 @@ var parser = argv.Parser({ }) // run the parser... -if(__filename == require.main){ - parser(process.argv) } +__filename == require.main + && parser(process.argv) ``` Option definitions in a bit more detail ```javascript var parser = argv.Parser({ - // basic/quick-n-dirty option... + // XXX config... + + // basic quick-n-dirty option... '-b': '-basic', '-basic': function(opts, key, value){ // ... @@ -156,7 +155,7 @@ var parser = argv.Parser({ // default value (optional) default: 123, - + // required status (optional) required: false, @@ -184,6 +183,8 @@ var parser = argv.Parser({ }).then(function(){ // ... }), + + // ... }) ``` @@ -316,19 +317,10 @@ Aditional information. Default value: `undefined` -#### Help formatting -##### `.helpColumnOffset` -Default value: `3` +#### More control over help... -##### `.helpColumnPrefix` -Default value: `"- "` - -##### `.helpArgumentSeparator` -Default value: `", "` - -##### `.helpValueSeparator` -Default value: `" "` +For more info on help formatting see `.help*` attributes in the [source](./argv.js). ### Nested parsers @@ -346,7 +338,7 @@ Values that if returned by option/command handlers can control the parse flow. - `ERROR` – Stop parsing, call `.error(..)` callbacks and exit with an error. -### `Parser(..)` +### `Parser(..)` Construct a parser instance ``` @@ -354,6 +346,8 @@ Parser() -> ``` +See [`(..)`](#parser-1) for more info. + #### `.then(..)` Add callback to `then` "event". @@ -447,6 +441,10 @@ its head. ### `.handleErrorExit(..)` +### More... + +For more info see the [source](./argv.js). + ## License diff --git a/argv.js b/argv.js index 64e7c91..0405f08 100644 --- a/argv.js +++ b/argv.js @@ -226,9 +226,12 @@ object.Constructor('Parser', { splitOptions: true, optionPrefix: '-', commandPrefix: '@', + // NOTE: this must contain two goups the first is the prefix and the + // second must contain the option name... // NOTE: we only care about differentiating an option from a command // here by design... - optionInputPattern: /^--?(.*)$/, + optionInputPattern: /^([+-])\1?([^+-].*|)$/, + //optionInputPattern: /^(-)-?(.*)$/, //commandInputPattern: /^([.0-9a-zA-Z*].*)$/, commandInputPattern: /^([^-].*)$/, @@ -329,7 +332,7 @@ object.Constructor('Parser', { key = key.split(/=/).shift() // normalize option/command name... key = this.optionInputPattern.test(key) ? - key.replace(this.optionInputPattern, this.optionPrefix+'$1') + key.replace(this.optionInputPattern, this.optionPrefix+'$2') : !key.startsWith(this.commandPrefix) ? key.replace(this.commandInputPattern, this.commandPrefix+'$1') : key diff --git a/test.js b/test.js index 9fab9f9..a621416 100644 --- a/test.js +++ b/test.js @@ -113,7 +113,7 @@ var lang = module.lang = argv.Parser({ // handle both +x and -x - optionInputPattern: /^[+-][+-]?(.*)$/, + optionInputPattern: /^([+-])\1?([^+-].*|)$/, // XXX for testing, remove when done... '-echo': function(...args){