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 flagBooleanDefaultFalse(): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const argv = parse(["moo"], {
|
|
|
|
boolean: ["t", "verbose"],
|
|
|
|
default: { verbose: false, t: false }
|
|
|
|
});
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(argv, {
|
2018-12-24 10:28:01 -05:00
|
|
|
verbose: false,
|
|
|
|
t: false,
|
|
|
|
_: ["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 booleanGroups(): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const argv = parse(["-x", "-z", "one", "two", "three"], {
|
|
|
|
boolean: ["x", "y", "z"]
|
|
|
|
});
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(argv, {
|
2018-12-24 10:28:01 -05:00
|
|
|
x: true,
|
|
|
|
y: false,
|
|
|
|
z: true,
|
|
|
|
_: ["one", "two", "three"]
|
|
|
|
});
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function booleanAndAliasWithChainableApi(): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const aliased = ["-h", "derp"];
|
|
|
|
const regular = ["--herp", "derp"];
|
|
|
|
const aliasedArgv = parse(aliased, {
|
|
|
|
boolean: "herp",
|
|
|
|
alias: { h: "herp" }
|
|
|
|
});
|
|
|
|
const propertyArgv = parse(regular, {
|
|
|
|
boolean: "herp",
|
|
|
|
alias: { h: "herp" }
|
|
|
|
});
|
|
|
|
const expected = {
|
|
|
|
herp: true,
|
|
|
|
h: true,
|
|
|
|
_: ["derp"]
|
|
|
|
};
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(aliasedArgv, expected);
|
|
|
|
assertEquals(propertyArgv, expected);
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function booleanAndAliasWithOptionsHash(): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const aliased = ["-h", "derp"];
|
|
|
|
const regular = ["--herp", "derp"];
|
|
|
|
const opts = {
|
|
|
|
alias: { h: "herp" },
|
|
|
|
boolean: "herp"
|
|
|
|
};
|
|
|
|
const aliasedArgv = parse(aliased, opts);
|
|
|
|
const propertyArgv = parse(regular, opts);
|
|
|
|
const expected = {
|
|
|
|
herp: true,
|
|
|
|
h: true,
|
|
|
|
_: ["derp"]
|
|
|
|
};
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(aliasedArgv, expected);
|
|
|
|
assertEquals(propertyArgv, expected);
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function booleanAndAliasArrayWithOptionsHash(): 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"] },
|
|
|
|
boolean: "h"
|
|
|
|
};
|
|
|
|
const aliasedArgv = parse(aliased, opts);
|
|
|
|
const propertyArgv = parse(regular, opts);
|
|
|
|
const altPropertyArgv = parse(alt, opts);
|
|
|
|
const expected = {
|
|
|
|
harp: true,
|
|
|
|
herp: true,
|
|
|
|
h: true,
|
|
|
|
_: ["derp"]
|
|
|
|
};
|
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
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function booleanAndAliasUsingExplicitTrue(): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const aliased = ["-h", "true"];
|
|
|
|
const regular = ["--herp", "true"];
|
|
|
|
const opts = {
|
|
|
|
alias: { h: "herp" },
|
|
|
|
boolean: "h"
|
|
|
|
};
|
|
|
|
const aliasedArgv = parse(aliased, opts);
|
|
|
|
const propertyArgv = parse(regular, opts);
|
|
|
|
const expected = {
|
|
|
|
herp: true,
|
|
|
|
h: true,
|
|
|
|
_: []
|
|
|
|
};
|
|
|
|
|
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
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function booleanAndNonBoolean(): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const parsed = parse(["--boool", "--other=true"], {
|
|
|
|
boolean: "boool"
|
|
|
|
});
|
|
|
|
|
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"], {
|
|
|
|
boolean: "boool"
|
|
|
|
});
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(parsed2.boool, true);
|
|
|
|
assertEquals(parsed2.other, "false");
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function booleanParsingTrue(): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const parsed = parse(["--boool=true"], {
|
|
|
|
default: {
|
|
|
|
boool: false
|
|
|
|
},
|
|
|
|
boolean: ["boool"]
|
|
|
|
});
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(parsed.boool, true);
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function booleanParsingFalse(): void {
|
2018-12-24 10:28:01 -05:00
|
|
|
const parsed = parse(["--boool=false"], {
|
|
|
|
default: {
|
|
|
|
boool: true
|
|
|
|
},
|
|
|
|
boolean: ["boool"]
|
|
|
|
});
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(parsed.boool, false);
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|