From 9d09eb220cfc9a6ad259fe616cd22790137a3906 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Thu, 10 Dec 2020 15:33:36 +0300 Subject: [PATCH] added required arg info in .usage info... Signed-off-by: Alex A. Naanou --- README.md | 40 ++++++++++++++++++++-------------------- argv.js | 30 ++++++++++++++++++++++++++++-- package.json | 2 +- 3 files changed, 49 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index c4e8d63..82b49f7 100644 --- a/README.md +++ b/README.md @@ -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 `` 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 diff --git a/argv.js b/argv.js index dfd6944..a292a00 100644 --- a/argv.js +++ b/argv.js @@ -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', diff --git a/package.json b/package.json index 1a07ea6..b68aa48 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-argv", - "version": "2.15.6", + "version": "2.15.7", "description": "simple argv parser", "main": "argv.js", "scripts": {