2020-03-08 06:27:23 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2020-06-07 09:20:33 -04:00
|
|
|
import { assert } from "../_util/assert.ts";
|
2020-02-08 15:15:59 -05:00
|
|
|
|
2020-03-08 06:27:23 -04:00
|
|
|
export interface Args {
|
|
|
|
/** Contains all the arguments that didn't have an option associated with
|
|
|
|
* them. */
|
|
|
|
_: Array<string | number>;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
2018-12-19 13:06:31 -05:00
|
|
|
export interface ArgParsingOptions {
|
2020-03-08 06:27:23 -04:00
|
|
|
/** When `true`, populate the result `_` with everything before the `--` and
|
|
|
|
* the result `['--']` with everything after the `--`. Here's an example:
|
|
|
|
*
|
2020-05-09 08:34:47 -04:00
|
|
|
* // $ deno run example.ts -- a arg1
|
2020-03-08 06:27:23 -04:00
|
|
|
* import { parse } from "https://deno.land/std/flags/mod.ts";
|
2020-06-12 15:23:38 -04:00
|
|
|
* console.dir(parse(Deno.args, { "--": false }));
|
2020-05-09 08:34:47 -04:00
|
|
|
* // output: { _: [ "a", "arg1" ] }
|
2020-06-12 15:23:38 -04:00
|
|
|
* console.dir(parse(Deno.args, { "--": true }));
|
2020-05-09 08:34:47 -04:00
|
|
|
* // output: { _: [], --: [ "a", "arg1" ] }
|
2020-03-08 06:27:23 -04:00
|
|
|
*
|
|
|
|
* Defaults to `false`.
|
|
|
|
*/
|
2020-03-10 12:08:58 -04:00
|
|
|
"--"?: boolean;
|
2020-03-08 06:27:23 -04:00
|
|
|
|
|
|
|
/** An object mapping string names to strings or arrays of string argument
|
|
|
|
* names to use as aliases */
|
2020-03-10 12:08:58 -04:00
|
|
|
alias?: Record<string, string | string[]>;
|
2020-03-08 06:27:23 -04:00
|
|
|
|
|
|
|
/** A boolean, string or array of strings to always treat as booleans. If
|
|
|
|
* `true` will treat all double hyphenated arguments without equal signs as
|
|
|
|
* `boolean` (e.g. affects `--foo`, not `-f` or `--foo=bar`) */
|
2020-03-10 12:08:58 -04:00
|
|
|
boolean?: boolean | string | string[];
|
2020-03-08 06:27:23 -04:00
|
|
|
|
|
|
|
/** An object mapping string argument names to default values. */
|
2020-03-10 12:08:58 -04:00
|
|
|
default?: Record<string, unknown>;
|
2020-03-08 06:27:23 -04:00
|
|
|
|
|
|
|
/** When `true`, populate the result `_` with everything after the first
|
|
|
|
* non-option. */
|
2020-03-10 12:08:58 -04:00
|
|
|
stopEarly?: boolean;
|
2018-12-19 13:06:31 -05:00
|
|
|
|
2020-03-08 06:27:23 -04:00
|
|
|
/** A string or array of strings argument names to always treat as strings. */
|
2020-03-10 12:08:58 -04:00
|
|
|
string?: string | string[];
|
2020-03-08 06:27:23 -04:00
|
|
|
|
|
|
|
/** A function which is invoked with a command line parameter not defined in
|
|
|
|
* the `options` configuration object. If the function returns `false`, the
|
|
|
|
* unknown option is not added to `parsedArgs`. */
|
2020-04-06 09:27:10 -04:00
|
|
|
unknown?: (arg: string, key?: string, value?: unknown) => unknown;
|
2020-03-08 06:27:23 -04:00
|
|
|
}
|
2018-12-19 13:06:31 -05:00
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
interface Flags {
|
2020-03-08 06:27:23 -04:00
|
|
|
bools: Record<string, boolean>;
|
|
|
|
strings: Record<string, boolean>;
|
2020-04-06 09:27:10 -04:00
|
|
|
unknownFn: (arg: string, key?: string, value?: unknown) => unknown;
|
2019-05-30 08:59:30 -04:00
|
|
|
allBools: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface NestedMapping {
|
|
|
|
[key: string]: NestedMapping | unknown;
|
|
|
|
}
|
|
|
|
|
2020-03-08 06:27:23 -04:00
|
|
|
function get<T>(obj: Record<string, T>, key: string): T | undefined {
|
2019-09-08 17:18:39 -04:00
|
|
|
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
|
|
return obj[key];
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 06:27:23 -04:00
|
|
|
|
|
|
|
function getForce<T>(obj: Record<string, T>, key: string): T {
|
2020-02-08 15:15:59 -05:00
|
|
|
const v = get(obj, key);
|
|
|
|
assert(v != null);
|
|
|
|
return v;
|
|
|
|
}
|
2019-09-08 17:18:39 -04:00
|
|
|
|
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;
|
2020-03-28 13:03:49 -04:00
|
|
|
keys.slice(0, -1).forEach((key) => {
|
2020-03-08 06:27:23 -04:00
|
|
|
o = (get(o, key) ?? {}) as NestedMapping;
|
2019-03-12 01:51:51 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
const key = keys[keys.length - 1];
|
|
|
|
return key in o;
|
|
|
|
}
|
|
|
|
|
2020-03-08 06:27:23 -04:00
|
|
|
/** Take a set of command line arguments, with an optional set of options, and
|
|
|
|
* return an object representation of those argument.
|
|
|
|
*
|
|
|
|
* const parsedArgs = parse(Deno.args);
|
|
|
|
*/
|
2018-12-21 13:02:52 -05:00
|
|
|
export function parse(
|
2020-03-08 06:27:23 -04:00
|
|
|
args: string[],
|
|
|
|
{
|
|
|
|
"--": doubleDash = false,
|
|
|
|
alias = {},
|
|
|
|
boolean = false,
|
|
|
|
default: defaults = {},
|
|
|
|
stopEarly = false,
|
|
|
|
string = [],
|
2020-04-06 09:27:10 -04:00
|
|
|
unknown = (i: string): unknown => i,
|
2020-07-14 15:24:17 -04:00
|
|
|
}: ArgParsingOptions = {},
|
2020-03-08 06:27:23 -04:00
|
|
|
): Args {
|
2019-05-30 08:59:30 -04:00
|
|
|
const flags: Flags = {
|
2018-12-19 13:06:31 -05:00
|
|
|
bools: {},
|
|
|
|
strings: {},
|
2020-03-08 06:27:23 -04:00
|
|
|
unknownFn: unknown,
|
2020-03-28 13:03:49 -04:00
|
|
|
allBools: false,
|
2018-12-19 13:06:31 -05:00
|
|
|
};
|
|
|
|
|
2020-03-08 06:27:23 -04:00
|
|
|
if (boolean !== undefined) {
|
|
|
|
if (typeof boolean === "boolean") {
|
|
|
|
flags.allBools = !!boolean;
|
2019-05-30 08:59:30 -04:00
|
|
|
} else {
|
2020-03-08 06:27:23 -04:00
|
|
|
const booleanArgs = typeof boolean === "string" ? [boolean] : boolean;
|
2019-05-30 08:59:30 -04:00
|
|
|
|
2020-03-08 06:27:23 -04:00
|
|
|
for (const key of booleanArgs.filter(Boolean)) {
|
2019-11-13 13:42:34 -05:00
|
|
|
flags.bools[key] = true;
|
2020-03-08 06:27:23 -04:00
|
|
|
}
|
2019-05-30 08:59:30 -04:00
|
|
|
}
|
2018-12-19 13:06:31 -05:00
|
|
|
}
|
|
|
|
|
2020-03-08 06:27:23 -04:00
|
|
|
const aliases: Record<string, string[]> = {};
|
|
|
|
if (alias !== undefined) {
|
|
|
|
for (const key in alias) {
|
|
|
|
const val = getForce(alias, key);
|
2019-05-30 08:59:30 -04:00
|
|
|
if (typeof val === "string") {
|
|
|
|
aliases[key] = [val];
|
|
|
|
} else {
|
|
|
|
aliases[key] = val;
|
|
|
|
}
|
2020-02-08 15:15:59 -05:00
|
|
|
for (const alias of getForce(aliases, key)) {
|
2020-03-28 13:03:49 -04:00
|
|
|
aliases[alias] = [key].concat(aliases[key].filter((y) => alias !== y));
|
2019-05-30 08:59:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 06:27:23 -04:00
|
|
|
if (string !== undefined) {
|
|
|
|
const stringArgs = typeof string === "string" ? [string] : string;
|
2019-05-30 08:59:30 -04:00
|
|
|
|
2020-03-08 06:27:23 -04:00
|
|
|
for (const key of stringArgs.filter(Boolean)) {
|
2018-12-19 13:06:31 -05:00
|
|
|
flags.strings[key] = true;
|
2019-09-08 17:18:39 -04:00
|
|
|
const alias = get(aliases, key);
|
|
|
|
if (alias) {
|
2020-03-08 06:27:23 -04:00
|
|
|
for (const al of alias) {
|
|
|
|
flags.strings[al] = true;
|
|
|
|
}
|
2018-12-19 13:06:31 -05:00
|
|
|
}
|
2020-03-08 06:27:23 -04:00
|
|
|
}
|
2019-05-30 08:59:30 -04:00
|
|
|
}
|
2018-12-19 13:06:31 -05:00
|
|
|
|
2020-03-08 06:27:23 -04:00
|
|
|
const argv: Args = { _: [] };
|
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)) ||
|
2019-09-08 17:18:39 -04:00
|
|
|
get(flags.bools, key) ||
|
|
|
|
!!get(flags.strings, key) ||
|
|
|
|
!!get(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;
|
2020-03-28 13:03:49 -04:00
|
|
|
keys.slice(0, -1).forEach(function (key): void {
|
2019-09-08 17:18:39 -04:00
|
|
|
if (get(o, key) === undefined) {
|
2019-05-30 08:59:30 -04:00
|
|
|
o[key] = {};
|
|
|
|
}
|
2019-09-08 17:18:39 -04:00
|
|
|
o = get(o, key) as NestedMapping;
|
2018-12-19 13:06:31 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
const key = keys[keys.length - 1];
|
|
|
|
if (
|
2019-09-08 17:18:39 -04:00
|
|
|
get(o, key) === undefined ||
|
|
|
|
get(flags.bools, key) ||
|
|
|
|
typeof get(o, key) === "boolean"
|
2018-12-19 13:06:31 -05:00
|
|
|
) {
|
|
|
|
o[key] = value;
|
2019-09-08 17:18:39 -04:00
|
|
|
} else if (Array.isArray(get(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 {
|
2019-09-08 17:18:39 -04:00
|
|
|
o[key] = [get(o, key), value];
|
2018-12-19 13:06:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
function setArg(
|
|
|
|
key: string,
|
|
|
|
val: unknown,
|
2020-07-14 15:24:17 -04:00
|
|
|
arg: string | undefined = undefined,
|
2019-05-30 08:59:30 -04:00
|
|
|
): void {
|
2019-03-12 01:51:51 -04:00
|
|
|
if (arg && flags.unknownFn && !argDefined(key, arg)) {
|
2020-04-06 09:27:10 -04:00
|
|
|
if (flags.unknownFn(arg, key, val) === false) return;
|
2019-03-12 01:51:51 -04:00
|
|
|
}
|
|
|
|
|
2019-09-08 17:18:39 -04:00
|
|
|
const value = !get(flags.strings, key) && isNumber(val) ? Number(val) : val;
|
2019-03-12 01:51:51 -04:00
|
|
|
setKey(argv, key.split("."), value);
|
|
|
|
|
2020-03-08 06:27:23 -04:00
|
|
|
const alias = get(aliases, key);
|
|
|
|
if (alias) {
|
|
|
|
for (const x of alias) {
|
|
|
|
setKey(argv, x.split("."), value);
|
|
|
|
}
|
|
|
|
}
|
2019-03-12 01:51:51 -04:00
|
|
|
}
|
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
function aliasIsBoolean(key: string): boolean {
|
2020-03-08 06:27:23 -04:00
|
|
|
return getForce(aliases, key).some(
|
2020-07-14 15:24:17 -04:00
|
|
|
(x) => typeof get(flags.bools, x) === "boolean",
|
2020-03-08 06:27:23 -04:00
|
|
|
);
|
2018-12-19 13:06:31 -05:00
|
|
|
}
|
|
|
|
|
2020-03-08 06:27:23 -04:00
|
|
|
for (const key of Object.keys(flags.bools)) {
|
2019-03-12 01:51:51 -04:00
|
|
|
setArg(key, defaults[key] === undefined ? false : defaults[key]);
|
2020-03-08 06:27:23 -04:00
|
|
|
}
|
2019-03-12 01:51:51 -04:00
|
|
|
|
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
|
2020-03-08 06:27:23 -04:00
|
|
|
if (args.includes("--")) {
|
2019-03-12 01:51:51 -04:00
|
|
|
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)) {
|
2020-03-08 06:27:23 -04:00
|
|
|
const m = arg.match(/^--([^=]+)=(.*)$/s);
|
2020-02-08 15:15:59 -05:00
|
|
|
assert(m != null);
|
2020-03-08 06:27:23 -04:00
|
|
|
const [, key, value] = m;
|
2019-05-30 08:59:30 -04:00
|
|
|
|
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)) {
|
2020-02-08 15:15:59 -05:00
|
|
|
const m = arg.match(/^--no-(.+)/);
|
|
|
|
assert(m != null);
|
|
|
|
setArg(m[1], false, arg);
|
2018-12-19 13:06:31 -05:00
|
|
|
} else if (/^--.+/.test(arg)) {
|
2020-02-08 15:15:59 -05:00
|
|
|
const m = arg.match(/^--(.+)/);
|
|
|
|
assert(m != null);
|
2020-03-08 06:27:23 -04:00
|
|
|
const [, key] = m;
|
2018-12-19 13:06:31 -05:00
|
|
|
const next = args[i + 1];
|
|
|
|
if (
|
|
|
|
next !== undefined &&
|
|
|
|
!/^-/.test(next) &&
|
2019-09-08 17:18:39 -04:00
|
|
|
!get(flags.bools, key) &&
|
2018-12-19 13:06:31 -05:00
|
|
|
!flags.allBools &&
|
2019-09-08 17:18:39 -04:00
|
|
|
(get(aliases, key) ? !aliasIsBoolean(key) : true)
|
2018-12-19 13:06:31 -05:00
|
|
|
) {
|
|
|
|
setArg(key, next, arg);
|
|
|
|
i++;
|
|
|
|
} else if (/^(true|false)$/.test(next)) {
|
|
|
|
setArg(key, next === "true", arg);
|
|
|
|
i++;
|
|
|
|
} else {
|
2019-09-08 17:18:39 -04:00
|
|
|
setArg(key, get(flags.strings, key) ? "" : true, arg);
|
2018-12-19 13:06:31 -05:00
|
|
|
}
|
|
|
|
} 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)) {
|
2020-11-02 17:17:26 -05:00
|
|
|
setArg(letters[j], next.split(/=(.+)/)[1], arg);
|
2018-12-19 13:06:31 -05:00
|
|
|
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 {
|
2019-09-08 17:18:39 -04:00
|
|
|
setArg(letters[j], get(flags.strings, letters[j]) ? "" : true, arg);
|
2018-12-19 13:06:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 06:27:23 -04:00
|
|
|
const [key] = arg.slice(-1);
|
2018-12-19 13:06:31 -05:00
|
|
|
if (!broken && key !== "-") {
|
|
|
|
if (
|
|
|
|
args[i + 1] &&
|
|
|
|
!/^(-|--)[^-]/.test(args[i + 1]) &&
|
2019-09-08 17:18:39 -04:00
|
|
|
!get(flags.bools, key) &&
|
|
|
|
(get(aliases, key) ? !aliasIsBoolean(key) : true)
|
2018-12-19 13:06:31 -05:00
|
|
|
) {
|
|
|
|
setArg(key, args[i + 1], arg);
|
|
|
|
i++;
|
2019-09-04 10:06:29 -04:00
|
|
|
} else if (args[i + 1] && /^(true|false)$/.test(args[i + 1])) {
|
2018-12-19 13:06:31 -05:00
|
|
|
setArg(key, args[i + 1] === "true", arg);
|
|
|
|
i++;
|
|
|
|
} else {
|
2019-09-08 17:18:39 -04:00
|
|
|
setArg(key, get(flags.strings, key) ? "" : true, arg);
|
2018-12-19 13:06:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!flags.unknownFn || flags.unknownFn(arg) !== false) {
|
2020-03-08 06:27:23 -04:00
|
|
|
argv._.push(flags.strings["_"] ?? !isNumber(arg) ? arg : Number(arg));
|
2018-12-19 13:06:31 -05:00
|
|
|
}
|
2020-03-08 06:27:23 -04:00
|
|
|
if (stopEarly) {
|
2019-10-05 12:02:34 -04:00
|
|
|
argv._.push(...args.slice(i + 1));
|
2018-12-19 13:06:31 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 06:27:23 -04:00
|
|
|
for (const key of Object.keys(defaults)) {
|
2018-12-19 13:06:31 -05:00
|
|
|
if (!hasKey(argv, key.split("."))) {
|
|
|
|
setKey(argv, key.split("."), defaults[key]);
|
|
|
|
|
2020-03-08 06:27:23 -04:00
|
|
|
if (aliases[key]) {
|
|
|
|
for (const x of aliases[key]) {
|
|
|
|
setKey(argv, x.split("."), defaults[key]);
|
|
|
|
}
|
|
|
|
}
|
2018-12-19 13:06:31 -05:00
|
|
|
}
|
2020-03-08 06:27:23 -04:00
|
|
|
}
|
2018-12-19 13:06:31 -05:00
|
|
|
|
2020-03-08 06:27:23 -04:00
|
|
|
if (doubleDash) {
|
2019-03-12 01:51:51 -04:00
|
|
|
argv["--"] = [];
|
2020-03-08 06:27:23 -04:00
|
|
|
for (const key of notFlags) {
|
2018-12-19 13:06:31 -05:00
|
|
|
argv["--"].push(key);
|
2020-03-08 06:27:23 -04:00
|
|
|
}
|
2018-12-19 13:06:31 -05:00
|
|
|
} else {
|
2020-03-08 06:27:23 -04:00
|
|
|
for (const key of notFlags) {
|
2018-12-19 13:06:31 -05:00
|
|
|
argv._.push(key);
|
2020-03-08 06:27:23 -04:00
|
|
|
}
|
2018-12-19 13:06:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return argv;
|
|
|
|
}
|