2019-02-07 11:45:47 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-12-19 13:06:31 -05:00
|
|
|
export interface ArgParsingOptions {
|
2019-05-30 08:59:30 -04:00
|
|
|
unknown?: (i: unknown) => unknown;
|
2019-03-12 01:51:51 -04:00
|
|
|
boolean?: boolean | string | string[];
|
2018-12-19 13:06:31 -05:00
|
|
|
alias?: { [key: string]: string | string[] };
|
|
|
|
string?: string | string[];
|
2019-06-06 12:56:33 -04:00
|
|
|
default?: { [key: string]: unknown };
|
2019-03-12 01:51:51 -04:00
|
|
|
"--"?: boolean;
|
|
|
|
stopEarly?: boolean;
|
2018-12-19 13:06:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const DEFAULT_OPTIONS = {
|
2019-05-30 08:59:30 -04:00
|
|
|
unknown: (i: unknown): unknown => i,
|
2018-12-19 13:06:31 -05:00
|
|
|
boolean: false,
|
|
|
|
alias: {},
|
|
|
|
string: [],
|
|
|
|
default: {},
|
|
|
|
"--": false,
|
|
|
|
stopEarly: false
|
|
|
|
};
|
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
interface Flags {
|
|
|
|
bools: { [key: string]: boolean };
|
|
|
|
strings: { [key: string]: boolean };
|
|
|
|
unknownFn: (i: unknown) => unknown;
|
|
|
|
allBools: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface NestedMapping {
|
|
|
|
[key: string]: NestedMapping | unknown;
|
|
|
|
}
|
|
|
|
|
2019-03-12 01:51:51 -04:00
|
|
|
function isNumber(x: unknown): boolean {
|
|
|
|
if (typeof x === "number") return true;
|
|
|
|
if (/^0x[0-9a-f]+$/i.test(String(x))) return true;
|
|
|
|
return /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(String(x));
|
|
|
|
}
|
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
function hasKey(obj: NestedMapping, keys: string[]): boolean {
|
2019-03-12 01:51:51 -04:00
|
|
|
let o = obj;
|
2019-05-30 08:59:30 -04:00
|
|
|
keys.slice(0, -1).forEach(function(key: string): void {
|
|
|
|
o = (o[key] || {}) as NestedMapping;
|
2019-03-12 01:51:51 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
const key = keys[keys.length - 1];
|
|
|
|
return key in o;
|
|
|
|
}
|
|
|
|
|
2018-12-21 13:02:52 -05:00
|
|
|
export function parse(
|
2019-05-30 08:59:30 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
args: any[],
|
2018-12-19 13:06:31 -05:00
|
|
|
initialOptions?: ArgParsingOptions
|
2019-04-24 07:41:23 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2018-12-19 13:06:31 -05:00
|
|
|
): { [key: string]: any } {
|
|
|
|
const options: ArgParsingOptions = {
|
|
|
|
...DEFAULT_OPTIONS,
|
|
|
|
...(initialOptions || {})
|
|
|
|
};
|
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
const flags: Flags = {
|
2018-12-19 13:06:31 -05:00
|
|
|
bools: {},
|
|
|
|
strings: {},
|
|
|
|
unknownFn: options.unknown!,
|
|
|
|
allBools: false
|
|
|
|
};
|
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
if (options.boolean !== undefined) {
|
|
|
|
if (typeof options.boolean === "boolean") {
|
|
|
|
flags.allBools = !!options.boolean;
|
|
|
|
} else {
|
|
|
|
const booleanArgs: string[] =
|
|
|
|
typeof options.boolean === "string"
|
|
|
|
? [options.boolean]
|
|
|
|
: options.boolean;
|
|
|
|
|
|
|
|
booleanArgs.filter(Boolean).forEach(
|
|
|
|
(key: string): void => {
|
|
|
|
flags.bools[key] = true;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2018-12-19 13:06:31 -05:00
|
|
|
}
|
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
const aliases: { [key: string]: string[] } = {};
|
|
|
|
if (options.alias !== undefined) {
|
|
|
|
for (const key in options.alias) {
|
|
|
|
const val = options.alias[key];
|
2018-12-19 13:06:31 -05:00
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
if (typeof val === "string") {
|
|
|
|
aliases[key] = [val];
|
|
|
|
} else {
|
|
|
|
aliases[key] = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const alias of aliases[key]) {
|
|
|
|
aliases[alias] = [key].concat(
|
|
|
|
aliases[key].filter((y: string): boolean => alias !== y)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.string !== undefined) {
|
|
|
|
const stringArgs =
|
|
|
|
typeof options.string === "string" ? [options.string] : options.string;
|
|
|
|
|
|
|
|
stringArgs.filter(Boolean).forEach(function(key): void {
|
2018-12-19 13:06:31 -05:00
|
|
|
flags.strings[key] = true;
|
|
|
|
if (aliases[key]) {
|
2019-05-30 08:59:30 -04:00
|
|
|
aliases[key].forEach(
|
|
|
|
(alias: string): void => {
|
|
|
|
flags.strings[alias] = true;
|
|
|
|
}
|
|
|
|
);
|
2018-12-19 13:06:31 -05:00
|
|
|
}
|
|
|
|
});
|
2019-05-30 08:59:30 -04:00
|
|
|
}
|
2018-12-19 13:06:31 -05:00
|
|
|
|
|
|
|
const defaults = options.default!;
|
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
const argv: { [key: string]: unknown[] } = { _: [] };
|
2018-12-19 13:06:31 -05:00
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
function argDefined(key: string, arg: string): boolean {
|
2018-12-19 13:06:31 -05:00
|
|
|
return (
|
|
|
|
(flags.allBools && /^--[^=]+$/.test(arg)) ||
|
|
|
|
flags.bools[key] ||
|
2019-05-30 08:59:30 -04:00
|
|
|
!!flags.strings[key] ||
|
|
|
|
!!aliases[key]
|
2018-12-19 13:06:31 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
function setKey(obj: NestedMapping, keys: string[], value: unknown): void {
|
2018-12-19 13:06:31 -05:00
|
|
|
let o = obj;
|
2019-04-24 07:41:23 -04:00
|
|
|
keys.slice(0, -1).forEach(function(key): void {
|
2019-05-30 08:59:30 -04:00
|
|
|
if (o[key] === undefined) {
|
|
|
|
o[key] = {};
|
|
|
|
}
|
|
|
|
o = o[key] as NestedMapping;
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
const key = keys[keys.length - 1];
|
|
|
|
if (
|
|
|
|
o[key] === undefined ||
|
|
|
|
flags.bools[key] ||
|
|
|
|
typeof o[key] === "boolean"
|
|
|
|
) {
|
|
|
|
o[key] = value;
|
|
|
|
} else if (Array.isArray(o[key])) {
|
2019-05-30 08:59:30 -04:00
|
|
|
(o[key] as unknown[]).push(value);
|
2018-12-19 13:06:31 -05:00
|
|
|
} else {
|
|
|
|
o[key] = [o[key], value];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
function setArg(
|
|
|
|
key: string,
|
|
|
|
val: unknown,
|
|
|
|
arg: string | undefined = undefined
|
|
|
|
): void {
|
2019-03-12 01:51:51 -04:00
|
|
|
if (arg && flags.unknownFn && !argDefined(key, arg)) {
|
|
|
|
if (flags.unknownFn(arg) === false) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const value = !flags.strings[key] && isNumber(val) ? Number(val) : val;
|
|
|
|
setKey(argv, key.split("."), value);
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
(aliases[key] || []).forEach(function(x): void {
|
2019-03-12 01:51:51 -04:00
|
|
|
setKey(argv, x.split("."), value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
function aliasIsBoolean(key: string): boolean {
|
2019-04-24 07:41:23 -04:00
|
|
|
return aliases[key].some(function(x): boolean {
|
2018-12-19 13:06:31 -05:00
|
|
|
return flags.bools[x];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
Object.keys(flags.bools).forEach(function(key): void {
|
2019-03-12 01:51:51 -04:00
|
|
|
setArg(key, defaults[key] === undefined ? false : defaults[key]);
|
|
|
|
});
|
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
let notFlags: string[] = [];
|
2019-03-12 01:51:51 -04:00
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
// all args after "--" are not parsed
|
2019-03-12 01:51:51 -04:00
|
|
|
if (args.indexOf("--") !== -1) {
|
|
|
|
notFlags = args.slice(args.indexOf("--") + 1);
|
|
|
|
args = args.slice(0, args.indexOf("--"));
|
|
|
|
}
|
|
|
|
|
2018-12-19 13:06:31 -05:00
|
|
|
for (let i = 0; i < args.length; i++) {
|
|
|
|
const arg = args[i];
|
|
|
|
|
|
|
|
if (/^--.+=/.test(arg)) {
|
|
|
|
// Using [\s\S] instead of . because js doesn't support the
|
|
|
|
// 'dotall' regex modifier. See:
|
|
|
|
// http://stackoverflow.com/a/1068308/13216
|
2019-05-30 08:59:30 -04:00
|
|
|
const m = arg.match(/^--([^=]+)=([\s\S]*)$/)!;
|
2018-12-19 13:06:31 -05:00
|
|
|
const key = m[1];
|
2019-05-30 08:59:30 -04:00
|
|
|
const value = m[2];
|
|
|
|
|
2018-12-19 13:06:31 -05:00
|
|
|
if (flags.bools[key]) {
|
2019-05-30 08:59:30 -04:00
|
|
|
const booleanValue = value !== "false";
|
|
|
|
setArg(key, booleanValue, arg);
|
|
|
|
} else {
|
|
|
|
setArg(key, value, arg);
|
2018-12-19 13:06:31 -05:00
|
|
|
}
|
|
|
|
} else if (/^--no-.+/.test(arg)) {
|
2019-05-30 08:59:30 -04:00
|
|
|
const key = arg.match(/^--no-(.+)/)![1];
|
2018-12-19 13:06:31 -05:00
|
|
|
setArg(key, false, arg);
|
|
|
|
} else if (/^--.+/.test(arg)) {
|
2019-05-30 08:59:30 -04:00
|
|
|
const key = arg.match(/^--(.+)/)![1];
|
2018-12-19 13:06:31 -05:00
|
|
|
const next = args[i + 1];
|
|
|
|
if (
|
|
|
|
next !== undefined &&
|
|
|
|
!/^-/.test(next) &&
|
|
|
|
!flags.bools[key] &&
|
|
|
|
!flags.allBools &&
|
|
|
|
(aliases[key] ? !aliasIsBoolean(key) : true)
|
|
|
|
) {
|
|
|
|
setArg(key, next, arg);
|
|
|
|
i++;
|
|
|
|
} else if (/^(true|false)$/.test(next)) {
|
|
|
|
setArg(key, next === "true", arg);
|
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
setArg(key, flags.strings[key] ? "" : true, arg);
|
|
|
|
}
|
|
|
|
} else if (/^-[^-]+/.test(arg)) {
|
|
|
|
const letters = arg.slice(1, -1).split("");
|
|
|
|
|
|
|
|
let broken = false;
|
|
|
|
for (let j = 0; j < letters.length; j++) {
|
|
|
|
const next = arg.slice(j + 2);
|
|
|
|
|
|
|
|
if (next === "-") {
|
|
|
|
setArg(letters[j], next, arg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (/[A-Za-z]/.test(letters[j]) && /=/.test(next)) {
|
|
|
|
setArg(letters[j], next.split("=")[1], arg);
|
|
|
|
broken = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
/[A-Za-z]/.test(letters[j]) &&
|
|
|
|
/-?\d+(\.\d*)?(e-?\d+)?$/.test(next)
|
|
|
|
) {
|
|
|
|
setArg(letters[j], next, arg);
|
|
|
|
broken = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (letters[j + 1] && letters[j + 1].match(/\W/)) {
|
|
|
|
setArg(letters[j], arg.slice(j + 2), arg);
|
|
|
|
broken = true;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
setArg(letters[j], flags.strings[letters[j]] ? "" : true, arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const key = arg.slice(-1)[0];
|
|
|
|
if (!broken && key !== "-") {
|
|
|
|
if (
|
|
|
|
args[i + 1] &&
|
|
|
|
!/^(-|--)[^-]/.test(args[i + 1]) &&
|
|
|
|
!flags.bools[key] &&
|
|
|
|
(aliases[key] ? !aliasIsBoolean(key) : true)
|
|
|
|
) {
|
|
|
|
setArg(key, args[i + 1], arg);
|
|
|
|
i++;
|
|
|
|
} else if (args[i + 1] && /true|false/.test(args[i + 1])) {
|
|
|
|
setArg(key, args[i + 1] === "true", arg);
|
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
setArg(key, flags.strings[key] ? "" : true, arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!flags.unknownFn || flags.unknownFn(arg) !== false) {
|
|
|
|
argv._.push(flags.strings["_"] || !isNumber(arg) ? arg : Number(arg));
|
|
|
|
}
|
|
|
|
if (options.stopEarly) {
|
|
|
|
argv._.push.apply(argv._, args.slice(i + 1));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
Object.keys(defaults).forEach(function(key): void {
|
2018-12-19 13:06:31 -05:00
|
|
|
if (!hasKey(argv, key.split("."))) {
|
|
|
|
setKey(argv, key.split("."), defaults[key]);
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
(aliases[key] || []).forEach(function(x): void {
|
2018-12-19 13:06:31 -05:00
|
|
|
setKey(argv, x.split("."), defaults[key]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (options["--"]) {
|
2019-03-12 01:51:51 -04:00
|
|
|
argv["--"] = [];
|
2019-04-24 07:41:23 -04:00
|
|
|
notFlags.forEach(function(key): void {
|
2018-12-19 13:06:31 -05:00
|
|
|
argv["--"].push(key);
|
|
|
|
});
|
|
|
|
} else {
|
2019-04-24 07:41:23 -04:00
|
|
|
notFlags.forEach(function(key): void {
|
2018-12-19 13:06:31 -05:00
|
|
|
argv._.push(key);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return argv;
|
|
|
|
}
|