2019-02-07 11:45:47 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-03-06 16:39:50 -05:00
|
|
|
import { test } from "../../testing/mod.ts";
|
2019-03-06 19:42:24 -05:00
|
|
|
import { assertEquals } from "../../testing/asserts.ts";
|
2019-01-12 16:50:04 -05:00
|
|
|
import { parse } from "../mod.ts";
|
2018-12-19 13:06:31 -05:00
|
|
|
|
|
|
|
test(function booleanAndAliasIsNotUnknown() {
|
2018-12-24 10:28:01 -05:00
|
|
|
const unknown = [];
|
|
|
|
function unknownFn(arg) {
|
|
|
|
unknown.push(arg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const aliased = ["-h", "true", "--derp", "true"];
|
|
|
|
const regular = ["--herp", "true", "-d", "true"];
|
|
|
|
const opts = {
|
|
|
|
alias: { h: "herp" },
|
|
|
|
boolean: "h",
|
|
|
|
unknown: unknownFn
|
|
|
|
};
|
|
|
|
const aliasedArgv = parse(aliased, opts);
|
|
|
|
const propertyArgv = parse(regular, opts);
|
2018-12-19 13:06:31 -05:00
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(unknown, ["--derp", "-d"]);
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test(function flagBooleanTrueAnyDoubleHyphenArgumentIsNotUnknown() {
|
2018-12-24 10:28:01 -05:00
|
|
|
const unknown = [];
|
|
|
|
function unknownFn(arg) {
|
|
|
|
unknown.push(arg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const argv = parse(["--honk", "--tacos=good", "cow", "-p", "55"], {
|
|
|
|
boolean: true,
|
|
|
|
unknown: unknownFn
|
|
|
|
});
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(unknown, ["--tacos=good", "cow", "-p"]);
|
|
|
|
assertEquals(argv, {
|
2018-12-24 10:28:01 -05:00
|
|
|
honk: true,
|
|
|
|
_: []
|
|
|
|
});
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test(function stringAndAliasIsNotUnkown() {
|
2018-12-24 10:28:01 -05:00
|
|
|
const unknown = [];
|
|
|
|
function unknownFn(arg) {
|
|
|
|
unknown.push(arg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const aliased = ["-h", "hello", "--derp", "goodbye"];
|
|
|
|
const regular = ["--herp", "hello", "-d", "moon"];
|
|
|
|
const opts = {
|
|
|
|
alias: { h: "herp" },
|
|
|
|
string: "h",
|
|
|
|
unknown: unknownFn
|
|
|
|
};
|
|
|
|
const aliasedArgv = parse(aliased, opts);
|
|
|
|
const propertyArgv = parse(regular, opts);
|
2018-12-19 13:06:31 -05:00
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(unknown, ["--derp", "-d"]);
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test(function defaultAndAliasIsNotUnknown() {
|
2018-12-24 10:28:01 -05:00
|
|
|
const unknown = [];
|
|
|
|
function unknownFn(arg) {
|
|
|
|
unknown.push(arg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const aliased = ["-h", "hello"];
|
|
|
|
const regular = ["--herp", "hello"];
|
|
|
|
const opts = {
|
|
|
|
default: { h: "bar" },
|
|
|
|
alias: { h: "herp" },
|
|
|
|
unknown: unknownFn
|
|
|
|
};
|
|
|
|
const aliasedArgv = parse(aliased, opts);
|
|
|
|
const propertyArgv = 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
|
|
|
});
|
|
|
|
|
|
|
|
test(function valueFollowingDoubleHyphenIsNotUnknown() {
|
2018-12-24 10:28:01 -05:00
|
|
|
const unknown = [];
|
|
|
|
function unknownFn(arg) {
|
|
|
|
unknown.push(arg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const aliased = ["--bad", "--", "good", "arg"];
|
|
|
|
const opts = {
|
|
|
|
"--": true,
|
|
|
|
unknown: unknownFn
|
|
|
|
};
|
|
|
|
const argv = parse(aliased, opts);
|
2018-12-19 13:06:31 -05:00
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(unknown, ["--bad"]);
|
|
|
|
assertEquals(argv, {
|
2018-12-24 10:28:01 -05:00
|
|
|
"--": ["good", "arg"],
|
|
|
|
_: []
|
|
|
|
});
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|