UNPKG

2.21 kBTypeScriptView Raw
1import Option, { OptionConfig } from './Option';
2interface CommandArg {
3 required: boolean;
4 value: string;
5 variadic: boolean;
6}
7interface HelpConfig {
8 bin: string;
9 subCommands?: Command[];
10 versionNumber?: string;
11 globalOptions?: Option[];
12}
13interface HelpSection {
14 title?: string;
15 body: string;
16}
17interface CommandConfig {
18 allowUnknownOptions?: boolean;
19}
20declare type HelpCallback = (sections: HelpSection[]) => void;
21declare type CommandExample = ((bin: string) => string) | string;
22export default class Command {
23 rawName: string;
24 description: string;
25 options: Option[];
26 aliasNames: string[];
27 name: string;
28 args: CommandArg[];
29 commandAction?: (...args: any[]) => any;
30 usageText?: string;
31 versionNumber?: string;
32 examples: CommandExample[];
33 config: CommandConfig;
34 helpCallback?: HelpCallback;
35 constructor(rawName: string, description: string);
36 usage(text: string): this;
37 allowUnknownOptions(): this;
38 version(version: string, customFlags?: string): this;
39 example(example: CommandExample): this;
40 /**
41 * Add a option for this command
42 * @param rawName Raw option name(s)
43 * @param description Option description
44 * @param config Option config
45 */
46 option(rawName: string, description: string, config?: OptionConfig): this;
47 alias(name: string): this;
48 action(callback: (...args: any[]) => any): this;
49 /**
50 * Check if a command name is matched by this command
51 * @param name Command name
52 */
53 isMatched(name: string): boolean;
54 readonly isDefaultCommand: boolean;
55 /**
56 * Check if an option is registered in this command
57 * @param name Option name
58 */
59 hasOption(name: string): Option | undefined;
60 outputHelp(config: HelpConfig): void;
61 outputVersion(bin: string): void;
62 /**
63 * Check if the parsed options contain any unknown options
64 * Exit and output error when true
65 * @param options Original options, i.e. not camelCased one
66 * @param globalCommand
67 */
68 checkUnknownOptions(options: {
69 [k: string]: any;
70 }, globalCommand: Command): void;
71}
72export { HelpCallback, CommandExample };