Skip to content

Commit ed7efca

Browse files
authored
fix: do not throw error when no subcommand specified but main has run (#58)
1 parent 4bf355e commit ed7efca

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/command.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,24 @@ export async function runCommand<T extends ArgsDef = ArgsDef>(
4141
(arg) => !arg.startsWith("-"),
4242
);
4343
const subCommandName = opts.rawArgs[subCommandArgIndex];
44-
if (!subCommandName && !cmd.run) {
44+
if (subCommandName) {
45+
if (!subCommands[subCommandName]) {
46+
throw new CLIError(
47+
`Unknown command \`${subCommandName}\``,
48+
"E_UNKNOWN_COMMAND",
49+
);
50+
}
51+
const subCommand = await resolveValue(subCommands[subCommandName]);
52+
if (subCommand) {
53+
await runCommand(subCommand, {
54+
rawArgs: opts.rawArgs.slice(subCommandArgIndex + 1),
55+
});
56+
}
57+
} else if (!cmd.run) {
4558
throw new CLIError(`No command specified.`, "E_NO_COMMAND");
4659
}
47-
if (!subCommands[subCommandName]) {
48-
throw new CLIError(
49-
`Unknown command \`${subCommandName}\``,
50-
"E_UNKNOWN_COMMAND",
51-
);
52-
}
53-
const subCommand = await resolveValue(subCommands[subCommandName]);
54-
if (subCommand) {
55-
await runCommand(subCommand, {
56-
rawArgs: opts.rawArgs.slice(subCommandArgIndex + 1),
57-
});
58-
}
5960
}
61+
6062
// Handle main command
6163
if (typeof cmd.run === "function") {
6264
await cmd.run(context);

0 commit comments

Comments
 (0)