diff --git a/src/index.ts b/src/index.ts index a5a7ac9..c6d13eb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -92,7 +92,6 @@ export function parse< if (argv.length === 0) return {} as Args; const obj = { ...defaults, _: [] } as unknown as Args; - const args = []; for (let i = 0; i < argv.length; i++) { const curr = argv[i]; const next = argv[i + 1]; @@ -122,9 +121,14 @@ export function parse< } } else if (!curr.includes("=") && next && next[0] !== "-") { key = curr.replace(/^-{1,2}/, ''); - value = next; t = type(key, types as any); - i++; + // treat boolean as flag without parameter + if (t === 'boolean') { + value = 'true'; + } else { + value = next; + i++; + } } else { const eq = curr.indexOf('='); if (eq === -1) { @@ -135,7 +139,7 @@ export function parse< } t = type(key, types as any); } - + if ((!t || t === "boolean") && key.length > 3 && key.startsWith('no-')) { set(obj, key.slice(3), false) } else { diff --git a/test/flags.test.ts b/test/flags.test.ts index b71791c..e1a1b37 100644 --- a/test/flags.test.ts +++ b/test/flags.test.ts @@ -194,7 +194,7 @@ describe("aliases", () => { _: [], help: true }; - const result = parse(input, { alias: { h: 'help' }}); + const result = parse(input, { alias: { h: 'help' } }); expect(result).toEqual(output); }); @@ -243,4 +243,17 @@ describe("special cases", () => { const result = parse(input); expect(result).toEqual(output); }); + + it("string after boolean should be treated as positional", () => { + const input = ["--get", "http://my-url.com"]; + const opts = { + boolean: ['get'] + } + const output = { + "_": ["http://my-url.com"], + "get": true, + }; + const result = parse(input, opts); + expect(result).toEqual(output); + }); }); -- 2.51.2 From d1774a6508dc5c7cd96d356daf043a7db860caed Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Tue, 31 Dec 2024 14:18:27 +0700 Subject: [PATCH 2/2] Add changeset --- .changeset/fresh-adults-deliver.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/fresh-adults-deliver.md diff --git a/.changeset/fresh-adults-deliver.md b/.changeset/fresh-adults-deliver.md new file mode 100644 index 0000000..1952fa2 --- /dev/null +++ b/.changeset/fresh-adults-deliver.md @@ -0,0 +1,5 @@ +--- +"ultraflag": minor +--- + +Treat boolean arguments as positionals