[READ-ONLY] Mirror of https://github.com/bombshell-dev/args. <1kB CLI flag parser
args args-parser cli command-line command-line-parser node
5

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #4 from TrySound/boolean

Treat boolean arguments as positionals

authored by

Nate Moore and committed by
GitHub
(Jan 12, 2025, 9:06 PM -0600) 34b68512 abc47f87

+27 -5
+5
.changeset/fresh-adults-deliver.md
··· 1 + --- 2 + "ultraflag": minor 3 + --- 4 + 5 + Treat boolean arguments as positionals
+8 -4
src/index.ts
··· 92 92 if (argv.length === 0) return {} as Args<TArgs>; 93 93 const obj = { ...defaults, _: [] } as unknown as Args<TArgs>; 94 94 95 - const args = []; 96 95 for (let i = 0; i < argv.length; i++) { 97 96 const curr = argv[i]; 98 97 const next = argv[i + 1]; ··· 122 121 } 123 122 } else if (!curr.includes("=") && next && next[0] !== "-") { 124 123 key = curr.replace(/^-{1,2}/, ''); 125 - value = next; 126 124 t = type(key, types as any); 127 - i++; 125 + // treat boolean as flag without parameter 126 + if (t === 'boolean') { 127 + value = 'true'; 128 + } else { 129 + value = next; 130 + i++; 131 + } 128 132 } else { 129 133 const eq = curr.indexOf('='); 130 134 if (eq === -1) { ··· 135 139 } 136 140 t = type(key, types as any); 137 141 } 138 - 142 + 139 143 if ((!t || t === "boolean") && key.length > 3 && key.startsWith('no-')) { 140 144 set(obj, key.slice(3), false) 141 145 } else {
+14 -1
test/flags.test.ts
··· 194 194 _: [], 195 195 help: true 196 196 }; 197 - const result = parse(input, { alias: { h: 'help' }}); 197 + const result = parse(input, { alias: { h: 'help' } }); 198 198 expect(result).toEqual(output); 199 199 }); 200 200 ··· 241 241 _: ['-'], 242 242 }; 243 243 const result = parse(input); 244 + expect(result).toEqual(output); 245 + }); 246 + 247 + it("string after boolean should be treated as positional", () => { 248 + const input = ["--get", "http://my-url.com"]; 249 + const opts = { 250 + boolean: ['get'] 251 + } 252 + const output = { 253 + "_": ["http://my-url.com"], 254 + "get": true, 255 + }; 256 + const result = parse(input, opts); 244 257 expect(result).toEqual(output); 245 258 }); 246 259 });