Skip to content

Commit

Permalink
feat: adds strictOptions() (#1738)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Feb 22, 2021
1 parent 0b519a4 commit 0f78930
Show file tree
Hide file tree
Showing 5 changed files with 5,393 additions and 21 deletions.
7 changes: 7 additions & 0 deletions docs/api.md
Expand Up @@ -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.

<a name="string"></a>.string(key)
------------

Expand Down
7 changes: 4 additions & 3 deletions lib/validation.ts
Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
15 changes: 15 additions & 0 deletions lib/yargs.ts
Expand Up @@ -170,6 +170,7 @@ export function Yargs (processArgs: string | string[] = [], cwd = process.cwd(),
groups,
strict,
strictCommands,
strictOptions,
completionCommand,
output,
exitError,
Expand Down Expand Up @@ -198,6 +199,7 @@ export function Yargs (processArgs: string | string[] = [], cwd = process.cwd(),
parsed: self.parsed,
strict,
strictCommands,
strictOptions,
completionCommand,
parseFn,
parseContext,
Expand Down Expand Up @@ -942,6 +944,14 @@ export function Yargs (processArgs: string | string[] = [], cwd = process.cwd(),
}
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('<object>', [config], arguments.length)
Expand Down Expand Up @@ -1371,6 +1381,8 @@ export function Yargs (processArgs: string | string[] = [], cwd = process.cwd(),
}
if (strict && !failedStrictCommands) {
validation.unknownArguments(argv, aliases, positionalMap, isDefaultCommand)
} else if (strictOptions) {
validation.unknownArguments(argv, aliases, {}, false, false)
}
validation.customChecks(argv, aliases)
validation.limitedChoices(argv)
Expand Down Expand Up @@ -1514,6 +1526,7 @@ export interface YargsInstance {
getParserConfiguration (): Configuration
getStrict (): boolean
getStrictCommands (): boolean
getStrictOptions (): boolean
getUsageInstance (): UsageInstance
getValidationInstance (): ValidationInstance
global (keys: string | string[], global?: boolean): YargsInstance
Expand Down Expand Up @@ -1567,6 +1580,7 @@ export interface YargsInstance {
skipValidation (keys: string | string[]): YargsInstance
strict (enable?: boolean): YargsInstance
strictCommands (enable?: boolean): YargsInstance
strictOptions (enable?: boolean): YargsInstance
string (key: string | string []): YargsInstance
terminalWidth (): number | null
updateStrings (obj: Dictionary<string>): YargsInstance
Expand Down Expand Up @@ -1697,6 +1711,7 @@ interface FrozenYargsInstance {
groups: Dictionary<string[]>
strict: boolean
strictCommands: boolean
strictOptions: boolean
completionCommand: string | null
output: string
exitError: YError | string | undefined | null
Expand Down

0 comments on commit 0f78930

Please sign in to comment.