args
Advanced tools
Comparing version 2.5.0 to 2.6.0
56
index.js
@@ -262,2 +262,37 @@ 'use strict'; | ||
Object.assign(options, details); | ||
} else { | ||
// Unknown Option | ||
const availableOptions = [].concat( | ||
...this.details.options.map(opt => opt.usage) | ||
); | ||
const suggestOption = stringSimilarity.findBestMatch( | ||
option, | ||
availableOptions | ||
); | ||
process.stdout.write(`The option "${option}" is unknown.`); | ||
if (suggestOption.bestMatch.rating >= 0.5) { | ||
process.stdout.write(' Did you mean the following one?\n'); | ||
const suggestion = this.details.options.filter(item => { | ||
for (const flag of item.usage) { | ||
if (flag === suggestOption.bestMatch.target) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}); | ||
process.stdout.write( | ||
this.generateDetails(suggestion)[0].trim() + '\n' | ||
); | ||
// eslint-disable-next-line unicorn/no-process-exit | ||
process.exit(); | ||
} else { | ||
process.stdout.write(` Here's a list of all available options: \n`); | ||
this.showHelp(); | ||
} | ||
} | ||
@@ -289,3 +324,3 @@ } | ||
// Get all properties of kind from global scope | ||
const items = this.details[kind]; | ||
const items = typeof kind === 'string' ? this.details[kind] : kind; | ||
const parts = []; | ||
@@ -526,3 +561,2 @@ const isCmd = kind === 'commands'; | ||
const optionList = this.getOptions(); | ||
const unknownSubcommand = !defined && subCommand; | ||
@@ -541,21 +575,5 @@ Object.assign(args, this.raw); | ||
if (unknownSubcommand) { | ||
const availableSubcommands = this.details.commands.map(sub => sub.usage); | ||
const suggestSubcommand = stringSimilarity.findBestMatch( | ||
subCommand, | ||
availableSubcommands | ||
); | ||
console.log(`\nUnknown Subcommand ${this.printMainColor(subCommand)}`); | ||
if (suggestSubcommand.bestMatch.rating >= 0.5) { | ||
// 0.5 rating will catch small typos, e.g. import => omport | ||
console.log( | ||
`Did you mean ${this.printMainColor(suggestSubcommand.bestMatch.target)}?` | ||
); | ||
} | ||
} | ||
// Show usage information if "help" or "h" option was used | ||
// And respect the option related to it | ||
if ((this.config.help && helpTriggered) || unknownSubcommand) { | ||
if (this.config.help && helpTriggered) { | ||
this.showHelp(); | ||
@@ -562,0 +580,0 @@ } |
{ | ||
"name": "args", | ||
"version": "2.5.0", | ||
"version": "2.6.0", | ||
"description": "Minimal toolkit for building CLIs", | ||
@@ -5,0 +5,0 @@ "files": [ |
@@ -14,4 +14,4 @@ # args | ||
- Clean [syntax](#usage) for defining options and commands | ||
- The core only contains a [few hundred lines](src/index.js) of code (even after transpiling) | ||
- Easily [retrieve](#usage) values of options | ||
- Automatically suggests a similar option, if the user entered an unknown one | ||
@@ -18,0 +18,0 @@ ## Usage |
28277
531