版博士V2.0程序
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

891 lines
27 KiB

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