command.d.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /// <reference types="node" />
  2. import { Dictionary } from './common-types';
  3. import { Middleware } from './middleware';
  4. import { Positional } from './parse-command';
  5. import { RequireDirectoryOptions } from 'require-directory';
  6. import { UsageInstance } from './usage';
  7. import { ValidationInstance } from './validation';
  8. import { YargsInstance, Options, OptionDefinition, Context, Arguments, DetailedArguments } from './yargs';
  9. export declare function command(yargs: YargsInstance, usage: UsageInstance, validation: ValidationInstance, globalMiddleware?: Middleware[]): CommandInstance;
  10. /** Instance of the command module. */
  11. export interface CommandInstance {
  12. addDirectory(dir: string, context: Context, req: NodeRequireFunction, callerFile: string, opts?: RequireDirectoryOptions<any>): void;
  13. addHandler(handler: CommandHandlerDefinition): void;
  14. addHandler(cmd: string | string[], description: CommandHandler['description'], builder?: CommandBuilderDefinition | CommandBuilder, handler?: CommandHandlerCallback, commandMiddleware?: Middleware[], deprecated?: boolean): void;
  15. cmdToParseOptions(cmdString: string): Positionals;
  16. freeze(): void;
  17. getCommandHandlers(): Dictionary<CommandHandler>;
  18. getCommands(): string[];
  19. hasDefaultCommand(): boolean;
  20. reset(): CommandInstance;
  21. runCommand(command: string | null, yargs: YargsInstance, parsed: DetailedArguments, commandIndex?: number): Arguments | Promise<Arguments>;
  22. runDefaultBuilderOn(yargs: YargsInstance): void;
  23. unfreeze(): void;
  24. }
  25. export interface CommandHandlerDefinition extends Partial<Pick<CommandHandler, 'deprecated' | 'description' | 'handler' | 'middlewares'>> {
  26. aliases?: string[];
  27. builder?: CommandBuilder | CommandBuilderDefinition;
  28. command?: string | string[];
  29. desc?: CommandHandler['description'];
  30. describe?: CommandHandler['description'];
  31. }
  32. export declare function isCommandHandlerDefinition(cmd: string | string[] | CommandHandlerDefinition): cmd is CommandHandlerDefinition;
  33. export interface CommandBuilderDefinition {
  34. builder?: CommandBuilder;
  35. deprecated?: boolean;
  36. handler: CommandHandlerCallback;
  37. middlewares?: Middleware[];
  38. }
  39. export declare function isCommandBuilderDefinition(builder?: CommandBuilder | CommandBuilderDefinition): builder is CommandBuilderDefinition;
  40. export interface CommandHandlerCallback {
  41. (argv: Arguments): any;
  42. }
  43. export interface CommandHandler {
  44. builder: CommandBuilder;
  45. demanded: Positional[];
  46. deprecated?: boolean;
  47. description?: string | false;
  48. handler: CommandHandlerCallback;
  49. middlewares: Middleware[];
  50. optional: Positional[];
  51. original: string;
  52. }
  53. export declare type CommandBuilder = CommandBuilderCallback | Dictionary<OptionDefinition>;
  54. interface CommandBuilderCallback {
  55. (y: YargsInstance): YargsInstance | void;
  56. }
  57. export declare function isCommandBuilderCallback(builder: CommandBuilder): builder is CommandBuilderCallback;
  58. interface Positionals extends Pick<Options, 'alias' | 'array' | 'default'> {
  59. demand: Dictionary<boolean>;
  60. }
  61. export interface FinishCommandHandler {
  62. (handlerResult: any): any;
  63. }
  64. export {};