(
argv: Config.Argv,
options: Record<string, Options> & {
deprecationEntries?: DeprecatedOptions;
} = {},
rawArgv: Array<string> = [],
)
| 74 | }; |
| 75 | |
| 76 | export default function validateCLIOptions( |
| 77 | argv: Config.Argv, |
| 78 | options: Record<string, Options> & { |
| 79 | deprecationEntries?: DeprecatedOptions; |
| 80 | } = {}, |
| 81 | rawArgv: Array<string> = [], |
| 82 | ): boolean { |
| 83 | const yargsSpecialOptions = ['$0', '_', 'help', 'h']; |
| 84 | |
| 85 | const allowedOptions = Object.keys(options).reduce( |
| 86 | (acc, option) => |
| 87 | acc.add(option).add((options[option].alias as string) || option), |
| 88 | new Set(yargsSpecialOptions), |
| 89 | ); |
| 90 | |
| 91 | const deprecationEntries = options.deprecationEntries ?? {}; |
| 92 | const CLIDeprecations = Object.keys(deprecationEntries).reduce< |
| 93 | Record<string, DeprecatedOptionFunc> |
| 94 | >((acc, entry) => { |
| 95 | acc[entry] = deprecationEntries[entry]; |
| 96 | if (options[entry]) { |
| 97 | const alias = options[entry].alias as string; |
| 98 | if (alias) { |
| 99 | acc[alias] = deprecationEntries[entry]; |
| 100 | } |
| 101 | } |
| 102 | return acc; |
| 103 | }, {}); |
| 104 | const deprecations = new Set(Object.keys(CLIDeprecations)); |
| 105 | const deprecatedOptions = Object.keys(argv) |
| 106 | .filter(arg => deprecations.has(arg) && argv[arg] != null) |
| 107 | .map(arg => ({fatal: !allowedOptions.has(arg), name: arg})); |
| 108 | |
| 109 | if (deprecatedOptions.length > 0) { |
| 110 | validateDeprecatedOptions(deprecatedOptions, CLIDeprecations, argv); |
| 111 | } |
| 112 | |
| 113 | const unrecognizedOptions = Object.keys(argv).filter( |
| 114 | arg => |
| 115 | !allowedOptions.has(camelcase(arg, {locale: 'en-US'})) && |
| 116 | !allowedOptions.has(arg) && |
| 117 | (rawArgv.length === 0 || rawArgv.includes(arg)), |
| 118 | ); |
| 119 | |
| 120 | if (unrecognizedOptions.length > 0) { |
| 121 | throw createCLIValidationError(unrecognizedOptions, allowedOptions); |
| 122 | } |
| 123 | |
| 124 | return true; |
| 125 | } |
no test coverage detected