// The following TSLint rules have been disabled: // unified-signatures: Because there is useful information in the argument names of the overloaded signatures // Convention: // Use 'union types' when: // - parameter types have similar signature type (i.e. 'string | ReadonlyArray') // - parameter names have the same semantic meaning (i.e. ['command', 'commands'] , ['key', 'keys']) // An example for not using 'union types' is the declaration of 'env' where `prefix` and `enable` parameters // have different semantics. On the other hand, in the declaration of 'usage', a `command: string` parameter // has the same semantic meaning with declaring an overload method by using `commands: ReadonlyArray`, // thus it's preferred to use `command: string | ReadonlyArray` // Use parameterless declaration instead of declaring all parameters optional, // when all parameters are optional and more than one import { Configuration, DetailedArguments } from "yargs-parser"; declare namespace yargs { type BuilderCallback = | ((args: Argv) => PromiseLike>) | ((args: Argv) => Argv) | ((args: Argv) => void); type ParserConfigurationOptions = Configuration & { /** Sort commands alphabetically. Default is `false` */ "sort-commands": boolean; }; /** * The type parameter `T` is the expected shape of the parsed options. * `Arguments` is those options plus `_` and `$0`, and an indexer falling * back to `unknown` for unknown options. * * For the return type / `argv` property, we create a mapped type over * `Arguments` to simplify the inferred type signature in client code. */ interface Argv { (args?: readonly string[] | string, cwd?: string): Argv; /** * Set key names as equivalent such that updates to a key will propagate to aliases and vice-versa. * * Optionally `.alias()` can take an object that maps keys to aliases. * Each key of this object should be the canonical version of the option, and each value should be a string or an array of strings. */ // Aliases for previously declared options can inherit the types of those options. alias( shortName: K1, longName: K2 | readonly K2[], ): Argv; alias( shortName: K2, longName: K1 | readonly K1[], ): Argv; alias(shortName: string | readonly string[], longName: string | readonly string[]): Argv; alias(aliases: { [shortName: string]: string | readonly string[] }): Argv; /** * Get the arguments as a plain old object. * * Arguments without a corresponding flag show up in the `argv._` array. * * The script name or node command is available at `argv.$0` similarly to how `$0` works in bash or perl. * * If `yargs` is executed in an environment that embeds node and there's no script name (e.g. Electron or nw.js), * it will ignore the first parameter since it expects it to be the script name. In order to override * this behavior, use `.parse(process.argv.slice(1))` instead of .argv and the first parameter won't be ignored. */ argv: | { [key in keyof Arguments as key | CamelCaseKey]: Arguments[key] } | Promise<{ [key in keyof Arguments as key | CamelCaseKey]: Arguments[key] }>; /** * Tell the parser to interpret `key` as an array. * If `.array('foo')` is set, `--foo foo bar` will be parsed as `['foo', 'bar']` rather than as `'foo'`. * Also, if you use the option multiple times all the values will be flattened in one array so `--foo foo --foo bar` will be parsed as `['foo', 'bar']` * * When the option is used with a positional, use `--` to tell `yargs` to stop adding values to the array. */ array(key: K | readonly K[]): Argv & { [key in K]: ToArray }>; array( key: K | readonly K[], ): Argv | undefined }>; /** * Interpret `key` as a boolean. If a non-flag option follows `key` in `process.argv`, that string won't get set as the value of `key`. * * `key` will default to `false`, unless a `default(key, undefined)` is explicitly set. * * If `key` is an array, interpret all the elements as booleans. */ boolean(key: K | readonly K[]): Argv & { [key in K]: boolean | undefined }>; boolean(key: K | readonly K[]): Argv; /** * Check that certain conditions are met in the provided arguments. * @param func Called with two arguments, the parsed `argv` hash and an array of options and their aliases. * If `func` throws or returns a non-truthy value, show the thrown error, usage information, and exit. * @param global Indicates whether `check()` should be enabled both at the top-level and for each sub-command. */ check(func: (argv: Arguments, aliases: { [alias: string]: string }) => any, global?: boolean): Argv; /** * Limit valid values for key to a predefined set of choices, given as an array or as an individual value. * If this method is called multiple times, all enumerated values will be merged together. * Choices are generally strings or numbers, and value matching is case-sensitive. * * Optionally `.choices()` can take an object that maps multiple keys to their choices. * * Choices can also be specified as choices in the object given to `option()`. */ choices( key: K, values: C, ): Argv & { [key in K]: C[number] | undefined }>; choices( key: K, values: C, ): Argv; choices( choices: C, ): Argv & { [key in keyof C]: C[key][number] | undefined }>; /** * Provide a synchronous function to coerce or transform the value(s) given on the command line for `key`. * * The coercion function should accept one argument, representing the parsed value from the command line, and should return a new value or throw an error. * The returned value will be used as the value for `key` (or one of its aliases) in `argv`. * * If the function throws, the error will be treated as a validation failure, delegating to either a custom `.fail()` handler or printing the error message in the console. * * Coercion will be applied to a value after all other modifications, such as `.normalize()`. * * Optionally `.coerce()` can take an object that maps several keys to their respective coercion function. * * You can also map the same function to several keys at one time. Just pass an array of keys as the first argument to `.coerce()`. * * If you are using dot-notion or arrays, .e.g., `user.email` and `user.password`, coercion will be applied to the final object that has been parsed */ coerce( key: K | readonly K[], func: (arg: any) => V, ): Argv & { [key in K]: V | undefined }>; coerce( key: K | readonly K[], func: (arg: any) => V, ): Argv; coerce any }>( opts: O, ): Argv & { [key in keyof O]: ReturnType | undefined }>; /** * Define the commands exposed by your application. * @param command Should be a string representing the command or an array of strings representing the command and its aliases. * @param description Use to provide a description for each command your application accepts (the values stored in `argv._`). * Set `description` to false to create a hidden command. Hidden commands don't show up in the help output and aren't available for completion. * @param [builder] Object to give hints about the options that your command accepts. * Can also be a function. This function is executed with a yargs instance, and can be used to provide advanced command specific help. * * Note that when `void` is returned, the handler `argv` object type will not include command-specific arguments. * @param [handler] Function, which will be executed with the parsed `argv` object. */ command( command: string | readonly string[], description: string, builder?: BuilderCallback, handler?: (args: ArgumentsCamelCase) => void | Promise, middlewares?: Array>, deprecated?: boolean | string, ): Argv; command( command: string | readonly string[], description: string, builder?: O, handler?: (args: ArgumentsCamelCase>) => void | Promise, middlewares?: Array>, deprecated?: boolean | string, ): Argv; command( // eslint-disable-line @definitelytyped/no-unnecessary-generics command: string | readonly string[], description: string, module: CommandModule, ): Argv; command( command: string | readonly string[], showInHelp: false, builder?: BuilderCallback, handler?: (args: ArgumentsCamelCase) => void | Promise, middlewares?: Array>, deprecated?: boolean | string, ): Argv; command( command: string | readonly string[], showInHelp: false, builder?: O, handler?: (args: ArgumentsCamelCase>) => void | Promise, ): Argv; command( // eslint-disable-line @definitelytyped/no-unnecessary-generics command: string | readonly string[], showInHelp: false, module: CommandModule, ): Argv; // eslint-disable-next-line @definitelytyped/no-unnecessary-generics command(module: CommandModule): Argv; // eslint-disable-next-line @definitelytyped/no-unnecessary-generics command(modules: Array>): Argv; // Advanced API /** Apply command modules from a directory relative to the module calling this method. */ commandDir(dir: string, opts?: RequireDirectoryOptions): Argv; /** * Enable bash/zsh-completion shortcuts for commands and options. * * If invoked without parameters, `.completion()` will make completion the command to output the completion script. * * @param [cmd] When present in `argv._`, will result in the `.bashrc` or `.zshrc` completion script being outputted. * To enable bash/zsh completions, concat the generated script to your `.bashrc` or `.bash_profile` (or `.zshrc` for zsh). * @param [description] Provide a description in your usage instructions for the command that generates the completion scripts. * @param [func] Rather than relying on yargs' default completion functionality, which shiver me timbers is pretty awesome, you can provide your own completion method. */ completion(): Argv; completion(cmd: string, func?: AsyncCompletionFunction): Argv; completion(cmd: string, func?: SyncCompletionFunction): Argv; completion(cmd: string, func?: PromiseCompletionFunction): Argv; completion(cmd: string, func?: FallbackCompletionFunction): Argv; completion(cmd: string, description?: string | false, func?: AsyncCompletionFunction): Argv; completion(cmd: string, description?: string | false, func?: SyncCompletionFunction): Argv; completion(cmd: string, description?: string | false, func?: PromiseCompletionFunction): Argv; completion(cmd: string, description?: string | false, func?: FallbackCompletionFunction): Argv; /** * Tells the parser that if the option specified by `key` is passed in, it should be interpreted as a path to a JSON config file. * The file is loaded and parsed, and its properties are set as arguments. * Because the file is loaded using Node's require(), the filename MUST end in `.json` to be interpreted correctly. * * If invoked without parameters, `.config()` will make --config the option to pass the JSON config file. * * @param [description] Provided to customize the config (`key`) option in the usage string. * @param [explicitConfigurationObject] An explicit configuration `object` */ config(): Argv; config( key: string | readonly string[], description?: string, parseFn?: (configPath: string) => object, ): Argv; config(key: string | readonly string[], parseFn: (configPath: string) => object): Argv; config(explicitConfigurationObject: object): Argv; /** * Given the key `x` is set, the key `y` must not be set. `y` can either be a single string or an array of argument names that `x` conflicts with. * * Optionally `.conflicts()` can accept an object specifying multiple conflicting keys. */ conflicts(key: string, value: string | readonly string[]): Argv; conflicts(conflicts: { [key: string]: string | readonly string[] }): Argv; /** * Interpret `key` as a boolean flag, but set its parsed value to the number of flag occurrences rather than `true` or `false`. Default value is thus `0`. */ count(key: K | readonly K[]): Argv & { [key in K]: number }>; count(key: K | readonly K[]): Argv; /** * Set `argv[key]` to `value` if no option was specified in `process.argv`. * * Optionally `.default()` can take an object that maps keys to default values. * * The default value can be a `function` which returns a value. The name of the function will be used in the usage string. * * Optionally, `description` can also be provided and will take precedence over displaying the value in the usage instructions. */ default(key: K, value: V, description?: string): Argv & { [key in K]: V }>; default(key: K, value: V, description?: string): Argv; default(defaults: D, description?: string): Argv & D>; /** * @deprecated since version 6.6.0 * Use '.demandCommand()' or '.demandOption()' instead */ demand(key: K | readonly K[], msg?: string | true): Argv>; demand(key: K | readonly K[], msg?: string | true): Argv; demand(key: string | readonly string[], required?: boolean): Argv; demand(positionals: number, msg: string): Argv; demand(positionals: number, required?: boolean): Argv; demand(positionals: number, max: number, msg?: string): Argv; /** * @param key If is a string, show the usage information and exit if key wasn't specified in `process.argv`. * If is an array, demand each element. * @param msg If string is given, it will be printed when the argument is missing, instead of the standard error message. * @param demand Controls whether the option is demanded; this is useful when using .options() to specify command line parameters. */ demandOption(key: K | readonly K[], msg?: string | true): Argv>; demandOption( key: K | readonly K[], msg?: string | true, ): Argv; demandOption(key: string | readonly string[], demand?: boolean): Argv; /** * Demand in context of commands. * You can demand a minimum and a maximum number a user can have within your program, as well as provide corresponding error messages if either of the demands is not met. */ demandCommand(): Argv; demandCommand(min: number, minMsg?: string): Argv; demandCommand(min: number, max?: number, minMsg?: string, maxMsg?: string): Argv; /** * Shows a [deprecated] notice in front of the option */ deprecateOption(option: string, msg?: string): Argv; /** * Describe a `key` for the generated usage information. * * Optionally `.describe()` can take an object that maps keys to descriptions. */ describe(key: string | readonly string[], description: string): Argv; describe(descriptions: { [key: string]: string }): Argv; /** Should yargs attempt to detect the os' locale? Defaults to `true`. */ detectLocale(detect: boolean): Argv; /** * Tell yargs to parse environment variables matching the given prefix and apply them to argv as though they were command line arguments. * * Use the "__" separator in the environment variable to indicate nested options. (e.g. prefix_nested__foo => nested.foo) * * If this method is called with no argument or with an empty string or with true, then all env vars will be applied to argv. * * Program arguments are defined in this order of precedence: * 1. Command line args * 2. Env vars * 3. Config file/objects * 4. Configured defaults * * Env var parsing is disabled by default, but you can also explicitly disable it by calling `.env(false)`, e.g. if you need to undo previous configuration. */ env(): Argv; env(prefix: string): Argv; env(enable: boolean): Argv; /** A message to print at the end of the usage instructions */ epilog(msg: string): Argv; /** A message to print at the end of the usage instructions */ epilogue(msg: string): Argv; /** * Give some example invocations of your program. * Inside `cmd`, the string `$0` will get interpolated to the current script name or node command for the present script similar to how `$0` works in bash or perl. * Examples will be printed out as part of the help message. */ example(command: string, description: string): Argv; example(command: ReadonlyArray<[string, string?]>): Argv; /** Manually indicate that the program should exit, and provide context about why we wanted to exit. Follows the behavior set by `.exitProcess().` */ exit(code: number, err: Error): void; /** * By default, yargs exits the process when the user passes a help flag, the user uses the `.version` functionality, validation fails, or the command handler fails. * Calling `.exitProcess(false)` disables this behavior, enabling further actions after yargs have been validated. */ exitProcess(enabled: boolean): Argv; /** * Method to execute when a failure occurs, rather than printing the failure message. * @param func Is called with the failure message that would have been printed, the Error instance originally thrown and yargs state when the failure occurred. */ fail(func: ((msg: string, err: Error, yargs: Argv) => any) | boolean): Argv; /** * Allows to programmatically get completion choices for any line. * @param args An array of the words in the command line to complete. * @param done The callback to be called with the resulting completions. */ getCompletion( args: readonly string[], done: (err: Error | null, completions: readonly string[]) => void, ): Argv; getCompletion(args: readonly string[], done?: never): Promise; /** * Returns a promise which resolves to a string containing the help text. */ getHelp(): Promise; /** * Indicate that an option (or group of options) should not be reset when a command is executed * * Options default to being global. */ global(key: string | readonly string[]): Argv; /** Given a key, or an array of keys, places options under an alternative heading when displaying usage instructions */ group(key: string | readonly string[], groupName: string): Argv; /** Hides a key from the generated usage information. Unless a `--show-hidden` option is also passed with `--help` (see `showHidden()`). */ hide(key: string): Argv; /** * Configure an (e.g. `--help`) and implicit command that displays the usage string and exits the process. * By default yargs enables help on the `--help` option. * * Note that any multi-char aliases (e.g. `help`) used for the help option will also be used for the implicit command. * If there are no multi-char aliases (e.g. `h`), then all single-char aliases will be used for the command. * * If invoked without parameters, `.help()` will use `--help` as the option and help as the implicit command to trigger help output. * * @param [description] Customizes the description of the help option in the usage string. * @param [enableExplicit] If `false` is provided, it will disable --help. */ help(): Argv; help(enableExplicit: boolean): Argv; help(option: string, enableExplicit: boolean): Argv; help(option: string, description?: string, enableExplicit?: boolean): Argv; /** * Given the key `x` is set, it is required that the key `y` is set. * y` can either be the name of an argument to imply, a number indicating the position of an argument or an array of multiple implications to associate with `x`. * * Optionally `.implies()` can accept an object specifying multiple implications. */ implies(key: string, value: string | readonly string[]): Argv; implies(implies: { [key: string]: string | readonly string[] }): Argv; /** * Return the locale that yargs is currently using. * * By default, yargs will auto-detect the operating system's locale so that yargs-generated help content will display in the user's language. */ locale(): string; /** * Override the auto-detected locale from the user's operating system with a static locale. * Note that the OS locale can be modified by setting/exporting the `LC_ALL` environment variable. */ locale(loc: string): Argv; /** * Define global middleware functions to be called first, in list order, for all cli command. * @param callbacks Can be a function or a list of functions. Each callback gets passed a reference to argv. * @param [applyBeforeValidation] Set to `true` to apply middleware before validation. This will execute the middleware prior to validation checks, but after parsing. */ middleware( callbacks: MiddlewareFunction | ReadonlyArray>, applyBeforeValidation?: boolean, ): Argv; /** * The number of arguments that should be consumed after a key. This can be a useful hint to prevent parsing ambiguity. * * Optionally `.nargs()` can take an object of `key`/`narg` pairs. */ nargs(key: string, count: number): Argv; nargs(nargs: { [key: string]: number }): Argv; /** The key provided represents a path and should have `path.normalize()` applied. */ normalize(key: K | readonly K[]): Argv & { [key in K]: ToString }>; normalize(key: K | readonly K[]): Argv; /** * Tell the parser to always interpret key as a number. * * If `key` is an array, all elements will be parsed as numbers. * * If the option is given on the command line without a value, `argv` will be populated with `undefined`. * * If the value given on the command line cannot be parsed as a number, `argv` will be populated with `NaN`. * * Note that decimals, hexadecimals, and scientific notation are all accepted. */ number(key: K | readonly K[]): Argv & { [key in K]: ToNumber }>; number(key: K | readonly K[]): Argv; /** * Method to execute when a command finishes successfully. * @param func Is called with the successful result of the command that finished. */ onFinishCommand(func: (result: any) => void): Argv; /** * This method can be used to make yargs aware of options that could exist. * You can also pass an opt object which can hold further customization, like `.alias()`, `.demandOption()` etc. for that option. */ option( key: K, options: O, ): Argv & { [key in K]: InferredOptionType } & Alias>; option( key: K, options: O, ): Argv } & Alias>; option( options: O, ): Argv & InferredOptionTypes & Alias>; /** * This method can be used to make yargs aware of options that could exist. * You can also pass an opt object which can hold further customization, like `.alias()`, `.demandOption()` etc. for that option. */ options( key: K, options: O, ): Argv & { [key in K]: InferredOptionType }>; options( key: K, options: O, ): Argv }>; options(options: O): Argv & InferredOptionTypes>; /** * Parse `args` instead of `process.argv`. Returns the `argv` object. `args` may either be a pre-processed argv array, or a raw argument string. * * Note: Providing a callback to parse() disables the `exitProcess` setting until after the callback is invoked. * @param [context] Provides a useful mechanism for passing state information to commands */ parse(): | { [key in keyof Arguments as key | CamelCaseKey]: Arguments[key] } | Promise<{ [key in keyof Arguments as key | CamelCaseKey]: Arguments[key] }>; parse( arg: string | readonly string[], context?: object, parseCallback?: ParseCallback, ): | { [key in keyof Arguments as key | CamelCaseKey]: Arguments[key] } | Promise<{ [key in keyof Arguments as key | CamelCaseKey]: Arguments[key] }>; parseSync(): { [key in keyof Arguments as key | CamelCaseKey]: Arguments[key] }; parseSync( arg: string | readonly string[], context?: object, parseCallback?: ParseCallback, ): { [key in keyof Arguments as key | CamelCaseKey]: Arguments[key] }; parseAsync(): Promise<{ [key in keyof Arguments as key | CamelCaseKey]: Arguments[key] }>; parseAsync( arg: string | readonly string[], context?: object, parseCallback?: ParseCallback, ): Promise<{ [key in keyof Arguments as key | CamelCaseKey]: Arguments[key] }>; /** * If the arguments have not been parsed, this property is `false`. * * If the arguments have been parsed, this contain detailed parsed arguments. */ parsed: DetailedArguments | false; /** Allows to configure advanced yargs features. */ parserConfiguration(configuration: Partial): Argv; /** * Similar to `config()`, indicates that yargs should interpret the object from the specified key in package.json as a configuration object. * @param [cwd] If provided, the package.json will be read from this location */ pkgConf(key: string | readonly string[], cwd?: string): Argv; /** * Allows you to configure a command's positional arguments with an API similar to `.option()`. * `.positional()` should be called in a command's builder function, and is not available on the top-level yargs instance. If so, it will throw an error. */ positional( key: K, opt: O, ): Argv & { [key in K]: InferredOptionType }>; positional( key: K, opt: O, ): Argv }>; /** Should yargs provide suggestions regarding similar commands if no matching command is found? */ recommendCommands(): Argv; /** * @deprecated since version 6.6.0 * Use '.demandCommand()' or '.demandOption()' instead */ require(key: K | readonly K[], msg?: string | true): Argv>; require(key: string, msg: string): Argv; require(key: string, required: boolean): Argv; require(keys: readonly number[], msg: string): Argv; require(keys: readonly number[], required: boolean): Argv; require(positionals: number, required: boolean): Argv; require(positionals: number, msg: string): Argv; /** * @deprecated since version 6.6.0 * Use '.demandCommand()' or '.demandOption()' instead */ required(key: K | readonly K[], msg?: string | true): Argv>; required(key: string, msg: string): Argv; required(key: string, required: boolean): Argv; required(keys: readonly number[], msg: string): Argv; required(keys: readonly number[], required: boolean): Argv; required(positionals: number, required: boolean): Argv; required(positionals: number, msg: string): Argv; requiresArg(key: string | readonly string[]): Argv; /** Set the name of your script ($0). Default is the base filename executed by node (`process.argv[1]`) */ scriptName($0: string): Argv; /** * Generate a bash completion script. * Users of your application can install this script in their `.bashrc`, and yargs will provide completion shortcuts for commands and options. */ showCompletionScript(): Argv; /** * Configure the `--show-hidden` option that displays the hidden keys (see `hide()`). * @param option If `boolean`, it enables/disables this option altogether. i.e. hidden keys will be permanently hidden if first argument is `false`. * If `string` it changes the key name ("--show-hidden"). * @param description Changes the default description ("Show hidden options") */ showHidden(option?: string | boolean): Argv; showHidden(option: string, description?: string): Argv; /** * Print the usage data using the console function consoleLevel for printing. * @param [consoleLevel='error'] */ showHelp(consoleLevel?: string): Argv; /** * Provide the usage data as a string. * @param printCallback a function with a single argument. */ showHelp(printCallback: (s: string) => void): Argv; /** * By default, yargs outputs a usage string if any error is detected. * Use the `.showHelpOnFail()` method to customize this behavior. * @param enable If `false`, the usage string is not output. * @param [message] Message that is output after the error message. */ showHelpOnFail(enable: boolean, message?: string): Argv; /** * Print the version data using the console function consoleLevel or the specified function. * @param [level='error'] */ showVersion(level?: "error" | "log" | ((message: string) => void)): Argv; /** Specifies either a single option key (string), or an array of options. If any of the options is present, yargs validation is skipped. */ skipValidation(key: string | readonly string[]): Argv; /** * Any command-line argument given that is not demanded, or does not have a corresponding description, will be reported as an error. * * Unrecognized commands will also be reported as errors. */ strict(): Argv; strict(enabled: boolean): Argv; /** * 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. */ strictCommands(): Argv; strictCommands(enabled: boolean): Argv; /** * Similar to `.strict()`, except that it only applies to unrecognized options. A * user can still provide arbitrary positional options, but unknown options * will raise an error. */ strictOptions(): Argv; strictOptions(enabled: boolean): Argv; /** * Tell the parser logic not to interpret `key` as a number or boolean. This can be useful if you need to preserve leading zeros in an input. * * If `key` is an array, interpret all the elements as strings. * * `.string('_')` will result in non-hyphenated arguments being interpreted as strings, regardless of whether they resemble numbers. */ string(key: K | readonly K[]): Argv & { [key in K]: ToString }>; string(key: K | readonly K[]): Argv; // Intended to be used with '.wrap()' terminalWidth(): number; updateLocale(obj: { [key: string]: string }): Argv; /** * Override the default strings used by yargs with the key/value pairs provided in obj * * If you explicitly specify a locale(), you should do so before calling `updateStrings()`. */ updateStrings(obj: { [key: string]: string }): Argv; /** * Set a usage message to show which commands to use. * Inside `message`, the string `$0` will get interpolated to the current script name or node command for the present script similar to how `$0` works in bash or perl. * * If the optional `description`/`builder`/`handler` are provided, `.usage()` acts an an alias for `.command()`. * This allows you to use `.usage()` to configure the default command that will be run as an entry-point to your application * and allows you to provide configuration for the positional arguments accepted by your program: */ usage(message: string): Argv; usage( command: string | readonly string[], description: string, builder?: (args: Argv) => Argv, handler?: (args: ArgumentsCamelCase) => void | Promise, ): Argv; usage( command: string | readonly string[], showInHelp: boolean, builder?: (args: Argv) => Argv, handler?: (args: ArgumentsCamelCase) => void | Promise, ): Argv; usage( command: string | readonly string[], description: string, builder?: O, handler?: (args: ArgumentsCamelCase>) => void | Promise, ): Argv; usage( command: string | readonly string[], showInHelp: boolean, builder?: O, handler?: (args: ArgumentsCamelCase>) => void | Promise, ): Argv; /** * Add an option (e.g. `--version`) that displays the version number (given by the version parameter) and exits the process. * By default yargs enables version for the `--version` option. * * If no arguments are passed to version (`.version()`), yargs will parse the package.json of your module and use its version value. * * If the boolean argument `false` is provided, it will disable `--version`. */ version(): Argv; version(version: string): Argv; version(enable: boolean): Argv; version(optionKey: string, version: string): Argv; version(optionKey: string, description: string, version: string): Argv; /** * Format usage output to wrap at columns many columns. * * By default wrap will be set to `Math.min(80, windowWidth)`. Use `.wrap(null)` to specify no column limit (no right-align). * Use `.wrap(yargs.terminalWidth())` to maximize the width of yargs' usage instructions. */ wrap(columns: number | null): Argv; } type Arguments = T & { /** Non-option arguments */ _: Array; /** The script name or node command */ $0: string; /** All remaining options */ [argName: string]: unknown; }; /** Arguments type, with camelcased keys */ type ArgumentsCamelCase = { [key in keyof T as key | CamelCaseKey]: T[key] } & { /** Non-option arguments */ _: Array; /** The script name or node command */ $0: string; /** All remaining options */ [argName: string]: unknown; }; interface RequireDirectoryOptions { /** Look for command modules in all subdirectories and apply them as a flattened (non-hierarchical) list. */ recurse?: boolean | undefined; /** The types of files to look for when requiring command modules. */ extensions?: readonly string[] | undefined; /** * A synchronous function called for each command module encountered. * Accepts `commandObject`, `pathToFile`, and `filename` as arguments. * Returns `commandObject` to include the command; any falsy value to exclude/skip it. */ visit?: ((commandObject: any, pathToFile?: string, filename?: string) => any) | undefined; /** Whitelist certain modules */ include?: RegExp | ((pathToFile: string) => boolean) | undefined; /** Blacklist certain modules. */ exclude?: RegExp | ((pathToFile: string) => boolean) | undefined; } interface Options { /** string or array of strings, alias(es) for the canonical option key, see `alias()` */ alias?: string | readonly string[] | undefined; /** boolean, interpret option as an array, see `array()` */ array?: boolean | undefined; /** boolean, interpret option as a boolean flag, see `boolean()` */ boolean?: boolean | undefined; /** value or array of values, limit valid option arguments to a predefined set, see `choices()` */ choices?: Choices | undefined; /** function, coerce or transform parsed command line values into another value, see `coerce()` */ coerce?: ((arg: any) => any) | undefined; /** boolean, interpret option as a path to a JSON config file, see `config()` */ config?: boolean | undefined; /** function, provide a custom config parsing function, see `config()` */ configParser?: ((configPath: string) => object) | undefined; /** string or object, require certain keys not to be set, see `conflicts()` */ conflicts?: string | readonly string[] | { [key: string]: string | readonly string[] } | undefined; /** boolean, interpret option as a count of boolean flags, see `count()` */ count?: boolean | undefined; /** value, set a default value for the option, see `default()` */ default?: any; /** string, use this description for the default value in help content, see `default()` */ defaultDescription?: string | undefined; /** * @deprecated since version 6.6.0 * Use 'demandOption' instead */ demand?: boolean | string | undefined; /** boolean or string, mark the argument as deprecated, see `deprecateOption()` */ deprecate?: boolean | string | undefined; /** boolean or string, mark the argument as deprecated, see `deprecateOption()` */ deprecated?: boolean | string | undefined; /** boolean or string, demand the option be given, with optional error message, see `demandOption()` */ demandOption?: boolean | string | undefined; /** string, the option description for help content, see `describe()` */ desc?: string | undefined; /** string, the option description for help content, see `describe()` */ describe?: string | undefined; /** string, the option description for help content, see `describe()` */ description?: string | undefined; /** boolean, indicate that this key should not be reset when a command is invoked, see `global()` */ global?: boolean | undefined; /** string, when displaying usage instructions place the option under an alternative group heading, see `group()` */ group?: string | undefined; /** don't display option in help output. */ hidden?: boolean | undefined; /** string or object, require certain keys to be set, see `implies()` */ implies?: string | readonly string[] | { [key: string]: string | readonly string[] } | undefined; /** number, specify how many arguments should be consumed for the option, see `nargs()` */ nargs?: number | undefined; /** boolean, apply path.normalize() to the option, see `normalize()` */ normalize?: boolean | undefined; /** boolean, interpret option as a number, `number()` */ number?: boolean | undefined; /** * @deprecated since version 6.6.0 * Use 'demandOption' instead */ require?: boolean | string | undefined; /** * @deprecated since version 6.6.0 * Use 'demandOption' instead */ required?: boolean | string | undefined; /** boolean, require the option be specified with a value, see `requiresArg()` */ requiresArg?: boolean | undefined; /** boolean, skips validation if the option is present, see `skipValidation()` */ skipValidation?: boolean | undefined; /** boolean, interpret option as a string, see `string()` */ string?: boolean | undefined; type?: "array" | "count" | PositionalOptionsType | undefined; } interface PositionalOptions { /** string or array of strings, see `alias()` */ alias?: string | readonly string[] | undefined; /** boolean, interpret option as an array, see `array()` */ array?: boolean | undefined; /** value or array of values, limit valid option arguments to a predefined set, see `choices()` */ choices?: Choices | undefined; /** function, coerce or transform parsed command line values into another value, see `coerce()` */ coerce?: ((arg: any) => any) | undefined; /** string or object, require certain keys not to be set, see `conflicts()` */ conflicts?: string | readonly string[] | { [key: string]: string | readonly string[] } | undefined; /** value, set a default value for the option, see `default()` */ default?: any; /** boolean or string, demand the option be given, with optional error message, see `demandOption()` */ demandOption?: boolean | string | undefined; /** string, the option description for help content, see `describe()` */ desc?: string | undefined; /** string, the option description for help content, see `describe()` */ describe?: string | undefined; /** string, the option description for help content, see `describe()` */ description?: string | undefined; /** string or object, require certain keys to be set, see `implies()` */ implies?: string | readonly string[] | { [key: string]: string | readonly string[] } | undefined; /** boolean, apply path.normalize() to the option, see normalize() */ normalize?: boolean | undefined; type?: PositionalOptionsType | undefined; } // not implemented: yargs camelizes '_', but only if there's a '-' in the arg name // not implemented: yargs decamelizes (converts fooBar to foo-bar) /** Convert literal string types like 'foo-bar' to 'FooBar' */ type PascalCase = string extends S ? string : S extends `${infer T}-${infer U}` ? `${Capitalize}${PascalCase}` : Capitalize; /** Convert literal string types like 'foo-bar' to 'fooBar' */ type CamelCase = string extends S ? string : S extends `${infer T}-${infer U}` ? `${T}${PascalCase}` : S; /** Convert literal string types like 'foo-bar' to 'fooBar', allowing all `PropertyKey` types */ type CamelCaseKey = K extends string ? Exclude, ""> : K; /** Remove keys K in T */ type Omit = { [key in Exclude]: T[key] }; /** Remove undefined as a possible value for keys K in T */ type Defined = Omit & { [key in K]: Exclude }; /** Convert T to T[] and T | undefined to T[] | undefined */ type ToArray = Array> | Extract; /** Gives string[] if T is an array type, otherwise string. Preserves | undefined. */ type ToString = (Exclude extends any[] ? string[] : string) | Extract; /** Gives number[] if T is an array type, otherwise number. Preserves | undefined. */ type ToNumber = (Exclude extends any[] ? number[] : number) | Extract; // prettier-ignore type InferredOptionType = // Handle special cases first O extends ({ coerce: (arg: any) => infer T }) ? IsRequiredOrHasDefault extends true ? T : T | undefined : O extends ( | { type: "count"; default: infer D } | { count: true; default: infer D } ) ? number | Exclude : O extends ( | { type: "count" } | { count: true } ) ? number // Try to infer type with InferredOptionTypePrimitive : IsUnknown> extends false ? InferredOptionTypePrimitive // Use the type of `default` as the last resort : O extends ({ default: infer D }) ? Exclude : unknown; type Alias = O extends { alias: infer T } ? T extends Exclude ? { [key in T]: InferredOptionType } : {} : {}; // prettier-ignore type IsRequiredOrHasDefault = O extends ( | { required: string | true } | { require: string | true } | { demand: string | true } | { demandOption: string | true } | { default: {} } ) ? true : false; type IsAny = 0 extends (1 & T) ? true : false; // prettier-ignore type IsUnknown = IsAny extends true ? false : unknown extends T ? true : false; // prettier-ignore type InferredOptionTypePrimitive = O extends { default: infer D } ? IsRequiredOrHasDefault extends true ? InferredOptionTypeInner | Exclude : InferredOptionTypeInner | D : IsRequiredOrHasDefault extends true ? InferredOptionTypeInner : InferredOptionTypeInner | undefined; // prettier-ignore type InferredOptionTypeInner = O extends { type: "array"; choices: ReadonlyArray } ? C[] : O extends { type: "array"; string: true } ? string[] : O extends { type: "array"; number: true } ? number[] : O extends { type: "array"; normalize: true } ? string[] : O extends { array: true; choices: ReadonlyArray } ? C[] : O extends { array: true; type: "string" } ? string[] : O extends { array: true; type: "number" } ? number[] : O extends { array: true; string: true } ? string[] : O extends { array: true; number: true } ? number[] : O extends { array: true; normalize: true } ? string[] : O extends { choices: ReadonlyArray } ? C : O extends { type: "array" } ? Array : O extends { type: "boolean" } ? boolean : O extends { type: "number" } ? number : O extends { type: "string" } ? string : O extends { array: true } ? Array : O extends { boolean: true } ? boolean : O extends { number: true } ? number : O extends { string: true } ? string : O extends { normalize: true } ? string : unknown; type InferredOptionTypes = { [key in keyof O]: InferredOptionType }; interface CommandModule { /** array of strings (or a single string) representing aliases of `exports.command`, positional args defined in an alias are ignored */ aliases?: readonly string[] | string | undefined; /** object declaring the options the command accepts, or a function accepting and returning a yargs instance */ builder?: CommandBuilder | undefined; /** string (or array of strings) that executes this command when given on the command line, first string may contain positional args */ command?: readonly string[] | string | undefined; /** boolean (or string) to show deprecation notice */ deprecated?: boolean | string | undefined; /** string used as the description for the command in help text, use `false` for a hidden command */ describe?: string | false | undefined; /** a function which will be passed the parsed argv. */ handler: (args: ArgumentsCamelCase) => void | Promise; } type ParseCallback = ( err: Error | undefined, argv: ArgumentsCamelCase, output: string, ) => void | Promise; type CommandBuilder = | { [key: string]: Options } | ((args: Argv) => Argv) | ((args: Argv) => PromiseLike>); type SyncCompletionFunction = (current: string, argv: any) => string[]; type AsyncCompletionFunction = ( current: string, argv: any, done: (completion: readonly string[]) => void, ) => void; type PromiseCompletionFunction = (current: string, argv: any) => Promise; type FallbackCompletionFunction = ( current: string, argv: any, completionFilter: (onCompleted?: CompletionCallback) => any, done: (completions: string[]) => any, ) => void; type MiddlewareFunction = (args: ArgumentsCamelCase) => void | Promise; type Choices = ReadonlyArray; type PositionalOptionsType = "boolean" | "number" | "string"; type CompletionCallback = (err: Error | null, completions: string[] | undefined) => void; } declare var yargs: yargs.Argv; export = yargs;