Skip to content

Commit 34b6851

Browse files
authored
Merge pull request #4 from TrySound/boolean
Treat boolean arguments as positionals
2 parents abc47f8 + d1774a6 commit 34b6851

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

.changeset/fresh-adults-deliver.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"ultraflag": minor
3+
---
4+
5+
Treat boolean arguments as positionals

src/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ export function parse<
9292
if (argv.length === 0) return {} as Args<TArgs>;
9393
const obj = { ...defaults, _: [] } as unknown as Args<TArgs>;
9494

95-
const args = [];
9695
for (let i = 0; i < argv.length; i++) {
9796
const curr = argv[i];
9897
const next = argv[i + 1];
@@ -122,9 +121,14 @@ export function parse<
122121
}
123122
} else if (!curr.includes("=") && next && next[0] !== "-") {
124123
key = curr.replace(/^-{1,2}/, '');
125-
value = next;
126124
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+
}
128132
} else {
129133
const eq = curr.indexOf('=');
130134
if (eq === -1) {
@@ -135,7 +139,7 @@ export function parse<
135139
}
136140
t = type(key, types as any);
137141
}
138-
142+
139143
if ((!t || t === "boolean") && key.length > 3 && key.startsWith('no-')) {
140144
set(obj, key.slice(3), false)
141145
} else {

test/flags.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ describe("aliases", () => {
194194
_: [],
195195
help: true
196196
};
197-
const result = parse(input, { alias: { h: 'help' }});
197+
const result = parse(input, { alias: { h: 'help' } });
198198
expect(result).toEqual(output);
199199
});
200200

@@ -243,4 +243,17 @@ describe("special cases", () => {
243243
const result = parse(input);
244244
expect(result).toEqual(output);
245245
});
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);
257+
expect(result).toEqual(output);
258+
});
246259
});

0 commit comments

Comments
 (0)