index.d.ts 27 KB

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