[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.

Treat boolean values as positionals

Here changed behavior of booleans to not "swallow" positionals after
them.

For example `curl --get http://my-url.com` where --get is boolean
I need to get `{ get: true, _: ['http://my-url.com'] }`.

Node's parseArgs works the same way.

Bogdan Chadkin (Dec 31, 2024, 2:13 PM +0700) bfd84690 abc47f87

+22 -5
+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 });