index.d.ts 27 KB

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