| 1 | // Type definitions for commander
|
|---|
| 2 | // Original definitions by: Alan Agius <https://github.com/alan-agius4>, Marcelo Dezem <https://github.com/mdezem>, vvakame <https://github.com/vvakame>, Jules Randolph <https://github.com/sveinburne>
|
|---|
| 3 |
|
|---|
| 4 | // Using method rather than property for method-signature-style, to document method overloads separately. Allow either.
|
|---|
| 5 | /* eslint-disable @typescript-eslint/method-signature-style */
|
|---|
| 6 | /* eslint-disable @typescript-eslint/no-explicit-any */
|
|---|
| 7 |
|
|---|
| 8 | export class CommanderError extends Error {
|
|---|
| 9 | code: string;
|
|---|
| 10 | exitCode: number;
|
|---|
| 11 | message: string;
|
|---|
| 12 | nestedError?: string;
|
|---|
| 13 |
|
|---|
| 14 | /**
|
|---|
| 15 | * Constructs the CommanderError class
|
|---|
| 16 | * @param exitCode - suggested exit code which could be used with process.exit
|
|---|
| 17 | * @param code - an id string representing the error
|
|---|
| 18 | * @param message - human-readable description of the error
|
|---|
| 19 | * @constructor
|
|---|
| 20 | */
|
|---|
| 21 | constructor(exitCode: number, code: string, message: string);
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | export class InvalidArgumentError extends CommanderError {
|
|---|
| 25 | /**
|
|---|
| 26 | * Constructs the InvalidArgumentError class
|
|---|
| 27 | * @param message - explanation of why argument is invalid
|
|---|
| 28 | * @constructor
|
|---|
| 29 | */
|
|---|
| 30 | constructor(message: string);
|
|---|
| 31 | }
|
|---|
| 32 | export { InvalidArgumentError as InvalidOptionArgumentError }; // deprecated old name
|
|---|
| 33 |
|
|---|
| 34 | export class Argument {
|
|---|
| 35 | description: string;
|
|---|
| 36 | required: boolean;
|
|---|
| 37 | variadic: boolean;
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * Initialize a new command argument with the given name and description.
|
|---|
| 41 | * The default is that the argument is required, and you can explicitly
|
|---|
| 42 | * indicate this with <> around the name. Put [] around the name for an optional argument.
|
|---|
| 43 | */
|
|---|
| 44 | constructor(arg: string, description?: string);
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * Return argument name.
|
|---|
| 48 | */
|
|---|
| 49 | name(): string;
|
|---|
| 50 |
|
|---|
| 51 | /**
|
|---|
| 52 | * Set the default value, and optionally supply the description to be displayed in the help.
|
|---|
| 53 | */
|
|---|
| 54 | default(value: unknown, description?: string): this;
|
|---|
| 55 |
|
|---|
| 56 | /**
|
|---|
| 57 | * Set the custom handler for processing CLI command arguments into argument values.
|
|---|
| 58 | */
|
|---|
| 59 | argParser<T>(fn: (value: string, previous: T) => T): this;
|
|---|
| 60 |
|
|---|
| 61 | /**
|
|---|
| 62 | * Only allow argument value to be one of choices.
|
|---|
| 63 | */
|
|---|
| 64 | choices(values: string[]): this;
|
|---|
| 65 |
|
|---|
| 66 | /**
|
|---|
| 67 | * Make option-argument required.
|
|---|
| 68 | */
|
|---|
| 69 | argRequired(): this;
|
|---|
| 70 |
|
|---|
| 71 | /**
|
|---|
| 72 | * Make option-argument optional.
|
|---|
| 73 | */
|
|---|
| 74 | argOptional(): this;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | export class Option {
|
|---|
| 78 | flags: string;
|
|---|
| 79 | description: string;
|
|---|
| 80 |
|
|---|
| 81 | required: boolean; // A value must be supplied when the option is specified.
|
|---|
| 82 | optional: boolean; // A value is optional when the option is specified.
|
|---|
| 83 | variadic: boolean;
|
|---|
| 84 | mandatory: boolean; // The option must have a value after parsing, which usually means it must be specified on command line.
|
|---|
| 85 | optionFlags: string;
|
|---|
| 86 | short?: string;
|
|---|
| 87 | long?: string;
|
|---|
| 88 | negate: boolean;
|
|---|
| 89 | defaultValue?: any;
|
|---|
| 90 | defaultValueDescription?: string;
|
|---|
| 91 | parseArg?: <T>(value: string, previous: T) => T;
|
|---|
| 92 | hidden: boolean;
|
|---|
| 93 | argChoices?: string[];
|
|---|
| 94 |
|
|---|
| 95 | constructor(flags: string, description?: string);
|
|---|
| 96 |
|
|---|
| 97 | /**
|
|---|
| 98 | * Set the default value, and optionally supply the description to be displayed in the help.
|
|---|
| 99 | */
|
|---|
| 100 | default(value: unknown, description?: string): this;
|
|---|
| 101 |
|
|---|
| 102 | /**
|
|---|
| 103 | * Set environment variable to check for option value.
|
|---|
| 104 | * Priority order of option values is default < env < cli
|
|---|
| 105 | */
|
|---|
| 106 | env(name: string): this;
|
|---|
| 107 |
|
|---|
| 108 | /**
|
|---|
| 109 | * Calculate the full description, including defaultValue etc.
|
|---|
| 110 | */
|
|---|
| 111 | fullDescription(): string;
|
|---|
| 112 |
|
|---|
| 113 | /**
|
|---|
| 114 | * Set the custom handler for processing CLI option arguments into option values.
|
|---|
| 115 | */
|
|---|
| 116 | argParser<T>(fn: (value: string, previous: T) => T): this;
|
|---|
| 117 |
|
|---|
| 118 | /**
|
|---|
| 119 | * Whether the option is mandatory and must have a value after parsing.
|
|---|
| 120 | */
|
|---|
| 121 | makeOptionMandatory(mandatory?: boolean): this;
|
|---|
| 122 |
|
|---|
| 123 | /**
|
|---|
| 124 | * Hide option in help.
|
|---|
| 125 | */
|
|---|
| 126 | hideHelp(hide?: boolean): this;
|
|---|
| 127 |
|
|---|
| 128 | /**
|
|---|
| 129 | * Only allow option value to be one of choices.
|
|---|
| 130 | */
|
|---|
| 131 | choices(values: string[]): this;
|
|---|
| 132 |
|
|---|
| 133 | /**
|
|---|
| 134 | * Return option name.
|
|---|
| 135 | */
|
|---|
| 136 | name(): string;
|
|---|
| 137 |
|
|---|
| 138 | /**
|
|---|
| 139 | * Return option name, in a camelcase format that can be used
|
|---|
| 140 | * as a object attribute key.
|
|---|
| 141 | */
|
|---|
| 142 | attributeName(): string;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | export class Help {
|
|---|
| 146 | /** output helpWidth, long lines are wrapped to fit */
|
|---|
| 147 | helpWidth?: number;
|
|---|
| 148 | sortSubcommands: boolean;
|
|---|
| 149 | sortOptions: boolean;
|
|---|
| 150 |
|
|---|
| 151 | constructor();
|
|---|
| 152 |
|
|---|
| 153 | /** Get the command term to show in the list of subcommands. */
|
|---|
| 154 | subcommandTerm(cmd: Command): string;
|
|---|
| 155 | /** Get the command description to show in the list of subcommands. */
|
|---|
| 156 | subcommandDescription(cmd: Command): string;
|
|---|
| 157 | /** Get the option term to show in the list of options. */
|
|---|
| 158 | optionTerm(option: Option): string;
|
|---|
| 159 | /** Get the option description to show in the list of options. */
|
|---|
| 160 | optionDescription(option: Option): string;
|
|---|
| 161 | /** Get the argument term to show in the list of arguments. */
|
|---|
| 162 | argumentTerm(argument: Argument): string;
|
|---|
| 163 | /** Get the argument description to show in the list of arguments. */
|
|---|
| 164 | argumentDescription(argument: Argument): string;
|
|---|
| 165 |
|
|---|
| 166 | /** Get the command usage to be displayed at the top of the built-in help. */
|
|---|
| 167 | commandUsage(cmd: Command): string;
|
|---|
| 168 | /** Get the description for the command. */
|
|---|
| 169 | commandDescription(cmd: Command): string;
|
|---|
| 170 |
|
|---|
| 171 | /** Get an array of the visible subcommands. Includes a placeholder for the implicit help command, if there is one. */
|
|---|
| 172 | visibleCommands(cmd: Command): Command[];
|
|---|
| 173 | /** Get an array of the visible options. Includes a placeholder for the implicit help option, if there is one. */
|
|---|
| 174 | visibleOptions(cmd: Command): Option[];
|
|---|
| 175 | /** Get an array of the arguments which have descriptions. */
|
|---|
| 176 | visibleArguments(cmd: Command): Argument[];
|
|---|
| 177 |
|
|---|
| 178 | /** Get the longest command term length. */
|
|---|
| 179 | longestSubcommandTermLength(cmd: Command, helper: Help): number;
|
|---|
| 180 | /** Get the longest option term length. */
|
|---|
| 181 | longestOptionTermLength(cmd: Command, helper: Help): number;
|
|---|
| 182 | /** Get the longest argument term length. */
|
|---|
| 183 | longestArgumentTermLength(cmd: Command, helper: Help): number;
|
|---|
| 184 | /** Calculate the pad width from the maximum term length. */
|
|---|
| 185 | padWidth(cmd: Command, helper: Help): number;
|
|---|
| 186 |
|
|---|
| 187 | /**
|
|---|
| 188 | * Wrap the given string to width characters per line, with lines after the first indented.
|
|---|
| 189 | * Do not wrap if insufficient room for wrapping (minColumnWidth), or string is manually formatted.
|
|---|
| 190 | */
|
|---|
| 191 | wrap(str: string, width: number, indent: number, minColumnWidth?: number): string;
|
|---|
| 192 |
|
|---|
| 193 | /** Generate the built-in help text. */
|
|---|
| 194 | formatHelp(cmd: Command, helper: Help): string;
|
|---|
| 195 | }
|
|---|
| 196 | export type HelpConfiguration = Partial<Help>;
|
|---|
| 197 |
|
|---|
| 198 | export interface ParseOptions {
|
|---|
| 199 | from: 'node' | 'electron' | 'user';
|
|---|
| 200 | }
|
|---|
| 201 | export interface HelpContext { // optional parameter for .help() and .outputHelp()
|
|---|
| 202 | error: boolean;
|
|---|
| 203 | }
|
|---|
| 204 | export interface AddHelpTextContext { // passed to text function used with .addHelpText()
|
|---|
| 205 | error: boolean;
|
|---|
| 206 | command: Command;
|
|---|
| 207 | }
|
|---|
| 208 | export interface OutputConfiguration {
|
|---|
| 209 | writeOut?(str: string): void;
|
|---|
| 210 | writeErr?(str: string): void;
|
|---|
| 211 | getOutHelpWidth?(): number;
|
|---|
| 212 | getErrHelpWidth?(): number;
|
|---|
| 213 | outputError?(str: string, write: (str: string) => void): void;
|
|---|
| 214 |
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | export type AddHelpTextPosition = 'beforeAll' | 'before' | 'after' | 'afterAll';
|
|---|
| 218 | export type HookEvent = 'preAction' | 'postAction';
|
|---|
| 219 | export type OptionValueSource = 'default' | 'env' | 'config' | 'cli';
|
|---|
| 220 |
|
|---|
| 221 | export interface OptionValues {
|
|---|
| 222 | [key: string]: any;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | export class Command {
|
|---|
| 226 | args: string[];
|
|---|
| 227 | processedArgs: any[];
|
|---|
| 228 | commands: Command[];
|
|---|
| 229 | parent: Command | null;
|
|---|
| 230 |
|
|---|
| 231 | constructor(name?: string);
|
|---|
| 232 |
|
|---|
| 233 | /**
|
|---|
| 234 | * Set the program version to `str`.
|
|---|
| 235 | *
|
|---|
| 236 | * This method auto-registers the "-V, --version" flag
|
|---|
| 237 | * which will print the version number when passed.
|
|---|
| 238 | *
|
|---|
| 239 | * You can optionally supply the flags and description to override the defaults.
|
|---|
| 240 | */
|
|---|
| 241 | version(str: string, flags?: string, description?: string): this;
|
|---|
| 242 |
|
|---|
| 243 | /**
|
|---|
| 244 | * Define a command, implemented using an action handler.
|
|---|
| 245 | *
|
|---|
| 246 | * @remarks
|
|---|
| 247 | * The command description is supplied using `.description`, not as a parameter to `.command`.
|
|---|
| 248 | *
|
|---|
| 249 | * @example
|
|---|
| 250 | * ```ts
|
|---|
| 251 | * program
|
|---|
| 252 | * .command('clone <source> [destination]')
|
|---|
| 253 | * .description('clone a repository into a newly created directory')
|
|---|
| 254 | * .action((source, destination) => {
|
|---|
| 255 | * console.log('clone command called');
|
|---|
| 256 | * });
|
|---|
| 257 | * ```
|
|---|
| 258 | *
|
|---|
| 259 | * @param nameAndArgs - command name and arguments, args are `<required>` or `[optional]` and last may also be `variadic...`
|
|---|
| 260 | * @param opts - configuration options
|
|---|
| 261 | * @returns new command
|
|---|
| 262 | */
|
|---|
| 263 | command(nameAndArgs: string, opts?: CommandOptions): ReturnType<this['createCommand']>;
|
|---|
| 264 | /**
|
|---|
| 265 | * Define a command, implemented in a separate executable file.
|
|---|
| 266 | *
|
|---|
| 267 | * @remarks
|
|---|
| 268 | * The command description is supplied as the second parameter to `.command`.
|
|---|
| 269 | *
|
|---|
| 270 | * @example
|
|---|
| 271 | * ```ts
|
|---|
| 272 | * program
|
|---|
| 273 | * .command('start <service>', 'start named service')
|
|---|
| 274 | * .command('stop [service]', 'stop named service, or all if no name supplied');
|
|---|
| 275 | * ```
|
|---|
| 276 | *
|
|---|
| 277 | * @param nameAndArgs - command name and arguments, args are `<required>` or `[optional]` and last may also be `variadic...`
|
|---|
| 278 | * @param description - description of executable command
|
|---|
| 279 | * @param opts - configuration options
|
|---|
| 280 | * @returns `this` command for chaining
|
|---|
| 281 | */
|
|---|
| 282 | command(nameAndArgs: string, description: string, opts?: ExecutableCommandOptions): this;
|
|---|
| 283 |
|
|---|
| 284 | /**
|
|---|
| 285 | * Factory routine to create a new unattached command.
|
|---|
| 286 | *
|
|---|
| 287 | * See .command() for creating an attached subcommand, which uses this routine to
|
|---|
| 288 | * create the command. You can override createCommand to customise subcommands.
|
|---|
| 289 | */
|
|---|
| 290 | createCommand(name?: string): Command;
|
|---|
| 291 |
|
|---|
| 292 | /**
|
|---|
| 293 | * Add a prepared subcommand.
|
|---|
| 294 | *
|
|---|
| 295 | * See .command() for creating an attached subcommand which inherits settings from its parent.
|
|---|
| 296 | *
|
|---|
| 297 | * @returns `this` command for chaining
|
|---|
| 298 | */
|
|---|
| 299 | addCommand(cmd: Command, opts?: CommandOptions): this;
|
|---|
| 300 |
|
|---|
| 301 | /**
|
|---|
| 302 | * Factory routine to create a new unattached argument.
|
|---|
| 303 | *
|
|---|
| 304 | * See .argument() for creating an attached argument, which uses this routine to
|
|---|
| 305 | * create the argument. You can override createArgument to return a custom argument.
|
|---|
| 306 | */
|
|---|
| 307 | createArgument(name: string, description?: string): Argument;
|
|---|
| 308 |
|
|---|
| 309 | /**
|
|---|
| 310 | * Define argument syntax for command.
|
|---|
| 311 | *
|
|---|
| 312 | * The default is that the argument is required, and you can explicitly
|
|---|
| 313 | * indicate this with <> around the name. Put [] around the name for an optional argument.
|
|---|
| 314 | *
|
|---|
| 315 | * @example
|
|---|
| 316 | * ```
|
|---|
| 317 | * program.argument('<input-file>');
|
|---|
| 318 | * program.argument('[output-file]');
|
|---|
| 319 | * ```
|
|---|
| 320 | *
|
|---|
| 321 | * @returns `this` command for chaining
|
|---|
| 322 | */
|
|---|
| 323 | argument<T>(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): this;
|
|---|
| 324 | argument(name: string, description?: string, defaultValue?: unknown): this;
|
|---|
| 325 |
|
|---|
| 326 | /**
|
|---|
| 327 | * Define argument syntax for command, adding a prepared argument.
|
|---|
| 328 | *
|
|---|
| 329 | * @returns `this` command for chaining
|
|---|
| 330 | */
|
|---|
| 331 | addArgument(arg: Argument): this;
|
|---|
| 332 |
|
|---|
| 333 | /**
|
|---|
| 334 | * Define argument syntax for command, adding multiple at once (without descriptions).
|
|---|
| 335 | *
|
|---|
| 336 | * See also .argument().
|
|---|
| 337 | *
|
|---|
| 338 | * @example
|
|---|
| 339 | * ```
|
|---|
| 340 | * program.arguments('<cmd> [env]');
|
|---|
| 341 | * ```
|
|---|
| 342 | *
|
|---|
| 343 | * @returns `this` command for chaining
|
|---|
| 344 | */
|
|---|
| 345 | arguments(names: string): this;
|
|---|
| 346 |
|
|---|
| 347 | /**
|
|---|
| 348 | * Override default decision whether to add implicit help command.
|
|---|
| 349 | *
|
|---|
| 350 | * @example
|
|---|
| 351 | * ```
|
|---|
| 352 | * addHelpCommand() // force on
|
|---|
| 353 | * addHelpCommand(false); // force off
|
|---|
| 354 | * addHelpCommand('help [cmd]', 'display help for [cmd]'); // force on with custom details
|
|---|
| 355 | * ```
|
|---|
| 356 | *
|
|---|
| 357 | * @returns `this` command for chaining
|
|---|
| 358 | */
|
|---|
| 359 | addHelpCommand(enableOrNameAndArgs?: string | boolean, description?: string): this;
|
|---|
| 360 |
|
|---|
| 361 | /**
|
|---|
| 362 | * Add hook for life cycle event.
|
|---|
| 363 | */
|
|---|
| 364 | hook(event: HookEvent, listener: (thisCommand: Command, actionCommand: Command) => void | Promise<void>): this;
|
|---|
| 365 |
|
|---|
| 366 | /**
|
|---|
| 367 | * Register callback to use as replacement for calling process.exit.
|
|---|
| 368 | */
|
|---|
| 369 | exitOverride(callback?: (err: CommanderError) => never|void): this;
|
|---|
| 370 |
|
|---|
| 371 | /**
|
|---|
| 372 | * You can customise the help with a subclass of Help by overriding createHelp,
|
|---|
| 373 | * or by overriding Help properties using configureHelp().
|
|---|
| 374 | */
|
|---|
| 375 | createHelp(): Help;
|
|---|
| 376 |
|
|---|
| 377 | /**
|
|---|
| 378 | * You can customise the help by overriding Help properties using configureHelp(),
|
|---|
| 379 | * or with a subclass of Help by overriding createHelp().
|
|---|
| 380 | */
|
|---|
| 381 | configureHelp(configuration: HelpConfiguration): this;
|
|---|
| 382 | /** Get configuration */
|
|---|
| 383 | configureHelp(): HelpConfiguration;
|
|---|
| 384 |
|
|---|
| 385 | /**
|
|---|
| 386 | * The default output goes to stdout and stderr. You can customise this for special
|
|---|
| 387 | * applications. You can also customise the display of errors by overriding outputError.
|
|---|
| 388 | *
|
|---|
| 389 | * The configuration properties are all functions:
|
|---|
| 390 | * ```
|
|---|
| 391 | * // functions to change where being written, stdout and stderr
|
|---|
| 392 | * writeOut(str)
|
|---|
| 393 | * writeErr(str)
|
|---|
| 394 | * // matching functions to specify width for wrapping help
|
|---|
| 395 | * getOutHelpWidth()
|
|---|
| 396 | * getErrHelpWidth()
|
|---|
| 397 | * // functions based on what is being written out
|
|---|
| 398 | * outputError(str, write) // used for displaying errors, and not used for displaying help
|
|---|
| 399 | * ```
|
|---|
| 400 | */
|
|---|
| 401 | configureOutput(configuration: OutputConfiguration): this;
|
|---|
| 402 | /** Get configuration */
|
|---|
| 403 | configureOutput(): OutputConfiguration;
|
|---|
| 404 |
|
|---|
| 405 | /**
|
|---|
| 406 | * Copy settings that are useful to have in common across root command and subcommands.
|
|---|
| 407 | *
|
|---|
| 408 | * (Used internally when adding a command using `.command()` so subcommands inherit parent settings.)
|
|---|
| 409 | */
|
|---|
| 410 | copyInheritedSettings(sourceCommand: Command): this;
|
|---|
| 411 |
|
|---|
| 412 | /**
|
|---|
| 413 | * Display the help or a custom message after an error occurs.
|
|---|
| 414 | */
|
|---|
| 415 | showHelpAfterError(displayHelp?: boolean | string): this;
|
|---|
| 416 |
|
|---|
| 417 | /**
|
|---|
| 418 | * Display suggestion of similar commands for unknown commands, or options for unknown options.
|
|---|
| 419 | */
|
|---|
| 420 | showSuggestionAfterError(displaySuggestion?: boolean): this;
|
|---|
| 421 |
|
|---|
| 422 | /**
|
|---|
| 423 | * Register callback `fn` for the command.
|
|---|
| 424 | *
|
|---|
| 425 | * @example
|
|---|
| 426 | * ```
|
|---|
| 427 | * program
|
|---|
| 428 | * .command('serve')
|
|---|
| 429 | * .description('start service')
|
|---|
| 430 | * .action(function() {
|
|---|
| 431 | * // do work here
|
|---|
| 432 | * });
|
|---|
| 433 | * ```
|
|---|
| 434 | *
|
|---|
| 435 | * @returns `this` command for chaining
|
|---|
| 436 | */
|
|---|
| 437 | action(fn: (...args: any[]) => void | Promise<void>): this;
|
|---|
| 438 |
|
|---|
| 439 | /**
|
|---|
| 440 | * Define option with `flags`, `description` and optional
|
|---|
| 441 | * coercion `fn`.
|
|---|
| 442 | *
|
|---|
| 443 | * The `flags` string contains the short and/or long flags,
|
|---|
| 444 | * separated by comma, a pipe or space. The following are all valid
|
|---|
| 445 | * all will output this way when `--help` is used.
|
|---|
| 446 | *
|
|---|
| 447 | * "-p, --pepper"
|
|---|
| 448 | * "-p|--pepper"
|
|---|
| 449 | * "-p --pepper"
|
|---|
| 450 | *
|
|---|
| 451 | * @example
|
|---|
| 452 | * ```
|
|---|
| 453 | * // simple boolean defaulting to false
|
|---|
| 454 | * program.option('-p, --pepper', 'add pepper');
|
|---|
| 455 | *
|
|---|
| 456 | * --pepper
|
|---|
| 457 | * program.pepper
|
|---|
| 458 | * // => Boolean
|
|---|
| 459 | *
|
|---|
| 460 | * // simple boolean defaulting to true
|
|---|
| 461 | * program.option('-C, --no-cheese', 'remove cheese');
|
|---|
| 462 | *
|
|---|
| 463 | * program.cheese
|
|---|
| 464 | * // => true
|
|---|
| 465 | *
|
|---|
| 466 | * --no-cheese
|
|---|
| 467 | * program.cheese
|
|---|
| 468 | * // => false
|
|---|
| 469 | *
|
|---|
| 470 | * // required argument
|
|---|
| 471 | * program.option('-C, --chdir <path>', 'change the working directory');
|
|---|
| 472 | *
|
|---|
| 473 | * --chdir /tmp
|
|---|
| 474 | * program.chdir
|
|---|
| 475 | * // => "/tmp"
|
|---|
| 476 | *
|
|---|
| 477 | * // optional argument
|
|---|
| 478 | * program.option('-c, --cheese [type]', 'add cheese [marble]');
|
|---|
| 479 | * ```
|
|---|
| 480 | *
|
|---|
| 481 | * @returns `this` command for chaining
|
|---|
| 482 | */
|
|---|
| 483 | option(flags: string, description?: string, defaultValue?: string | boolean): this;
|
|---|
| 484 | option<T>(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): this;
|
|---|
| 485 | /** @deprecated since v7, instead use choices or a custom function */
|
|---|
| 486 | option(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): this;
|
|---|
| 487 |
|
|---|
| 488 | /**
|
|---|
| 489 | * Define a required option, which must have a value after parsing. This usually means
|
|---|
| 490 | * the option must be specified on the command line. (Otherwise the same as .option().)
|
|---|
| 491 | *
|
|---|
| 492 | * The `flags` string contains the short and/or long flags, separated by comma, a pipe or space.
|
|---|
| 493 | */
|
|---|
| 494 | requiredOption(flags: string, description?: string, defaultValue?: string | boolean): this;
|
|---|
| 495 | requiredOption<T>(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): this;
|
|---|
| 496 | /** @deprecated since v7, instead use choices or a custom function */
|
|---|
| 497 | requiredOption(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): this;
|
|---|
| 498 |
|
|---|
| 499 | /**
|
|---|
| 500 | * Factory routine to create a new unattached option.
|
|---|
| 501 | *
|
|---|
| 502 | * See .option() for creating an attached option, which uses this routine to
|
|---|
| 503 | * create the option. You can override createOption to return a custom option.
|
|---|
| 504 | */
|
|---|
| 505 |
|
|---|
| 506 | createOption(flags: string, description?: string): Option;
|
|---|
| 507 |
|
|---|
| 508 | /**
|
|---|
| 509 | * Add a prepared Option.
|
|---|
| 510 | *
|
|---|
| 511 | * See .option() and .requiredOption() for creating and attaching an option in a single call.
|
|---|
| 512 | */
|
|---|
| 513 | addOption(option: Option): this;
|
|---|
| 514 |
|
|---|
| 515 | /**
|
|---|
| 516 | * Whether to store option values as properties on command object,
|
|---|
| 517 | * or store separately (specify false). In both cases the option values can be accessed using .opts().
|
|---|
| 518 | *
|
|---|
| 519 | * @returns `this` command for chaining
|
|---|
| 520 | */
|
|---|
| 521 | storeOptionsAsProperties<T extends OptionValues>(): this & T;
|
|---|
| 522 | storeOptionsAsProperties<T extends OptionValues>(storeAsProperties: true): this & T;
|
|---|
| 523 | storeOptionsAsProperties(storeAsProperties?: boolean): this;
|
|---|
| 524 |
|
|---|
| 525 | /**
|
|---|
| 526 | * Retrieve option value.
|
|---|
| 527 | */
|
|---|
| 528 | getOptionValue(key: string): any;
|
|---|
| 529 |
|
|---|
| 530 | /**
|
|---|
| 531 | * Store option value.
|
|---|
| 532 | */
|
|---|
| 533 | setOptionValue(key: string, value: unknown): this;
|
|---|
| 534 |
|
|---|
| 535 | /**
|
|---|
| 536 | * Store option value and where the value came from.
|
|---|
| 537 | */
|
|---|
| 538 | setOptionValueWithSource(key: string, value: unknown, source: OptionValueSource): this;
|
|---|
| 539 |
|
|---|
| 540 | /**
|
|---|
| 541 | * Retrieve option value source.
|
|---|
| 542 | */
|
|---|
| 543 | getOptionValueSource(key: string): OptionValueSource;
|
|---|
| 544 |
|
|---|
| 545 | /**
|
|---|
| 546 | * Alter parsing of short flags with optional values.
|
|---|
| 547 | *
|
|---|
| 548 | * @example
|
|---|
| 549 | * ```
|
|---|
| 550 | * // for `.option('-f,--flag [value]'):
|
|---|
| 551 | * .combineFlagAndOptionalValue(true) // `-f80` is treated like `--flag=80`, this is the default behaviour
|
|---|
| 552 | * .combineFlagAndOptionalValue(false) // `-fb` is treated like `-f -b`
|
|---|
| 553 | * ```
|
|---|
| 554 | *
|
|---|
| 555 | * @returns `this` command for chaining
|
|---|
| 556 | */
|
|---|
| 557 | combineFlagAndOptionalValue(combine?: boolean): this;
|
|---|
| 558 |
|
|---|
| 559 | /**
|
|---|
| 560 | * Allow unknown options on the command line.
|
|---|
| 561 | *
|
|---|
| 562 | * @returns `this` command for chaining
|
|---|
| 563 | */
|
|---|
| 564 | allowUnknownOption(allowUnknown?: boolean): this;
|
|---|
| 565 |
|
|---|
| 566 | /**
|
|---|
| 567 | * Allow excess command-arguments on the command line. Pass false to make excess arguments an error.
|
|---|
| 568 | *
|
|---|
| 569 | * @returns `this` command for chaining
|
|---|
| 570 | */
|
|---|
| 571 | allowExcessArguments(allowExcess?: boolean): this;
|
|---|
| 572 |
|
|---|
| 573 | /**
|
|---|
| 574 | * Enable positional options. Positional means global options are specified before subcommands which lets
|
|---|
| 575 | * subcommands reuse the same option names, and also enables subcommands to turn on passThroughOptions.
|
|---|
| 576 | *
|
|---|
| 577 | * The default behaviour is non-positional and global options may appear anywhere on the command line.
|
|---|
| 578 | *
|
|---|
| 579 | * @returns `this` command for chaining
|
|---|
| 580 | */
|
|---|
| 581 | enablePositionalOptions(positional?: boolean): this;
|
|---|
| 582 |
|
|---|
| 583 | /**
|
|---|
| 584 | * Pass through options that come after command-arguments rather than treat them as command-options,
|
|---|
| 585 | * so actual command-options come before command-arguments. Turning this on for a subcommand requires
|
|---|
| 586 | * positional options to have been enabled on the program (parent commands).
|
|---|
| 587 | *
|
|---|
| 588 | * The default behaviour is non-positional and options may appear before or after command-arguments.
|
|---|
| 589 | *
|
|---|
| 590 | * @returns `this` command for chaining
|
|---|
| 591 | */
|
|---|
| 592 | passThroughOptions(passThrough?: boolean): this;
|
|---|
| 593 |
|
|---|
| 594 | /**
|
|---|
| 595 | * Parse `argv`, setting options and invoking commands when defined.
|
|---|
| 596 | *
|
|---|
| 597 | * The default expectation is that the arguments are from node and have the application as argv[0]
|
|---|
| 598 | * and the script being run in argv[1], with user parameters after that.
|
|---|
| 599 | *
|
|---|
| 600 | * @example
|
|---|
| 601 | * ```
|
|---|
| 602 | * program.parse(process.argv);
|
|---|
| 603 | * program.parse(); // implicitly use process.argv and auto-detect node vs electron conventions
|
|---|
| 604 | * program.parse(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
|
|---|
| 605 | * ```
|
|---|
| 606 | *
|
|---|
| 607 | * @returns `this` command for chaining
|
|---|
| 608 | */
|
|---|
| 609 | parse(argv?: string[], options?: ParseOptions): this;
|
|---|
| 610 |
|
|---|
| 611 | /**
|
|---|
| 612 | * Parse `argv`, setting options and invoking commands when defined.
|
|---|
| 613 | *
|
|---|
| 614 | * Use parseAsync instead of parse if any of your action handlers are async. Returns a Promise.
|
|---|
| 615 | *
|
|---|
| 616 | * The default expectation is that the arguments are from node and have the application as argv[0]
|
|---|
| 617 | * and the script being run in argv[1], with user parameters after that.
|
|---|
| 618 | *
|
|---|
| 619 | * @example
|
|---|
| 620 | * ```
|
|---|
| 621 | * program.parseAsync(process.argv);
|
|---|
| 622 | * program.parseAsync(); // implicitly use process.argv and auto-detect node vs electron conventions
|
|---|
| 623 | * program.parseAsync(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
|
|---|
| 624 | * ```
|
|---|
| 625 | *
|
|---|
| 626 | * @returns Promise
|
|---|
| 627 | */
|
|---|
| 628 | parseAsync(argv?: string[], options?: ParseOptions): Promise<this>;
|
|---|
| 629 |
|
|---|
| 630 | /**
|
|---|
| 631 | * Parse options from `argv` removing known options,
|
|---|
| 632 | * and return argv split into operands and unknown arguments.
|
|---|
| 633 | *
|
|---|
| 634 | * argv => operands, unknown
|
|---|
| 635 | * --known kkk op => [op], []
|
|---|
| 636 | * op --known kkk => [op], []
|
|---|
| 637 | * sub --unknown uuu op => [sub], [--unknown uuu op]
|
|---|
| 638 | * sub -- --unknown uuu op => [sub --unknown uuu op], []
|
|---|
| 639 | */
|
|---|
| 640 | parseOptions(argv: string[]): ParseOptionsResult;
|
|---|
| 641 |
|
|---|
| 642 | /**
|
|---|
| 643 | * Return an object containing options as key-value pairs
|
|---|
| 644 | */
|
|---|
| 645 | opts<T extends OptionValues>(): T;
|
|---|
| 646 |
|
|---|
| 647 | /**
|
|---|
| 648 | * Set the description.
|
|---|
| 649 | *
|
|---|
| 650 | * @returns `this` command for chaining
|
|---|
| 651 | */
|
|---|
| 652 |
|
|---|
| 653 | description(str: string): this;
|
|---|
| 654 | /** @deprecated since v8, instead use .argument to add command argument with description */
|
|---|
| 655 | description(str: string, argsDescription: {[argName: string]: string}): this;
|
|---|
| 656 | /**
|
|---|
| 657 | * Get the description.
|
|---|
| 658 | */
|
|---|
| 659 | description(): string;
|
|---|
| 660 |
|
|---|
| 661 | /**
|
|---|
| 662 | * Set an alias for the command.
|
|---|
| 663 | *
|
|---|
| 664 | * You may call more than once to add multiple aliases. Only the first alias is shown in the auto-generated help.
|
|---|
| 665 | *
|
|---|
| 666 | * @returns `this` command for chaining
|
|---|
| 667 | */
|
|---|
| 668 | alias(alias: string): this;
|
|---|
| 669 | /**
|
|---|
| 670 | * Get alias for the command.
|
|---|
| 671 | */
|
|---|
| 672 | alias(): string;
|
|---|
| 673 |
|
|---|
| 674 | /**
|
|---|
| 675 | * Set aliases for the command.
|
|---|
| 676 | *
|
|---|
| 677 | * Only the first alias is shown in the auto-generated help.
|
|---|
| 678 | *
|
|---|
| 679 | * @returns `this` command for chaining
|
|---|
| 680 | */
|
|---|
| 681 | aliases(aliases: string[]): this;
|
|---|
| 682 | /**
|
|---|
| 683 | * Get aliases for the command.
|
|---|
| 684 | */
|
|---|
| 685 | aliases(): string[];
|
|---|
| 686 |
|
|---|
| 687 | /**
|
|---|
| 688 | * Set the command usage.
|
|---|
| 689 | *
|
|---|
| 690 | * @returns `this` command for chaining
|
|---|
| 691 | */
|
|---|
| 692 | usage(str: string): this;
|
|---|
| 693 | /**
|
|---|
| 694 | * Get the command usage.
|
|---|
| 695 | */
|
|---|
| 696 | usage(): string;
|
|---|
| 697 |
|
|---|
| 698 | /**
|
|---|
| 699 | * Set the name of the command.
|
|---|
| 700 | *
|
|---|
| 701 | * @returns `this` command for chaining
|
|---|
| 702 | */
|
|---|
| 703 | name(str: string): this;
|
|---|
| 704 | /**
|
|---|
| 705 | * Get the name of the command.
|
|---|
| 706 | */
|
|---|
| 707 | name(): string;
|
|---|
| 708 |
|
|---|
| 709 | /**
|
|---|
| 710 | * Output help information for this command.
|
|---|
| 711 | *
|
|---|
| 712 | * Outputs built-in help, and custom text added using `.addHelpText()`.
|
|---|
| 713 | *
|
|---|
| 714 | */
|
|---|
| 715 | outputHelp(context?: HelpContext): void;
|
|---|
| 716 | /** @deprecated since v7 */
|
|---|
| 717 | outputHelp(cb?: (str: string) => string): void;
|
|---|
| 718 |
|
|---|
| 719 | /**
|
|---|
| 720 | * Return command help documentation.
|
|---|
| 721 | */
|
|---|
| 722 | helpInformation(context?: HelpContext): string;
|
|---|
| 723 |
|
|---|
| 724 | /**
|
|---|
| 725 | * You can pass in flags and a description to override the help
|
|---|
| 726 | * flags and help description for your command. Pass in false
|
|---|
| 727 | * to disable the built-in help option.
|
|---|
| 728 | */
|
|---|
| 729 | helpOption(flags?: string | boolean, description?: string): this;
|
|---|
| 730 |
|
|---|
| 731 | /**
|
|---|
| 732 | * Output help information and exit.
|
|---|
| 733 | *
|
|---|
| 734 | * Outputs built-in help, and custom text added using `.addHelpText()`.
|
|---|
| 735 | */
|
|---|
| 736 | help(context?: HelpContext): never;
|
|---|
| 737 | /** @deprecated since v7 */
|
|---|
| 738 | help(cb?: (str: string) => string): never;
|
|---|
| 739 |
|
|---|
| 740 | /**
|
|---|
| 741 | * Add additional text to be displayed with the built-in help.
|
|---|
| 742 | *
|
|---|
| 743 | * Position is 'before' or 'after' to affect just this command,
|
|---|
| 744 | * and 'beforeAll' or 'afterAll' to affect this command and all its subcommands.
|
|---|
| 745 | */
|
|---|
| 746 | addHelpText(position: AddHelpTextPosition, text: string): this;
|
|---|
| 747 | addHelpText(position: AddHelpTextPosition, text: (context: AddHelpTextContext) => string): this;
|
|---|
| 748 |
|
|---|
| 749 | /**
|
|---|
| 750 | * Add a listener (callback) for when events occur. (Implemented using EventEmitter.)
|
|---|
| 751 | */
|
|---|
| 752 | on(event: string | symbol, listener: (...args: any[]) => void): this;
|
|---|
| 753 | }
|
|---|
| 754 |
|
|---|
| 755 | export interface CommandOptions {
|
|---|
| 756 | hidden?: boolean;
|
|---|
| 757 | isDefault?: boolean;
|
|---|
| 758 | /** @deprecated since v7, replaced by hidden */
|
|---|
| 759 | noHelp?: boolean;
|
|---|
| 760 | }
|
|---|
| 761 | export interface ExecutableCommandOptions extends CommandOptions {
|
|---|
| 762 | executableFile?: string;
|
|---|
| 763 | }
|
|---|
| 764 |
|
|---|
| 765 | export interface ParseOptionsResult {
|
|---|
| 766 | operands: string[];
|
|---|
| 767 | unknown: string[];
|
|---|
| 768 | }
|
|---|
| 769 |
|
|---|
| 770 | export function createCommand(name?: string): Command;
|
|---|
| 771 | export function createOption(flags: string, description?: string): Option;
|
|---|
| 772 | export function createArgument(name: string, description?: string): Argument;
|
|---|
| 773 |
|
|---|
| 774 | export const program: Command;
|
|---|