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("flagBooleanDefaultFalse", function (): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const argv = parse(["moo"], {
|
|
|
|
boolean: ["t", "verbose"],
|
2020-03-28 13:03:49 -04:00
|
|
|
default: { verbose: false, t: false },
|
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: false,
|
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("booleanGroups", function (): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const argv = parse(["-x", "-z", "one", "two", "three"], {
|
2020-03-28 13:03:49 -04:00
|
|
|
boolean: ["x", "y", "z"],
|
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
|
|
|
x: true,
|
|
|
|
y: false,
|
|
|
|
z: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
_: ["one", "two", "three"],
|
2018-12-24 10:28:01 -05:00
|
|
|
});
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(typeof argv.x, "boolean");
|
|
|
|
assertEquals(typeof argv.y, "boolean");
|
|
|
|
assertEquals(typeof argv.z, "boolean");
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("booleanAndAliasWithChainableApi", function (): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const aliased = ["-h", "derp"];
|
|
|
|
const regular = ["--herp", "derp"];
|
|
|
|
const aliasedArgv = parse(aliased, {
|
|
|
|
boolean: "herp",
|
2020-03-28 13:03:49 -04:00
|
|
|
alias: { h: "herp" },
|
2018-12-24 10:28:01 -05:00
|
|
|
});
|
|
|
|
const propertyArgv = parse(regular, {
|
|
|
|
boolean: "herp",
|
2020-03-28 13:03:49 -04:00
|
|
|
alias: { h: "herp" },
|
2018-12-24 10:28:01 -05:00
|
|
|
});
|
|
|
|
const expected = {
|
|
|
|
herp: true,
|
|
|
|
h: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
_: ["derp"],
|
2018-12-24 10:28:01 -05:00
|
|
|
};
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(aliasedArgv, expected);
|
|
|
|
assertEquals(propertyArgv, expected);
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("booleanAndAliasWithOptionsHash", function (): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const aliased = ["-h", "derp"];
|
|
|
|
const regular = ["--herp", "derp"];
|
|
|
|
const opts = {
|
|
|
|
alias: { h: "herp" },
|
2020-03-28 13:03:49 -04:00
|
|
|
boolean: "herp",
|
2018-12-24 10:28:01 -05:00
|
|
|
};
|
|
|
|
const aliasedArgv = parse(aliased, opts);
|
|
|
|
const propertyArgv = parse(regular, opts);
|
|
|
|
const expected = {
|
|
|
|
herp: true,
|
|
|
|
h: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
_: ["derp"],
|
2018-12-24 10:28:01 -05:00
|
|
|
};
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(aliasedArgv, expected);
|
|
|
|
assertEquals(propertyArgv, expected);
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("booleanAndAliasArrayWithOptionsHash", function (): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const aliased = ["-h", "derp"];
|
|
|
|
const regular = ["--herp", "derp"];
|
|
|
|
const alt = ["--harp", "derp"];
|
|
|
|
const opts = {
|
|
|
|
alias: { h: ["herp", "harp"] },
|
2020-03-28 13:03:49 -04:00
|
|
|
boolean: "h",
|
2018-12-24 10:28:01 -05:00
|
|
|
};
|
|
|
|
const aliasedArgv = parse(aliased, opts);
|
|
|
|
const propertyArgv = parse(regular, opts);
|
|
|
|
const altPropertyArgv = parse(alt, opts);
|
|
|
|
const expected = {
|
|
|
|
harp: true,
|
|
|
|
herp: true,
|
|
|
|
h: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
_: ["derp"],
|
2018-12-24 10:28:01 -05:00
|
|
|
};
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(aliasedArgv, expected);
|
|
|
|
assertEquals(propertyArgv, expected);
|
|
|
|
assertEquals(altPropertyArgv, expected);
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("booleanAndAliasUsingExplicitTrue", function (): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const aliased = ["-h", "true"];
|
|
|
|
const regular = ["--herp", "true"];
|
|
|
|
const opts = {
|
|
|
|
alias: { h: "herp" },
|
2020-03-28 13:03:49 -04:00
|
|
|
boolean: "h",
|
2018-12-24 10:28:01 -05:00
|
|
|
};
|
|
|
|
const aliasedArgv = parse(aliased, opts);
|
|
|
|
const propertyArgv = parse(regular, opts);
|
|
|
|
const expected = {
|
|
|
|
herp: true,
|
|
|
|
h: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
_: [],
|
2018-12-24 10:28:01 -05:00
|
|
|
};
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(aliasedArgv, expected);
|
|
|
|
assertEquals(propertyArgv, expected);
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
// regression, see https://github.com/substack/node-optimist/issues/71
|
|
|
|
// boolean and --x=true
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("booleanAndNonBoolean", function (): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const parsed = parse(["--boool", "--other=true"], {
|
2020-03-28 13:03:49 -04:00
|
|
|
boolean: "boool",
|
2018-12-24 10:28:01 -05:00
|
|
|
});
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(parsed.boool, true);
|
|
|
|
assertEquals(parsed.other, "true");
|
2018-12-24 10:28:01 -05:00
|
|
|
|
|
|
|
const parsed2 = parse(["--boool", "--other=false"], {
|
2020-03-28 13:03:49 -04:00
|
|
|
boolean: "boool",
|
2018-12-24 10:28:01 -05:00
|
|
|
});
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(parsed2.boool, true);
|
|
|
|
assertEquals(parsed2.other, "false");
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("booleanParsingTrue", function (): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const parsed = parse(["--boool=true"], {
|
|
|
|
default: {
|
2020-03-28 13:03:49 -04:00
|
|
|
boool: false,
|
2018-12-24 10:28:01 -05:00
|
|
|
},
|
2020-03-28 13:03:49 -04:00
|
|
|
boolean: ["boool"],
|
2018-12-24 10:28:01 -05:00
|
|
|
});
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(parsed.boool, true);
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("booleanParsingFalse", function (): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const parsed = parse(["--boool=false"], {
|
|
|
|
default: {
|
2020-03-28 13:03:49 -04:00
|
|
|
boool: true,
|
2018-12-24 10:28:01 -05:00
|
|
|
},
|
2020-03-28 13:03:49 -04:00
|
|
|
boolean: ["boool"],
|
2018-12-24 10:28:01 -05:00
|
|
|
});
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(parsed.boool, false);
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
2019-09-04 10:06:29 -04:00
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("booleanParsingTrueLike", function (): void {
|
2019-09-04 10:06:29 -04:00
|
|
|
const parsed = parse(["-t", "true123"], { boolean: ["t"] });
|
|
|
|
assertEquals(parsed.t, true);
|
|
|
|
|
|
|
|
const parsed2 = parse(["-t", "123"], { boolean: ["t"] });
|
|
|
|
assertEquals(parsed2.t, true);
|
|
|
|
|
|
|
|
const parsed3 = parse(["-t", "false123"], { boolean: ["t"] });
|
|
|
|
assertEquals(parsed3.t, true);
|
|
|
|
});
|
2019-10-16 11:34:36 -04:00
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("booleanNegationAfterBoolean", function (): void {
|
2019-10-16 11:34:36 -04:00
|
|
|
const parsed = parse(["--foo", "--no-foo"], { boolean: ["foo"] });
|
|
|
|
assertEquals(parsed.foo, false);
|
|
|
|
|
|
|
|
const parsed2 = parse(["--foo", "--no-foo", "123"], { boolean: ["foo"] });
|
|
|
|
assertEquals(parsed2.foo, false);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("booleanAfterBooleanNegation", function (): void {
|
2019-10-16 11:34:36 -04:00
|
|
|
const parsed = parse(["--no--foo", "--foo"], { boolean: ["foo"] });
|
|
|
|
assertEquals(parsed.foo, true);
|
|
|
|
|
|
|
|
const parsed2 = parse(["--no--foo", "--foo", "123"], { boolean: ["foo"] });
|
|
|
|
assertEquals(parsed2.foo, true);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("latestFlagIsBooleanNegation", function (): void {
|
2019-10-16 11:34:36 -04:00
|
|
|
const parsed = parse(["--no-foo", "--foo", "--no-foo"], { boolean: ["foo"] });
|
|
|
|
assertEquals(parsed.foo, false);
|
|
|
|
|
|
|
|
const parsed2 = parse(["--no-foo", "--foo", "--no-foo", "123"], {
|
2020-03-28 13:03:49 -04:00
|
|
|
boolean: ["foo"],
|
2019-10-16 11:34:36 -04:00
|
|
|
});
|
|
|
|
assertEquals(parsed2.foo, false);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("latestFlagIsBoolean", function (): void {
|
2019-10-16 11:34:36 -04:00
|
|
|
const parsed = parse(["--foo", "--no-foo", "--foo"], { boolean: ["foo"] });
|
|
|
|
assertEquals(parsed.foo, true);
|
|
|
|
|
|
|
|
const parsed2 = parse(["--foo", "--no-foo", "--foo", "123"], {
|
2020-03-28 13:03:49 -04:00
|
|
|
boolean: ["foo"],
|
2019-10-16 11:34:36 -04:00
|
|
|
});
|
|
|
|
assertEquals(parsed2.foo, true);
|
|
|
|
});
|