From 7aef2fdfad73a6778ca6205c6df2d4b53ac96cc1 Mon Sep 17 00:00:00 2001 From: Antonin Stefanutti Date: Tue, 2 Jul 2024 08:30:47 +0200 Subject: [PATCH] Print CLI usage when unknown options are passed --- libs/nomnom.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/libs/nomnom.js b/libs/nomnom.js index ce866fb..95dbf3b 100644 --- a/libs/nomnom.js +++ b/libs/nomnom.js @@ -423,18 +423,27 @@ class ArgParser { } return str; } - opt(arg) { + opt(arg, value) { // get the specified opt for this parsed arg - var match = Opt({}); + var match; this.specs.forEach(function (opt) { if (opt.matches(arg)) { match = opt; } }); + if (!match) { + if (typeof value == "string") { + this.print("invalid argument '" + value + "'" + + " \n\n" + this.getUsage(), 1); + } else { + this.print("invalid option '" + (arg.length == 1 ? "-" : "--") + arg + "'" + + " \n\n" + this.getUsage(), 1); + } + } return match; } setOption(options, arg, value) { - var option = this.opt(arg); + var option = this.opt(arg, value); if (option.callback) { var message = option.callback(value);