Skip to content

Commit da0b247

Browse files
authored
fix!: conditionally print negative boolean argument usage (#177)
1 parent 768646a commit da0b247

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

src/usage.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export async function showUsage<T extends ArgsDef = ArgsDef>(
1515
}
1616
}
1717

18+
// `no` prefix matcher (kebab-case or camelCase)
19+
const negativePrefixRe = /^no[-A-Z]/;
20+
1821
export async function renderUsage<T extends ArgsDef = ArgsDef>(
1922
cmd: CommandDef<T>,
2023
parent?: CommandDef<T>,
@@ -47,14 +50,7 @@ export async function renderUsage<T extends ArgsDef = ArgsDef>(
4750
} else {
4851
const isRequired = arg.required === true && arg.default === undefined;
4952
const argStr =
50-
(arg.type === "boolean" && arg.default === true
51-
? [
52-
...(arg.alias || []).map((a) => `--no-${a}`),
53-
`--no-${arg.name}`,
54-
].join(", ")
55-
: [...(arg.alias || []).map((a) => `-${a}`), `--${arg.name}`].join(
56-
", ",
57-
)) +
53+
[...(arg.alias || []).map((a) => `-${a}`), `--${arg.name}`].join(", ") +
5854
(arg.type === "string" && (arg.valueHint || arg.default)
5955
? `=${
6056
arg.valueHint ? `<${arg.valueHint}>` : `"${arg.default || ""}"`
@@ -63,14 +59,31 @@ export async function renderUsage<T extends ArgsDef = ArgsDef>(
6359
(arg.type === "enum" && arg.options
6460
? `=<${arg.options.join("|")}>`
6561
: "");
66-
const isNegative = arg.type === "boolean" && arg.default === true;
67-
const description = isNegative
68-
? arg.negativeDescription || arg.description
69-
: arg.description;
7062
argLines.push([
7163
"`" + argStr + (isRequired ? " (required)" : "") + "`",
72-
description || "",
64+
arg.description || "",
7365
]);
66+
67+
/**
68+
* print negative boolean arg variant usage when
69+
* - enabled by default or has `negativeDescription`
70+
* - not prefixed with `no-` or `no[A-Z]`
71+
*/
72+
if (
73+
arg.type === "boolean" &&
74+
(arg.default === true || arg.negativeDescription) &&
75+
!negativePrefixRe.test(arg.name)
76+
) {
77+
const negativeArgStr = [
78+
...(arg.alias || []).map((a) => `--no-${a}`),
79+
`--no-${arg.name}`,
80+
].join(", ");
81+
argLines.push([
82+
"`" + negativeArgStr + (isRequired ? " (required)" : "") + "`",
83+
arg.negativeDescription || "",
84+
]);
85+
}
86+
7487
if (isRequired) {
7588
usageLine.push(argStr);
7689
}

0 commit comments

Comments
 (0)