2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-03-19 13:24:11 -04:00
|
|
|
import { assertEquals } from "../testing/asserts.ts";
|
|
|
|
import { parse } from "./mod.ts";
|
2018-12-19 13:06:31 -05:00
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(function booleanAndAliasIsNotUnknown(): void {
|
2019-05-30 08:59:30 -04:00
|
|
|
const unknown: unknown[] = [];
|
|
|
|
function unknownFn(arg: unknown): boolean {
|
2018-12-24 10:28:01 -05:00
|
|
|
unknown.push(arg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const aliased = ["-h", "true", "--derp", "true"];
|
|
|
|
const regular = ["--herp", "true", "-d", "true"];
|
|
|
|
const opts = {
|
|
|
|
alias: { h: "herp" },
|
|
|
|
boolean: "h",
|
2020-03-28 13:03:49 -04:00
|
|
|
unknown: unknownFn,
|
2018-12-24 10:28:01 -05:00
|
|
|
};
|
2019-03-12 01:51:51 -04:00
|
|
|
parse(aliased, opts);
|
|
|
|
parse(regular, opts);
|
2018-12-19 13:06:31 -05:00
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(unknown, ["--derp", "-d"]);
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(function flagBooleanTrueAnyDoubleHyphenArgumentIsNotUnknown(): void {
|
2019-05-30 08:59:30 -04:00
|
|
|
const unknown: unknown[] = [];
|
|
|
|
function unknownFn(arg: unknown): boolean {
|
2018-12-24 10:28:01 -05:00
|
|
|
unknown.push(arg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const argv = parse(["--honk", "--tacos=good", "cow", "-p", "55"], {
|
|
|
|
boolean: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
unknown: unknownFn,
|
2018-12-24 10:28:01 -05:00
|
|
|
});
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(unknown, ["--tacos=good", "cow", "-p"]);
|
|
|
|
assertEquals(argv, {
|
2018-12-24 10:28:01 -05:00
|
|
|
honk: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
_: [],
|
2018-12-24 10:28:01 -05:00
|
|
|
});
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(function stringAndAliasIsNotUnkown(): void {
|
2019-05-30 08:59:30 -04:00
|
|
|
const unknown: unknown[] = [];
|
|
|
|
function unknownFn(arg: unknown): boolean {
|
2018-12-24 10:28:01 -05:00
|
|
|
unknown.push(arg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const aliased = ["-h", "hello", "--derp", "goodbye"];
|
|
|
|
const regular = ["--herp", "hello", "-d", "moon"];
|
|
|
|
const opts = {
|
|
|
|
alias: { h: "herp" },
|
|
|
|
string: "h",
|
2020-03-28 13:03:49 -04:00
|
|
|
unknown: unknownFn,
|
2018-12-24 10:28:01 -05:00
|
|
|
};
|
2019-03-12 01:51:51 -04:00
|
|
|
parse(aliased, opts);
|
|
|
|
parse(regular, opts);
|
2018-12-19 13:06:31 -05:00
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(unknown, ["--derp", "-d"]);
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(function defaultAndAliasIsNotUnknown(): void {
|
2019-05-30 08:59:30 -04:00
|
|
|
const unknown: unknown[] = [];
|
|
|
|
function unknownFn(arg: unknown): boolean {
|
2018-12-24 10:28:01 -05:00
|
|
|
unknown.push(arg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const aliased = ["-h", "hello"];
|
|
|
|
const regular = ["--herp", "hello"];
|
|
|
|
const opts = {
|
|
|
|
default: { h: "bar" },
|
|
|
|
alias: { h: "herp" },
|
2020-03-28 13:03:49 -04:00
|
|
|
unknown: unknownFn,
|
2018-12-24 10:28:01 -05:00
|
|
|
};
|
2019-03-12 01:51:51 -04:00
|
|
|
parse(aliased, opts);
|
|
|
|
parse(regular, opts);
|
2018-12-19 13:06:31 -05:00
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(unknown, []);
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(function valueFollowingDoubleHyphenIsNotUnknown(): void {
|
2019-05-30 08:59:30 -04:00
|
|
|
const unknown: unknown[] = [];
|
|
|
|
function unknownFn(arg: unknown): boolean {
|
2018-12-24 10:28:01 -05:00
|
|
|
unknown.push(arg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const aliased = ["--bad", "--", "good", "arg"];
|
|
|
|
const opts = {
|
|
|
|
"--": true,
|
2020-03-28 13:03:49 -04:00
|
|
|
unknown: unknownFn,
|
2018-12-24 10:28:01 -05:00
|
|
|
};
|
|
|
|
const argv = parse(aliased, opts);
|
2018-12-19 13:06:31 -05:00
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(unknown, ["--bad"]);
|
|
|
|
assertEquals(argv, {
|
2018-12-24 10:28:01 -05:00
|
|
|
"--": ["good", "arg"],
|
2020-03-28 13:03:49 -04:00
|
|
|
_: [],
|
2018-12-24 10:28:01 -05:00
|
|
|
});
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|