Skip to content
Merged
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
39 changes: 26 additions & 13 deletions src/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export async function showUsage<T extends ArgsDef = ArgsDef>(
}
}

// `no` prefix matcher (kebab-case or camelCase)
const negativePrefixRe = /^no[-A-Z]/;

export async function renderUsage<T extends ArgsDef = ArgsDef>(
cmd: CommandDef<T>,
parent?: CommandDef<T>,
Expand Down Expand Up @@ -47,14 +50,7 @@ export async function renderUsage<T extends ArgsDef = ArgsDef>(
} 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 || ""}"`
Expand All @@ -63,14 +59,31 @@ export async function renderUsage<T extends ArgsDef = ArgsDef>(
(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);
}
Expand Down