Skip to content

Commit 1cf93e0

Browse files
authored
fix(coercion): fix non-value boolean flags (#8)
1 parent c8e6642 commit 1cf93e0

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const defaultValue = (type?: "boolean" | "string" | "array") => {
5656

5757
const coerce = (value?: string, type?: "string" | "boolean" | "array") => {
5858
if (type === "string") return value;
59-
if (type === "boolean") return !!value;
59+
if (type === "boolean") return value === undefined ? true : value === 'true';
6060

6161
if (!value) return value;
6262
if (value.length > 3 && BOOL_RE.test(value)) return value === "true";

test/flags.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,24 @@ describe("special cases", () => {
257257
expect(result).toEqual(output);
258258
});
259259
});
260+
261+
describe("boolean flags", () => {
262+
it("should handle long-form boolean flags correctly", () => {
263+
const input = ["--add"];
264+
const opts = {
265+
boolean: ['add']
266+
};
267+
const output = { _: [], add: true };
268+
expect(parse(input, opts)).toEqual(output);
269+
});
270+
271+
it("should handle alias boolean flags correctly", () => {
272+
const input = ["-a"];
273+
const opts = {
274+
boolean: ['add'],
275+
alias: { a: 'add' }
276+
};
277+
const output = { _: [], add: true };
278+
expect(parse(input, opts)).toEqual(output);
279+
});
280+
});

0 commit comments

Comments
 (0)