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[] = [];
|
2020-04-06 09:27:10 -04:00
|
|
|
function unknownFn(arg: string, k?: string, v?: unknown): boolean {
|
|
|
|
unknown.push({ arg, k, v });
|
2018-12-24 10:28:01 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const aliased = ["-h", "true", "--derp", "true"];
|
2020-04-06 09:27:10 -04:00
|
|
|
const regular = ["--herp", "true", "-d", "false"];
|
2018-12-24 10:28:01 -05:00
|
|
|
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
|
|
|
|
2020-04-06 09:27:10 -04:00
|
|
|
assertEquals(unknown, [
|
|
|
|
{ arg: "--derp", k: "derp", v: "true" },
|
|
|
|
{ arg: "-d", k: "d", v: "false" },
|
|
|
|
]);
|
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[] = [];
|
2020-04-06 09:27:10 -04:00
|
|
|
function unknownFn(arg: string, k?: string, v?: unknown): boolean {
|
|
|
|
unknown.push({ arg, k, v });
|
2018-12-24 10:28:01 -05:00
|
|
|
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
|
|
|
});
|
2020-04-06 09:27:10 -04:00
|
|
|
assertEquals(unknown, [
|
|
|
|
{ arg: "--tacos=good", k: "tacos", v: "good" },
|
|
|
|
{ arg: "cow", k: undefined, v: undefined },
|
|
|
|
{ arg: "-p", k: "p", v: "55" },
|
|
|
|
]);
|
2019-03-06 19:42:24 -05:00
|
|
|
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[] = [];
|
2020-04-06 09:27:10 -04:00
|
|
|
function unknownFn(arg: string, k?: string, v?: unknown): boolean {
|
|
|
|
unknown.push({ arg, k, v });
|
2018-12-24 10:28:01 -05:00
|
|
|
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
|
|
|
|
2020-04-06 09:27:10 -04:00
|
|
|
assertEquals(unknown, [
|
|
|
|
{ arg: "--derp", k: "derp", v: "goodbye" },
|
|
|
|
{ arg: "-d", k: "d", v: "moon" },
|
|
|
|
]);
|
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[] = [];
|
2020-04-06 09:27:10 -04:00
|
|
|
function unknownFn(arg: string, k?: string, v?: unknown): boolean {
|
|
|
|
unknown.push({ arg, k, v });
|
2018-12-24 10:28:01 -05:00
|
|
|
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[] = [];
|
2020-04-06 09:27:10 -04:00
|
|
|
function unknownFn(arg: string, k?: string, v?: unknown): boolean {
|
|
|
|
unknown.push({ arg, k, v });
|
2018-12-24 10:28:01 -05:00
|
|
|
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
|
|
|
|
2020-04-06 09:27:10 -04:00
|
|
|
assertEquals(unknown, [{ arg: "--bad", k: "bad", v: true }]);
|
2019-03-06 19:42:24 -05:00
|
|
|
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
|
|
|
});
|