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 booleanAndAliasIsNotUnknown(): void {
|
2019-05-30 08:59:30 -04:00
|
|
|
const unknown: unknown[] = [];
|
|
|
|
function unknownFn(arg: unknown): boolean {
|
2018-12-24 10:28:01 -05:00
|
|
|
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
|
|
|
|
};
|
2019-03-12 01:51:51 -04:00
|
|
|
parse(aliased, opts);
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function flagBooleanTrueAnyDoubleHyphenArgumentIsNotUnknown(): void {
|
2019-05-30 08:59:30 -04:00
|
|
|
const unknown: unknown[] = [];
|
|
|
|
function unknownFn(arg: unknown): boolean {
|
2018-12-24 10:28:01 -05:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function stringAndAliasIsNotUnkown(): void {
|
2019-05-30 08:59:30 -04:00
|
|
|
const unknown: unknown[] = [];
|
|
|
|
function unknownFn(arg: unknown): boolean {
|
2018-12-24 10:28:01 -05:00
|
|
|
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
|
|
|
|
};
|
2019-03-12 01:51:51 -04:00
|
|
|
parse(aliased, opts);
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function defaultAndAliasIsNotUnknown(): void {
|
2019-05-30 08:59:30 -04:00
|
|
|
const unknown: unknown[] = [];
|
|
|
|
function unknownFn(arg: unknown): boolean {
|
2018-12-24 10:28:01 -05:00
|
|
|
unknown.push(arg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const aliased = ["-h", "hello"];
|
|
|
|
const regular = ["--herp", "hello"];
|
|
|
|
const opts = {
|
|
|
|
default: { h: "bar" },
|
|
|
|
alias: { h: "herp" },
|
|
|
|
unknown: unknownFn
|
|
|
|
};
|
2019-03-12 01:51:51 -04:00
|
|
|
parse(aliased, opts);
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function valueFollowingDoubleHyphenIsNotUnknown(): void {
|
2019-05-30 08:59:30 -04:00
|
|
|
const unknown: unknown[] = [];
|
|
|
|
function unknownFn(arg: unknown): boolean {
|
2018-12-24 10:28:01 -05:00
|
|
|
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
|
|
|
});
|