-
Notifications
You must be signed in to change notification settings - Fork 10
/
commands.ts
47 lines (36 loc) · 1.04 KB
/
commands.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { Command } from 'commander';
import { formatHelp } from './help';
type ExtendedCommand = Command & {
group(name): ExtendedCommand;
};
export function createCommand(name?: string): ExtendedCommand {
const command = new Command(name) as ExtendedCommand;
command.helpOption('-h, --help', 'Show help for command');
command.showHelpAfterError(true);
command.addHelpCommand(false);
command.configureHelp({
sortSubcommands: true,
showGlobalOptions: true,
subcommandTerm: (cmd) => cmd.name(),
formatHelp: formatHelp,
});
command.group = (group) => {
(command as any)._group = group;
return command;
};
return command;
}
export function findTopCommand(command: Command) {
while (command.parent?.parent) {
command = command.parent;
}
return command;
}
export function findCommand(command: Command, names: string[]) {
for (const name of names) {
const subCommand = command.commands.find((c) => c.name() === name);
if (!subCommand) break;
command = subCommand;
}
return command;
}