Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/command.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { consola } from "consola";
import type { CommandContext, CommandDef, ArgsDef } from "./types";
import { CLIError, resolveValue } from "./_utils";
import { parseArgs } from "./args";
Expand Down Expand Up @@ -50,7 +51,15 @@ export async function runCommand<T extends ArgsDef = ArgsDef>(
);
}
const subCommand = await resolveValue(subCommands[subCommandName]);
if (subCommand) {
// Same behavior as for runMain (see src/main.ts)
if (opts.rawArgs.includes("--version")) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, I should move it to another PR.

and should I create a function to avoid duplication with https://github.com/unjs/citty/blob/1cb60fe5b5aa7be64206e8d865b847adafae6549/src/main.ts#L22C7-L22C7 ?

const meta =
typeof cmd.meta === "function" ? await cmd.meta() : await cmd.meta;
if (!meta?.version) {
throw new CLIError("No version specified", "E_NO_VERSION");
}
consola.log(meta.version);
} else if (subCommand) {
await runCommand(subCommand, {
rawArgs: opts.rawArgs.slice(subCommandArgIndex + 1),
});
Expand Down
20 changes: 14 additions & 6 deletions src/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function showUsage<T extends ArgsDef = ArgsDef>(
parent?: CommandDef<T>,
) {
try {
consola.log((await renderUsage(cmd, parent)) + "\n");
consola.log(await renderUsage(cmd, parent));
} catch (error) {
consola.error(error);
}
Expand Down Expand Up @@ -91,27 +91,35 @@ export async function renderUsage<T extends ArgsDef = ArgsDef>(
"",
);

const hasOptions = argLines.length > 0 || posLines.length > 0;
const hasOptions = version || argLines.length > 0 || posLines.length > 0;

usageLines.push(
`${colors.underline(colors.bold("USAGE"))} \`${commandName}${
hasOptions ? " [OPTIONS]" : ""
} ${usageLine.join(" ")}\``,
"",
);

if (posLines.length > 0) {
usageLines.push("");
usageLines.push(colors.underline(colors.bold("ARGUMENTS")), "");
usageLines.push(formatLineColumns(posLines, " "));
usageLines.push("");
}

if (argLines.length > 0) {
usageLines.push(colors.underline(colors.bold("OPTIONS")), "");
usageLines.push(formatLineColumns(argLines, " "));
usageLines.push("");
usageLines.push(colors.underline(colors.bold("OPTIONS")), "");

if (version) {
usageLines.push(
formatLineColumns([["`--version`", "Show version"], ...argLines], " "),
);
} else {
usageLines.push(formatLineColumns(argLines, " "));
}
}

if (commandsLines.length > 0) {
usageLines.push("");
usageLines.push(colors.underline(colors.bold("COMMANDS")), "");
usageLines.push(formatLineColumns(commandsLines, " "));
usageLines.push(
Expand Down