2019-02-07 11:45:47 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-03-19 13:24:11 -04:00
|
|
|
import { test } from "../testing/mod.ts";
|
|
|
|
import { assertEquals } from "../testing/asserts.ts";
|
|
|
|
import { parse } from "./mod.ts";
|
2018-12-19 13:06:31 -05:00
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function _arseArgs(): 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"],
|
|
|
|
_: []
|
|
|
|
});
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
2018-12-24 10:28:01 -05:00
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function comprehensive(): 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",
|
|
|
|
"eek"
|
|
|
|
]),
|
|
|
|
{
|
|
|
|
c: true,
|
|
|
|
a: true,
|
|
|
|
t: true,
|
|
|
|
s: "woo",
|
|
|
|
h: "awesome",
|
|
|
|
b: true,
|
|
|
|
bool: true,
|
|
|
|
key: "value",
|
|
|
|
multi: ["quux", "baz"],
|
|
|
|
meep: false,
|
|
|
|
name: "meowmers",
|
|
|
|
_: ["bare", "--not-a-flag", "eek"]
|
|
|
|
}
|
|
|
|
);
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function flagBoolean(): 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
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function flagBooleanValue(): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const argv = parse(["--verbose", "false", "moo", "-t", "true"], {
|
|
|
|
boolean: ["t", "verbose"],
|
|
|
|
default: { verbose: true }
|
|
|
|
});
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(argv, {
|
2018-12-24 10:28:01 -05:00
|
|
|
verbose: false,
|
|
|
|
t: true,
|
|
|
|
_: ["moo"]
|
|
|
|
});
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function newlinesInParams(): 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
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function strings(): 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
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function stringArgs(): 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
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function emptyStrings(): 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"], {
|
|
|
|
string: ["a", "t"]
|
|
|
|
});
|
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
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function stringAndAlias(): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const x = parse(["--str", "000123"], {
|
|
|
|
string: "s",
|
|
|
|
alias: { s: "str" }
|
|
|
|
});
|
|
|
|
|
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",
|
|
|
|
alias: { str: "s" }
|
|
|
|
});
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function slashBreak(): 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",
|
|
|
|
_: []
|
|
|
|
});
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function alias(): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const argv = parse(["-f", "11", "--zoom", "55"], {
|
|
|
|
alias: { z: "zoom" }
|
|
|
|
});
|
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
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function multiAlias(): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const argv = parse(["-f", "11", "--zoom", "55"], {
|
|
|
|
alias: { z: ["zm", "zoom"] }
|
|
|
|
});
|
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
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function nestedDottedObjects(): 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",
|
2018-12-24 10:28:01 -05:00
|
|
|
"--beep.boop"
|
|
|
|
]);
|
|
|
|
|
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,
|
2019-03-12 01:51:51 -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
|
|
|
|
|
|
|
test(function flagBuiltinProperty(): void {
|
|
|
|
const argv = parse(["--toString", "--valueOf", "foo"]);
|
|
|
|
assertEquals(argv, { toString: true, valueOf: "foo", _: [] });
|
|
|
|
assertEquals(typeof argv.toString, "boolean");
|
|
|
|
assertEquals(typeof argv.valueOf, "string");
|
|
|
|
});
|