added required arg info in .usage info...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-12-10 15:33:36 +03:00
parent af65a118b2
commit 9d09eb220c
3 changed files with 49 additions and 23 deletions

View File

@ -51,25 +51,25 @@ This code is an evolution of that parser.
## Contents ## Contents
- [argv.js](#argvjs) - [argv.js](#argvjs)
- [Motivation](#motivation) - [Motivation](#motivation)
- [Features](#features) - [Features](#features)
- [Planned](#planned) - [Planned](#planned)
- [Contents](#contents) - [Contents](#contents)
- [Architecture](#architecture) - [Architecture](#architecture)
- [Basics and quick start](#basics-and-quick-start) - [Basics and quick start](#basics-and-quick-start)
- [Options in more detail](#options-in-more-detail) - [Options in more detail](#options-in-more-detail)
- [Help and metadata](#help-and-metadata) - [Help and metadata](#help-and-metadata)
- [Basic options](#basic-options) - [Basic options](#basic-options)
- [Commands](#commands) - [Commands](#commands)
- [Active options/commands](#active-optionscommands) - [Active options/commands](#active-optionscommands)
- [Nested parsers](#nested-parsers) - [Nested parsers](#nested-parsers)
- [Stopping](#stopping) - [Stopping](#stopping)
- [Error reporting](#error-reporting) - [Error reporting](#error-reporting)
- [Before parsing begins](#before-parsing-begins) - [Before parsing begins](#before-parsing-begins)
- [Handling the result](#handling-the-result) - [Handling the result](#handling-the-result)
- [Calling the script](#calling-the-script) - [Calling the script](#calling-the-script)
- [Advanced docs](#advanced-docs) - [Advanced docs](#advanced-docs)
- [License](#license) - [License](#license)
## Architecture ## Architecture
@ -528,7 +528,7 @@ The `<parser>` will call different sets of callbacks on different stop condition
This will create a parser that supports the following: This will create a parser that supports the following:
```shell_session ```shell_session
$ ./options.js --help $ ./options.js --help
Usage: options.js [OPTIONS] Usage: options.js -r [OPTIONS]
Example script options Example script options

30
argv.js
View File

@ -629,7 +629,7 @@ object.Constructor('Parser', {
Object.values(o).join(' ') Object.values(o).join(' ')
: o }), : o }),
license: getFromPackage('license'), license: getFromPackage('license'),
usage: '$SCRIPTNAME [OPTIONS]', usage: '$SCRIPTNAME $REQUIRED [OPTIONS]',
doc: undefined, doc: undefined,
examples: undefined, examples: undefined,
//footer: undefined, //footer: undefined,
@ -648,6 +648,14 @@ object.Constructor('Parser', {
: [a] }, : [a] },
// NOTE: if var value is not defined here we'll try and get it from // NOTE: if var value is not defined here we'll try and get it from
// parent... // parent...
// NOTE: this tries to be smart with spaces around $REQUIRED so
// as to keep it natural in the format string while removing
// the extra space when no value is present...
// 'script $REQUIRED args'
// can produce:
// 'script args'
// 'script x=VALUE args'
// depending on required options...
expandTextVars: function(text){ expandTextVars: function(text){
var that = this var that = this
var get = function(o, attr, dfl){ var get = function(o, attr, dfl){
@ -657,11 +665,29 @@ object.Constructor('Parser', {
|| (o.parent ? || (o.parent ?
get(o.parent, attr, dfl) get(o.parent, attr, dfl)
: dfl )} : dfl )}
// NOTE: this can get a bit expensive so we check if we need the
// value before generating it...
text = /\$REQUIRED/g.test(text) ?
// add required args and values...
text
.replace(/ ?\$REQUIRED ?/g,
that.requiredArguments()
.map(function([[key], arg]){
key = key.startsWith(COMMAND_PREFIX) ?
key.slice(COMMAND_PREFIX.length)
: key
return ' '
+(arg ?
key+'='+arg
: key) })
.join('')
+' ')
: text
return text return text
.replace(/\$AUTHOR/g, get(that, 'author', 'Author')) .replace(/\$AUTHOR/g, get(that, 'author', 'Author'))
.replace(/\$LICENSE/g, get(that, 'license', '-')) .replace(/\$LICENSE/g, get(that, 'license', '-'))
.replace(/\$VERSION/g, get(that, 'version', '0.0.0')) .replace(/\$VERSION/g, get(that, 'version', '0.0.0'))
.replace(/\$SCRIPTNAME/g, this.scriptName) }, .replace(/\$SCRIPTNAME/g, this.scriptName || 'SCRIPT') },
// NOTE: this will set .quiet to false... // NOTE: this will set .quiet to false...
'-h': '-help', '-h': '-help',

View File

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