Skip to content
Merged
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
5 changes: 4 additions & 1 deletion src/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ function formatOptionsWithTitle(options: NormalizedOptions, title: string): stri
for (const { opt, valueSuffix, visualFlag } of entries) {
const flagPart = formatFlag(opt, valueSuffix);
const padding = " ".repeat(maxWidth - visualFlag.length + 2);
lines.push(` ${flagPart}${padding}${opt.description ?? ""}`);
const description = opt.description ?? "";
const defaultSuffix =
opt.default !== undefined ? ` ${kleur.dim(`(default: ${opt.default})`)}` : "";
lines.push(` ${flagPart}${padding}${description}${defaultSuffix}`);
}

return lines;
Expand Down
76 changes: 76 additions & 0 deletions test/help.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,80 @@ ${kleur.bold("Options:")}
expect(help).toContain("my-cli init myapp");
});
});

describe("default values", () => {
it("shows default value for string option", () => {
const cmd = command({
name: "my-cli",
options: {
output: { type: "string", default: "out.txt", description: "Output file" },
},
handler: () => {},
});

const help = cmd.help();

expect(help).toContain("Output file");
expect(help).toContain(kleur.dim("(default: out.txt)"));
});

it("shows default value for number option", () => {
const cmd = command({
name: "my-cli",
options: {
port: { type: "number", default: 3000, description: "Port number" },
},
handler: () => {},
});

const help = cmd.help();

expect(help).toContain("Port number");
expect(help).toContain(kleur.dim("(default: 3000)"));
});

it("shows default value for boolean option", () => {
const cmd = command({
name: "my-cli",
options: {
verbose: { type: "boolean", default: true, description: "Verbose mode" },
},
handler: () => {},
});

const help = cmd.help();

expect(help).toContain("Verbose mode");
expect(help).toContain(kleur.dim("(default: true)"));
});

it("does not show default suffix when no default", () => {
const cmd = command({
name: "my-cli",
options: {
port: { type: "number", description: "Port number" },
},
handler: () => {},
});

const help = cmd.help();

expect(help).toContain("Port number");
expect(help).not.toContain("(default:");
});

it("shows default alongside description", () => {
const cmd = command({
name: "my-cli",
options: {
port: { type: "number", default: 8080, description: "Server port" },
},
handler: () => {},
});

const help = cmd.help();

expect(help).toContain(`Server port ${kleur.dim("(default: 8080)")}`);
});
});
});