2019-01-01 22:46:17 -05:00
|
|
|
import { test, assertEqual } from "../../testing/mod.ts";
|
2018-12-21 13:02:52 -05:00
|
|
|
import { parse } from "../index.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
|
|
|
|
2018-12-24 10:28:01 -05:00
|
|
|
assertEqual(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
|
|
|
|
});
|
|
|
|
assertEqual(unknown, ["--tacos=good", "cow", "-p"]);
|
|
|
|
assertEqual(argv, {
|
|
|
|
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
|
|
|
|
2018-12-24 10:28:01 -05:00
|
|
|
assertEqual(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
|
|
|
|
2018-12-24 10:28:01 -05:00
|
|
|
assertEqual(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
|
|
|
|
2018-12-24 10:28:01 -05:00
|
|
|
assertEqual(unknown, ["--bad"]);
|
|
|
|
assertEqual(argv, {
|
|
|
|
"--": ["good", "arg"],
|
|
|
|
_: []
|
|
|
|
});
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|