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-04-28 06:33:09 -04:00
|
|
|
Deno.test("parseArgs", function (): void {
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(parse(["--no-moo"]), { moo: false, _: [] });
|
|
|
|
assertEquals(parse(["-v", "a", "-v", "b", "-v", "c"]), {
|
2018-12-24 10:28:01 -05:00
|
|
|
v: ["a", "b", "c"],
|
2020-03-28 13:03:49 -04:00
|
|
|
_: [],
|
2018-12-24 10:28:01 -05:00
|
|
|
});
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
2018-12-24 10:28:01 -05:00
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("comprehensive", function (): void {
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(
|
2018-12-24 10:28:01 -05:00
|
|
|
parse([
|
|
|
|
"--name=meowmers",
|
|
|
|
"bare",
|
|
|
|
"-cats",
|
|
|
|
"woo",
|
|
|
|
"-h",
|
|
|
|
"awesome",
|
|
|
|
"--multi=quux",
|
|
|
|
"--key",
|
|
|
|
"value",
|
|
|
|
"-b",
|
|
|
|
"--bool",
|
|
|
|
"--no-meep",
|
|
|
|
"--multi=baz",
|
|
|
|
"--",
|
|
|
|
"--not-a-flag",
|
2020-03-28 13:03:49 -04:00
|
|
|
"eek",
|
2018-12-24 10:28:01 -05:00
|
|
|
]),
|
|
|
|
{
|
|
|
|
c: true,
|
|
|
|
a: true,
|
|
|
|
t: true,
|
|
|
|
s: "woo",
|
|
|
|
h: "awesome",
|
|
|
|
b: true,
|
|
|
|
bool: true,
|
|
|
|
key: "value",
|
|
|
|
multi: ["quux", "baz"],
|
|
|
|
meep: false,
|
|
|
|
name: "meowmers",
|
2020-03-28 13:03:49 -04:00
|
|
|
_: ["bare", "--not-a-flag", "eek"],
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2018-12-24 10:28:01 -05:00
|
|
|
);
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("flagBoolean", function (): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const argv = parse(["-t", "moo"], { boolean: "t" });
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(argv, { t: true, _: ["moo"] });
|
|
|
|
assertEquals(typeof argv.t, "boolean");
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("flagBooleanValue", function (): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const argv = parse(["--verbose", "false", "moo", "-t", "true"], {
|
|
|
|
boolean: ["t", "verbose"],
|
2020-03-28 13:03:49 -04:00
|
|
|
default: { verbose: true },
|
2018-12-24 10:28:01 -05:00
|
|
|
});
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(argv, {
|
2018-12-24 10:28:01 -05:00
|
|
|
verbose: false,
|
|
|
|
t: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
_: ["moo"],
|
2018-12-24 10:28:01 -05:00
|
|
|
});
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(typeof argv.verbose, "boolean");
|
|
|
|
assertEquals(typeof argv.t, "boolean");
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("newlinesInParams", function (): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const args = parse(["-s", "X\nX"]);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(args, { _: [], s: "X\nX" });
|
2018-12-24 10:28:01 -05:00
|
|
|
|
|
|
|
// reproduce in bash:
|
|
|
|
// VALUE="new
|
|
|
|
// line"
|
|
|
|
// deno program.js --s="$VALUE"
|
|
|
|
const args2 = parse(["--s=X\nX"]);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(args2, { _: [], s: "X\nX" });
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("strings", function (): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const s = parse(["-s", "0001234"], { string: "s" }).s;
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(s, "0001234");
|
|
|
|
assertEquals(typeof s, "string");
|
2018-12-24 10:28:01 -05:00
|
|
|
|
|
|
|
const x = parse(["-x", "56"], { string: "x" }).x;
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(x, "56");
|
|
|
|
assertEquals(typeof x, "string");
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("stringArgs", function (): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const s = parse([" ", " "], { string: "_" })._;
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(s.length, 2);
|
|
|
|
assertEquals(typeof s[0], "string");
|
|
|
|
assertEquals(s[0], " ");
|
|
|
|
assertEquals(typeof s[1], "string");
|
|
|
|
assertEquals(s[1], " ");
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("emptyStrings", function (): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const s = parse(["-s"], { string: "s" }).s;
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(s, "");
|
|
|
|
assertEquals(typeof s, "string");
|
2018-12-19 13:06:31 -05:00
|
|
|
|
2018-12-24 10:28:01 -05:00
|
|
|
const str = parse(["--str"], { string: "str" }).str;
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(str, "");
|
|
|
|
assertEquals(typeof str, "string");
|
2018-12-19 13:06:31 -05:00
|
|
|
|
2018-12-24 10:28:01 -05:00
|
|
|
const letters = parse(["-art"], {
|
2020-03-28 13:03:49 -04:00
|
|
|
string: ["a", "t"],
|
2018-12-24 10:28:01 -05:00
|
|
|
});
|
2018-12-19 13:06:31 -05:00
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(letters.a, "");
|
|
|
|
assertEquals(letters.r, true);
|
|
|
|
assertEquals(letters.t, "");
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("stringAndAlias", function (): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const x = parse(["--str", "000123"], {
|
|
|
|
string: "s",
|
2020-03-28 13:03:49 -04:00
|
|
|
alias: { s: "str" },
|
2018-12-24 10:28:01 -05:00
|
|
|
});
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(x.str, "000123");
|
|
|
|
assertEquals(typeof x.str, "string");
|
|
|
|
assertEquals(x.s, "000123");
|
|
|
|
assertEquals(typeof x.s, "string");
|
2018-12-24 10:28:01 -05:00
|
|
|
|
|
|
|
const y = parse(["-s", "000123"], {
|
|
|
|
string: "str",
|
2020-03-28 13:03:49 -04:00
|
|
|
alias: { str: "s" },
|
2018-12-24 10:28:01 -05:00
|
|
|
});
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(y.str, "000123");
|
|
|
|
assertEquals(typeof y.str, "string");
|
|
|
|
assertEquals(y.s, "000123");
|
|
|
|
assertEquals(typeof y.s, "string");
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("slashBreak", function (): void {
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(parse(["-I/foo/bar/baz"]), { I: "/foo/bar/baz", _: [] });
|
|
|
|
assertEquals(parse(["-xyz/foo/bar/baz"]), {
|
2018-12-24 10:28:01 -05:00
|
|
|
x: true,
|
|
|
|
y: true,
|
|
|
|
z: "/foo/bar/baz",
|
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-04-28 06:33:09 -04:00
|
|
|
Deno.test("alias", function (): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const argv = parse(["-f", "11", "--zoom", "55"], {
|
2020-03-28 13:03:49 -04:00
|
|
|
alias: { z: "zoom" },
|
2018-12-24 10:28:01 -05:00
|
|
|
});
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(argv.zoom, 55);
|
|
|
|
assertEquals(argv.z, argv.zoom);
|
|
|
|
assertEquals(argv.f, 11);
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("multiAlias", function (): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const argv = parse(["-f", "11", "--zoom", "55"], {
|
2020-03-28 13:03:49 -04:00
|
|
|
alias: { z: ["zm", "zoom"] },
|
2018-12-24 10:28:01 -05:00
|
|
|
});
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(argv.zoom, 55);
|
|
|
|
assertEquals(argv.z, argv.zoom);
|
|
|
|
assertEquals(argv.z, argv.zm);
|
|
|
|
assertEquals(argv.f, 11);
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("nestedDottedObjects", function (): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const argv = parse([
|
|
|
|
"--foo.bar",
|
|
|
|
"3",
|
|
|
|
"--foo.baz",
|
|
|
|
"4",
|
|
|
|
"--foo.quux.quibble",
|
|
|
|
"5",
|
2019-03-12 01:51:51 -04:00
|
|
|
"--foo.quux.oO",
|
2020-03-28 13:03:49 -04:00
|
|
|
"--beep.boop",
|
2018-12-24 10:28:01 -05:00
|
|
|
]);
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(argv.foo, {
|
2018-12-24 10:28:01 -05:00
|
|
|
bar: 3,
|
|
|
|
baz: 4,
|
|
|
|
quux: {
|
|
|
|
quibble: 5,
|
2020-03-28 13:03:49 -04:00
|
|
|
oO: true,
|
|
|
|
},
|
2018-12-24 10:28:01 -05:00
|
|
|
});
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(argv.beep, { boop: true });
|
2018-12-24 10:28:01 -05:00
|
|
|
});
|
2019-09-08 17:18:39 -04:00
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("flagBuiltinProperty", function (): void {
|
2019-09-08 17:18:39 -04:00
|
|
|
const argv = parse(["--toString", "--valueOf", "foo"]);
|
|
|
|
assertEquals(argv, { toString: true, valueOf: "foo", _: [] });
|
|
|
|
assertEquals(typeof argv.toString, "boolean");
|
|
|
|
assertEquals(typeof argv.valueOf, "string");
|
|
|
|
});
|