Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ function toArr(any: any) {
function toVal(out, key, val, opts) {
let x;
const old = out[key];
const nxt = ~opts.string.indexOf(key)
const nxt = opts.string.includes(key)
? val == undefined || val === true
? ""
? undefined
: String(val)
: typeof val === "boolean"
? val
: ~opts.boolean.indexOf(key)
: opts.boolean.includes(key)
? val === "false"
? false
: val === "true" ||
Expand Down Expand Up @@ -121,7 +121,7 @@ export function parseRawArgs<T = Default>(
out._.push(arg);
} else if (arg.substring(j, j + 3) === "no-") {
name = arg.slice(Math.max(0, j + 3));
if (strict && !~keys.indexOf(name)) {
if (strict && !keys.includes(name)) {
return opts.unknown(arg);
}
out[name] = false;
Expand All @@ -142,7 +142,7 @@ export function parseRawArgs<T = Default>(

for (idx = 0; idx < arr.length; idx++) {
name = arr[idx];
if (strict && !~keys.indexOf(name)) {
if (strict && !keys.includes(name)) {
return opts.unknown("-".repeat(j) + name);
}
toVal(out, name, idx + 1 < arr.length || val, opts);
Expand Down
1 change: 0 additions & 1 deletion src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export function parseArgs<T extends ArgsDef = ArgsDef>(
const parseOptions = {
boolean: [] as string[],
string: [] as string[],
number: [] as string[],
enum: [] as (number | string)[],
mixed: [] as string[],
alias: {} as Record<string, string | string[]>,
Expand Down
14 changes: 14 additions & 0 deletions test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,18 @@ describe("parseRawArgs", () => {
_: [],
});
});

it("handles arguments with no values", () => {
const args = ["--name", "--specified", "--any"];
const opts = { string: ["name"], boolean: ["specified"] };
const result = parseRawArgs(args, opts);

expect("name" in result).toBeTruthy();
expect("specified" in result).toBeTruthy();
expect("any" in result).toBeTruthy();

expect(result.name).toBeUndefined();
expect(result.specified).toBeTruthy();
expect(result.any).toBeTruthy();
});
});