diff --git a/src/index.ts b/src/index.ts index 2ca8e90..c41dc7e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -56,7 +56,7 @@ const defaultValue = (type?: "boolean" | "string" | "array") => { const coerce = (value?: string, type?: "string" | "boolean" | "array") => { if (type === "string") return value; - if (type === "boolean") return !!value; + if (type === "boolean") return value === undefined ? true : value === 'true'; if (!value) return value; if (value.length > 3 && BOOL_RE.test(value)) return value === "true"; diff --git a/test/flags.test.ts b/test/flags.test.ts index a96bd5c..61e01d2 100644 --- a/test/flags.test.ts +++ b/test/flags.test.ts @@ -257,3 +257,24 @@ describe("special cases", () => { expect(result).toEqual(output); }); }); + +describe("boolean flags", () => { + it("should handle long-form boolean flags correctly", () => { + const input = ["--add"]; + const opts = { + boolean: ['add'] + }; + const output = { _: [], add: true }; + expect(parse(input, opts)).toEqual(output); + }); + + it("should handle alias boolean flags correctly", () => { + const input = ["-a"]; + const opts = { + boolean: ['add'], + alias: { a: 'add' } + }; + const output = { _: [], add: true }; + expect(parse(input, opts)).toEqual(output); + }); +});