From 28f84f7efbe1a7aba7109450e4a12d89935b6e54 Mon Sep 17 00:00:00 2001 From: barbapapazes Date: Wed, 11 Oct 2023 15:35:21 +0200 Subject: [PATCH 1/4] feat: add usage line for version --- src/usage.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/usage.ts b/src/usage.ts index 2fb0274..e677de4 100644 --- a/src/usage.ts +++ b/src/usage.ts @@ -117,6 +117,7 @@ export async function renderUsage( usageLines.push( "", `Use \`${commandName} --help\` for more information about a command.`, + `Use \`${commandName} --version\` to get the version of the CLI.`, ); } From 96661d3e8a3b2f80e8627ebd54674203bab17338 Mon Sep 17 00:00:00 2001 From: barbapapazes Date: Wed, 11 Oct 2023 15:40:08 +0200 Subject: [PATCH 2/4] fix: usage --- src/usage.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/usage.ts b/src/usage.ts index e677de4..fd564f7 100644 --- a/src/usage.ts +++ b/src/usage.ts @@ -117,6 +117,11 @@ export async function renderUsage( usageLines.push( "", `Use \`${commandName} --help\` for more information about a command.`, + ); + } + + if (version) { + usageLines.push( `Use \`${commandName} --version\` to get the version of the CLI.`, ); } From 2f614ed521f8238dbd0500d192946812225d201c Mon Sep 17 00:00:00 2001 From: barbapapazes Date: Fri, 13 Oct 2023 10:12:05 +0200 Subject: [PATCH 3/4] feat: move version to options --- src/usage.ts | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/usage.ts b/src/usage.ts index fd564f7..eb332a6 100644 --- a/src/usage.ts +++ b/src/usage.ts @@ -91,7 +91,8 @@ export async function renderUsage( "", ); - const hasOptions = argLines.length > 0 || posLines.length > 0; + const hasOptions = version || argLines.length > 0; + usageLines.push( `${colors.underline(colors.bold("USAGE"))} \`${commandName}${ hasOptions ? " [OPTIONS]" : "" @@ -105,9 +106,19 @@ export async function renderUsage( usageLines.push(""); } - if (argLines.length > 0) { + if (hasOptions) { usageLines.push(colors.underline(colors.bold("OPTIONS")), ""); - usageLines.push(formatLineColumns(argLines, " ")); + + if (version) { + usageLines.push( + formatLineColumns([[colors.cyan("--version"), "Show version"]], " "), + ); + } + + if (argLines.length > 0) { + usageLines.push(formatLineColumns(argLines, " ")); + } + usageLines.push(""); } @@ -120,11 +131,5 @@ export async function renderUsage( ); } - if (version) { - usageLines.push( - `Use \`${commandName} --version\` to get the version of the CLI.`, - ); - } - return usageLines.filter((l) => typeof l === "string").join("\n"); } From 6e54a42935ca2f61b765cb838305fd8c80c9a901 Mon Sep 17 00:00:00 2001 From: barbapapazes Date: Wed, 13 Dec 2023 09:45:46 +0100 Subject: [PATCH 4/4] fix: update behavior --- src/command.ts | 11 ++++++++++- src/usage.ts | 19 ++++++++----------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/command.ts b/src/command.ts index 1abfe44..73f990e 100644 --- a/src/command.ts +++ b/src/command.ts @@ -1,3 +1,4 @@ +import { consola } from "consola"; import type { CommandContext, CommandDef, ArgsDef } from "./types"; import { CLIError, resolveValue } from "./_utils"; import { parseArgs } from "./args"; @@ -50,7 +51,15 @@ export async function runCommand( ); } const subCommand = await resolveValue(subCommands[subCommandName]); - if (subCommand) { + // Same behavior as for runMain (see src/main.ts) + if (opts.rawArgs.includes("--version")) { + 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), }); diff --git a/src/usage.ts b/src/usage.ts index eb332a6..d6800db 100644 --- a/src/usage.ts +++ b/src/usage.ts @@ -9,7 +9,7 @@ export async function showUsage( parent?: CommandDef, ) { try { - consola.log((await renderUsage(cmd, parent)) + "\n"); + consola.log(await renderUsage(cmd, parent)); } catch (error) { consola.error(error); } @@ -91,38 +91,35 @@ export async function renderUsage( "", ); - const hasOptions = version || argLines.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 (hasOptions) { + if (argLines.length > 0) { + usageLines.push(""); usageLines.push(colors.underline(colors.bold("OPTIONS")), ""); if (version) { usageLines.push( - formatLineColumns([[colors.cyan("--version"), "Show version"]], " "), + formatLineColumns([["`--version`", "Show version"], ...argLines], " "), ); - } - - if (argLines.length > 0) { + } else { usageLines.push(formatLineColumns(argLines, " ")); } - - usageLines.push(""); } if (commandsLines.length > 0) { + usageLines.push(""); usageLines.push(colors.underline(colors.bold("COMMANDS")), ""); usageLines.push(formatLineColumns(commandsLines, " ")); usageLines.push(