2018-12-19 13:06:31 -05:00
|
|
|
import { test, assertEqual } from "https://deno.land/x/testing/testing.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() {
|
|
|
|
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
|
|
|
|
};
|
2018-12-21 13:02:52 -05:00
|
|
|
const aliasedArgv = parse(aliased, opts);
|
|
|
|
const propertyArgv = parse(regular, opts);
|
2018-12-19 13:06:31 -05:00
|
|
|
|
|
|
|
assertEqual(unknown, ['--derp', '-d']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test(function flagBooleanTrueAnyDoubleHyphenArgumentIsNotUnknown() {
|
|
|
|
const unknown = [];
|
|
|
|
function unknownFn(arg) {
|
|
|
|
unknown.push(arg);
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-21 13:02:52 -05:00
|
|
|
const argv = parse(['--honk', '--tacos=good', 'cow', '-p', '55'], {
|
2018-12-19 13:06:31 -05:00
|
|
|
boolean: true,
|
|
|
|
unknown: unknownFn
|
|
|
|
});
|
|
|
|
assertEqual(unknown, ['--tacos=good', 'cow', '-p']);
|
|
|
|
assertEqual(argv, {
|
|
|
|
honk: true,
|
|
|
|
_: []
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test(function stringAndAliasIsNotUnkown() {
|
|
|
|
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
|
|
|
|
};
|
2018-12-21 13:02:52 -05:00
|
|
|
const aliasedArgv = parse(aliased, opts);
|
|
|
|
const propertyArgv = parse(regular, opts);
|
2018-12-19 13:06:31 -05:00
|
|
|
|
|
|
|
assertEqual(unknown, ['--derp', '-d']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test(function defaultAndAliasIsNotUnknown() {
|
|
|
|
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
|
|
|
|
};
|
2018-12-21 13:02:52 -05:00
|
|
|
const aliasedArgv = parse(aliased, opts);
|
|
|
|
const propertyArgv = parse(regular, opts);
|
2018-12-19 13:06:31 -05:00
|
|
|
|
|
|
|
assertEqual(unknown, []);
|
|
|
|
});
|
|
|
|
|
|
|
|
test(function valueFollowingDoubleHyphenIsNotUnknown() {
|
|
|
|
const unknown = [];
|
|
|
|
function unknownFn(arg) {
|
|
|
|
unknown.push(arg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const aliased = [ '--bad', '--', 'good', 'arg' ];
|
|
|
|
const opts = {
|
|
|
|
'--': true,
|
|
|
|
unknown: unknownFn
|
|
|
|
};
|
2018-12-21 13:02:52 -05:00
|
|
|
const argv = parse(aliased, opts);
|
2018-12-19 13:06:31 -05:00
|
|
|
|
|
|
|
assertEqual(unknown, ['--bad']);
|
|
|
|
assertEqual(argv, {
|
|
|
|
'--': ['good', 'arg'],
|
|
|
|
'_': []
|
|
|
|
})
|
|
|
|
});
|