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
- [argv.js](#argvjs)
- [Motivation](#motivation)
- [Features](#features)
- [Planned](#planned)
- [Contents](#contents)
- [Architecture](#architecture)
- [Basics and quick start](#basics-and-quick-start)
- [Options in more detail](#options-in-more-detail)
- [Help and metadata](#help-and-metadata)
- [Basic options](#basic-options)
- [Commands](#commands)
- [Active options/commands](#active-optionscommands)
- [Nested parsers](#nested-parsers)
- [Stopping](#stopping)
- [Error reporting](#error-reporting)
- [Before parsing begins](#before-parsing-begins)
- [Handling the result](#handling-the-result)
- [Calling the script](#calling-the-script)
- [Advanced docs](#advanced-docs)
- [License](#license)
- [Motivation](#motivation)
- [Features](#features)
- [Planned](#planned)
- [Contents](#contents)
- [Architecture](#architecture)
- [Basics and quick start](#basics-and-quick-start)
- [Options in more detail](#options-in-more-detail)
- [Help and metadata](#help-and-metadata)
- [Basic options](#basic-options)
- [Commands](#commands)
- [Active options/commands](#active-optionscommands)
- [Nested parsers](#nested-parsers)
- [Stopping](#stopping)
- [Error reporting](#error-reporting)
- [Before parsing begins](#before-parsing-begins)
- [Handling the result](#handling-the-result)
- [Calling the script](#calling-the-script)
- [Advanced docs](#advanced-docs)
- [License](#license)
## 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:
```shell_session
$ ./options.js --help
Usage: options.js [OPTIONS]
Usage: options.js -r [OPTIONS]
Example script options

30
argv.js
View File

@ -629,7 +629,7 @@ object.Constructor('Parser', {
Object.values(o).join(' ')
: o }),
license: getFromPackage('license'),
usage: '$SCRIPTNAME [OPTIONS]',
usage: '$SCRIPTNAME $REQUIRED [OPTIONS]',
doc: undefined,
examples: undefined,
//footer: undefined,
@ -648,6 +648,14 @@ object.Constructor('Parser', {
: [a] },
// NOTE: if var value is not defined here we'll try and get it from
// 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){
var that = this
var get = function(o, attr, dfl){
@ -657,11 +665,29 @@ object.Constructor('Parser', {
|| (o.parent ?
get(o.parent, attr, 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
.replace(/\$AUTHOR/g, get(that, 'author', 'Author'))
.replace(/\$LICENSE/g, get(that, 'license', '-'))
.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...
'-h': '-help',

View File

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