diff --git a/src/usage.ts b/src/usage.ts index cb4a794..8500c26 100644 --- a/src/usage.ts +++ b/src/usage.ts @@ -15,6 +15,9 @@ export async function showUsage( } } +// `no` prefix matcher (kebab-case or camelCase) +const negativePrefixRe = /^no[-A-Z]/; + export async function renderUsage( cmd: CommandDef, parent?: CommandDef, @@ -47,14 +50,7 @@ export async function renderUsage( } else { const isRequired = arg.required === true && arg.default === undefined; const argStr = - (arg.type === "boolean" && arg.default === true - ? [ - ...(arg.alias || []).map((a) => `--no-${a}`), - `--no-${arg.name}`, - ].join(", ") - : [...(arg.alias || []).map((a) => `-${a}`), `--${arg.name}`].join( - ", ", - )) + + [...(arg.alias || []).map((a) => `-${a}`), `--${arg.name}`].join(", ") + (arg.type === "string" && (arg.valueHint || arg.default) ? `=${ arg.valueHint ? `<${arg.valueHint}>` : `"${arg.default || ""}"` @@ -63,14 +59,31 @@ export async function renderUsage( (arg.type === "enum" && arg.options ? `=<${arg.options.join("|")}>` : ""); - const isNegative = arg.type === "boolean" && arg.default === true; - const description = isNegative - ? arg.negativeDescription || arg.description - : arg.description; argLines.push([ "`" + argStr + (isRequired ? " (required)" : "") + "`", - description || "", + arg.description || "", ]); + + /** + * print negative boolean arg variant usage when + * - enabled by default or has `negativeDescription` + * - not prefixed with `no-` or `no[A-Z]` + */ + if ( + arg.type === "boolean" && + (arg.default === true || arg.negativeDescription) && + !negativePrefixRe.test(arg.name) + ) { + const negativeArgStr = [ + ...(arg.alias || []).map((a) => `--no-${a}`), + `--no-${arg.name}`, + ].join(", "); + argLines.push([ + "`" + negativeArgStr + (isRequired ? " (required)" : "") + "`", + arg.negativeDescription || "", + ]); + } + if (isRequired) { usageLine.push(argStr); }