diff --git a/docs/api.md b/docs/api.md
index c06f73985..96fb0304b 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -1518,6 +1518,13 @@ Similar to `.strict()`, except that it only applies to unrecognized commands. A
user can still provide arbitrary options, but unknown positional commands
will raise an error.
+.strictOptions([enabled=true])
+---------
+
+Similar to `.strict()`, except that it only applies to unrecognized options. A
+user can still provide arbitrary positional commands, but unknown options
+will raise an error.
+
.string(key)
------------
diff --git a/lib/validation.ts b/lib/validation.ts
index 809e4cde3..aefb623b4 100644
--- a/lib/validation.ts
+++ b/lib/validation.ts
@@ -114,7 +114,7 @@ export function validation (yargs: YargsInstance, usage: UsageInstance, y18n: Y1
}
// check for unknown arguments (strict-mode).
- self.unknownArguments = function unknownArguments (argv, aliases, positionalMap, isDefaultCommand) {
+ self.unknownArguments = function unknownArguments (argv, aliases, positionalMap, isDefaultCommand, checkPositionals = true) {
const commandKeys = yargs.getCommandInstance().getCommands()
const unknown: string[] = []
const currentContext = yargs.getContext()
@@ -129,7 +129,7 @@ export function validation (yargs: YargsInstance, usage: UsageInstance, y18n: Y1
}
})
- if ((currentContext.commands.length > 0) || (commandKeys.length > 0) || isDefaultCommand) {
+ if (checkPositionals && ((currentContext.commands.length > 0) || (commandKeys.length > 0) || isDefaultCommand)) {
argv._.slice(currentContext.commands.length).forEach((key) => {
if (commandKeys.indexOf('' + key) === -1) {
unknown.push('' + key)
@@ -428,7 +428,8 @@ export interface ValidationInstance {
argv: Arguments,
aliases: DetailedArguments['aliases'],
positionalMap: Dictionary,
- isDefaultCommand: boolean
+ isDefaultCommand: boolean,
+ checkPositionals?: boolean,
): void
unknownCommands(argv: Arguments): boolean
}
diff --git a/lib/yargs-factory.ts b/lib/yargs-factory.ts
index 1f5c1de29..d87087337 100644
--- a/lib/yargs-factory.ts
+++ b/lib/yargs-factory.ts
@@ -167,6 +167,7 @@ function Yargs (processArgs: string | string[] = [], cwd = shim.process.cwd(), p
groups,
strict,
strictCommands,
+ strictOptions,
completionCommand,
output,
exitError,
@@ -195,6 +196,7 @@ function Yargs (processArgs: string | string[] = [], cwd = shim.process.cwd(), p
parsed: self.parsed,
strict,
strictCommands,
+ strictOptions,
completionCommand,
parseFn,
parseContext,
@@ -950,6 +952,14 @@ function Yargs (processArgs: string | string[] = [], cwd = shim.process.cwd(), p
}
self.getStrictCommands = () => strictCommands
+ let strictOptions = false
+ self.strictOptions = function (enabled) {
+ argsert('[boolean]', [enabled], arguments.length)
+ strictOptions = enabled !== false
+ return self
+ }
+ self.getStrictOptions = () => strictOptions
+
let parserConfig: Configuration = {}
self.parserConfiguration = function parserConfiguration (config) {
argsert('