diff options
Diffstat (limited to 'std/flags')
-rw-r--r-- | std/flags/README.md | 67 | ||||
-rwxr-xr-x | std/flags/all_bool_test.ts | 33 | ||||
-rwxr-xr-x | std/flags/bool_test.ts | 203 | ||||
-rwxr-xr-x | std/flags/dash_test.ts | 28 | ||||
-rwxr-xr-x | std/flags/default_bool_test.ts | 32 | ||||
-rwxr-xr-x | std/flags/dotted_test.ts | 23 | ||||
-rwxr-xr-x | std/flags/kv_short_test.ts | 13 | ||||
-rwxr-xr-x | std/flags/long_test.ts | 19 | ||||
-rw-r--r-- | std/flags/mod.ts | 355 | ||||
-rwxr-xr-x | std/flags/num_test.ts | 40 | ||||
-rwxr-xr-x | std/flags/parse_test.ts | 206 | ||||
-rwxr-xr-x | std/flags/short_test.ts | 45 | ||||
-rwxr-xr-x | std/flags/stop_early_test.ts | 15 | ||||
-rw-r--r-- | std/flags/test.ts | 2 | ||||
-rwxr-xr-x | std/flags/unknown_test.ts | 110 | ||||
-rwxr-xr-x | std/flags/whitespace_test.ts | 7 |
16 files changed, 0 insertions, 1198 deletions
diff --git a/std/flags/README.md b/std/flags/README.md deleted file mode 100644 index 7b8e508d4..000000000 --- a/std/flags/README.md +++ /dev/null @@ -1,67 +0,0 @@ -# flags - -Command line arguments parser for Deno based on minimist. - -# Example - -```ts -import { parse } from "https://deno.land/std@$STD_VERSION/flags/mod.ts"; - -console.dir(parse(Deno.args)); -``` - -``` -$ deno run https://deno.land/std/examples/flags.ts -a beep -b boop -{ _: [], a: 'beep', b: 'boop' } -``` - -``` -$ deno run https://deno.land/std/examples/flags.ts -x 3 -y 4 -n5 -abc --beep=boop foo bar baz -{ _: [ 'foo', 'bar', 'baz' ], - x: 3, - y: 4, - n: 5, - a: true, - b: true, - c: true, - beep: 'boop' } -``` - -# API - -## const parsedArgs = parse(args, options = {}); - -`parsedArgs._` contains all the arguments that didn't have an option associated -with them. - -Numeric-looking arguments will be returned as numbers unless `options.string` or -`options.boolean` is set for that argument name. - -Any arguments after `'--'` will not be parsed and will end up in `parsedArgs._`. - -options can be: - -- `options.string` - a string or array of strings argument names to always treat - as strings. -- `options.boolean` - 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`). -- `options.alias` - an object mapping string names to strings or arrays of - string argument names to use as aliases. -- `options.default` - an object mapping string argument names to default values. -- `options.stopEarly` - when true, populate `parsedArgs._` with everything after - the first non-option. -- `options['--']` - when true, populate `parsedArgs._` with everything before - the `--` and `parsedArgs['--']` with everything after the `--`. Here's an - example: - ```ts - // $ deno run example.ts -- a arg1 - import { parse } from "https://deno.land/std@$STD_VERSION/flags/mod.ts"; - console.dir(parse(Deno.args, { "--": false })); - // output: { _: [ "a", "arg1" ] } - console.dir(parse(Deno.args, { "--": true })); - // output: { _: [], --: [ "a", "arg1" ] } - ``` -- `options.unknown` - 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`. diff --git a/std/flags/all_bool_test.ts b/std/flags/all_bool_test.ts deleted file mode 100755 index 3da80cb37..000000000 --- a/std/flags/all_bool_test.ts +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { assertEquals } from "../testing/asserts.ts"; -import { parse } from "./mod.ts"; - -// flag boolean true (default all --args to boolean) -Deno.test("flagBooleanTrue", function (): void { - const argv = parse(["moo", "--honk", "cow"], { - boolean: true, - }); - - assertEquals(argv, { - honk: true, - _: ["moo", "cow"], - }); - - assertEquals(typeof argv.honk, "boolean"); -}); - -// flag boolean true only affects double hyphen arguments without equals signs -Deno.test("flagBooleanTrueOnlyAffectsDoubleDash", function (): void { - const argv = parse(["moo", "--honk", "cow", "-p", "55", "--tacos=good"], { - boolean: true, - }); - - assertEquals(argv, { - honk: true, - tacos: "good", - p: 55, - _: ["moo", "cow"], - }); - - assertEquals(typeof argv.honk, "boolean"); -}); diff --git a/std/flags/bool_test.ts b/std/flags/bool_test.ts deleted file mode 100755 index bafa21adf..000000000 --- a/std/flags/bool_test.ts +++ /dev/null @@ -1,203 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { assertEquals } from "../testing/asserts.ts"; -import { parse } from "./mod.ts"; - -Deno.test("flagBooleanDefaultFalse", function (): void { - const argv = parse(["moo"], { - boolean: ["t", "verbose"], - default: { verbose: false, t: false }, - }); - - assertEquals(argv, { - verbose: false, - t: false, - _: ["moo"], - }); - - assertEquals(typeof argv.verbose, "boolean"); - assertEquals(typeof argv.t, "boolean"); -}); - -Deno.test("booleanGroups", function (): void { - const argv = parse(["-x", "-z", "one", "two", "three"], { - boolean: ["x", "y", "z"], - }); - - assertEquals(argv, { - x: true, - y: false, - z: true, - _: ["one", "two", "three"], - }); - - assertEquals(typeof argv.x, "boolean"); - assertEquals(typeof argv.y, "boolean"); - assertEquals(typeof argv.z, "boolean"); -}); - -Deno.test("booleanAndAliasWithChainableApi", function (): void { - const aliased = ["-h", "derp"]; - const regular = ["--herp", "derp"]; - const aliasedArgv = parse(aliased, { - boolean: "herp", - alias: { h: "herp" }, - }); - const propertyArgv = parse(regular, { - boolean: "herp", - alias: { h: "herp" }, - }); - const expected = { - herp: true, - h: true, - _: ["derp"], - }; - - assertEquals(aliasedArgv, expected); - assertEquals(propertyArgv, expected); -}); - -Deno.test("booleanAndAliasWithOptionsHash", function (): void { - const aliased = ["-h", "derp"]; - const regular = ["--herp", "derp"]; - const opts = { - alias: { h: "herp" }, - boolean: "herp", - }; - const aliasedArgv = parse(aliased, opts); - const propertyArgv = parse(regular, opts); - const expected = { - herp: true, - h: true, - _: ["derp"], - }; - assertEquals(aliasedArgv, expected); - assertEquals(propertyArgv, expected); -}); - -Deno.test("booleanAndAliasArrayWithOptionsHash", function (): void { - const aliased = ["-h", "derp"]; - const regular = ["--herp", "derp"]; - const alt = ["--harp", "derp"]; - const opts = { - alias: { h: ["herp", "harp"] }, - boolean: "h", - }; - const aliasedArgv = parse(aliased, opts); - const propertyArgv = parse(regular, opts); - const altPropertyArgv = parse(alt, opts); - const expected = { - harp: true, - herp: true, - h: true, - _: ["derp"], - }; - assertEquals(aliasedArgv, expected); - assertEquals(propertyArgv, expected); - assertEquals(altPropertyArgv, expected); -}); - -Deno.test("booleanAndAliasUsingExplicitTrue", function (): void { - const aliased = ["-h", "true"]; - const regular = ["--herp", "true"]; - const opts = { - alias: { h: "herp" }, - boolean: "h", - }; - const aliasedArgv = parse(aliased, opts); - const propertyArgv = parse(regular, opts); - const expected = { - herp: true, - h: true, - _: [], - }; - - assertEquals(aliasedArgv, expected); - assertEquals(propertyArgv, expected); -}); - -// regression, see https://github.com/substack/node-optimist/issues/71 -// boolean and --x=true -Deno.test("booleanAndNonBoolean", function (): void { - const parsed = parse(["--boool", "--other=true"], { - boolean: "boool", - }); - - assertEquals(parsed.boool, true); - assertEquals(parsed.other, "true"); - - const parsed2 = parse(["--boool", "--other=false"], { - boolean: "boool", - }); - - assertEquals(parsed2.boool, true); - assertEquals(parsed2.other, "false"); -}); - -Deno.test("booleanParsingTrue", function (): void { - const parsed = parse(["--boool=true"], { - default: { - boool: false, - }, - boolean: ["boool"], - }); - - assertEquals(parsed.boool, true); -}); - -Deno.test("booleanParsingFalse", function (): void { - const parsed = parse(["--boool=false"], { - default: { - boool: true, - }, - boolean: ["boool"], - }); - - assertEquals(parsed.boool, false); -}); - -Deno.test("booleanParsingTrueLike", function (): void { - const parsed = parse(["-t", "true123"], { boolean: ["t"] }); - assertEquals(parsed.t, true); - - const parsed2 = parse(["-t", "123"], { boolean: ["t"] }); - assertEquals(parsed2.t, true); - - const parsed3 = parse(["-t", "false123"], { boolean: ["t"] }); - assertEquals(parsed3.t, true); -}); - -Deno.test("booleanNegationAfterBoolean", function (): void { - const parsed = parse(["--foo", "--no-foo"], { boolean: ["foo"] }); - assertEquals(parsed.foo, false); - - const parsed2 = parse(["--foo", "--no-foo", "123"], { boolean: ["foo"] }); - assertEquals(parsed2.foo, false); -}); - -Deno.test("booleanAfterBooleanNegation", function (): void { - const parsed = parse(["--no--foo", "--foo"], { boolean: ["foo"] }); - assertEquals(parsed.foo, true); - - const parsed2 = parse(["--no--foo", "--foo", "123"], { boolean: ["foo"] }); - assertEquals(parsed2.foo, true); -}); - -Deno.test("latestFlagIsBooleanNegation", function (): void { - const parsed = parse(["--no-foo", "--foo", "--no-foo"], { boolean: ["foo"] }); - assertEquals(parsed.foo, false); - - const parsed2 = parse(["--no-foo", "--foo", "--no-foo", "123"], { - boolean: ["foo"], - }); - assertEquals(parsed2.foo, false); -}); - -Deno.test("latestFlagIsBoolean", function (): void { - const parsed = parse(["--foo", "--no-foo", "--foo"], { boolean: ["foo"] }); - assertEquals(parsed.foo, true); - - const parsed2 = parse(["--foo", "--no-foo", "--foo", "123"], { - boolean: ["foo"], - }); - assertEquals(parsed2.foo, true); -}); diff --git a/std/flags/dash_test.ts b/std/flags/dash_test.ts deleted file mode 100755 index 959956d99..000000000 --- a/std/flags/dash_test.ts +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { assertEquals } from "../testing/asserts.ts"; -import { parse } from "./mod.ts"; - -Deno.test("hyphen", function (): void { - assertEquals(parse(["-n", "-"]), { n: "-", _: [] }); - assertEquals(parse(["-"]), { _: ["-"] }); - assertEquals(parse(["-f-"]), { f: "-", _: [] }); - assertEquals(parse(["-b", "-"], { boolean: "b" }), { b: true, _: ["-"] }); - assertEquals(parse(["-s", "-"], { string: "s" }), { s: "-", _: [] }); -}); - -Deno.test("doubleDash", function (): void { - assertEquals(parse(["-a", "--", "b"]), { a: true, _: ["b"] }); - assertEquals(parse(["--a", "--", "b"]), { a: true, _: ["b"] }); - assertEquals(parse(["--a", "--", "b"]), { a: true, _: ["b"] }); -}); - -Deno.test("moveArgsAfterDoubleDashIntoOwnArray", function (): void { - assertEquals( - parse(["--name", "John", "before", "--", "after"], { "--": true }), - { - name: "John", - _: ["before"], - "--": ["after"], - }, - ); -}); diff --git a/std/flags/default_bool_test.ts b/std/flags/default_bool_test.ts deleted file mode 100755 index 625bed3bf..000000000 --- a/std/flags/default_bool_test.ts +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { assertEquals } from "../testing/asserts.ts"; -import { parse } from "./mod.ts"; - -Deno.test("booleanDefaultTrue", function (): void { - const argv = parse([], { - boolean: "sometrue", - default: { sometrue: true }, - }); - assertEquals(argv.sometrue, true); -}); - -Deno.test("booleanDefaultFalse", function (): void { - const argv = parse([], { - boolean: "somefalse", - default: { somefalse: false }, - }); - assertEquals(argv.somefalse, false); -}); - -Deno.test("booleanDefaultNull", function (): void { - const argv = parse([], { - boolean: "maybe", - default: { maybe: null }, - }); - assertEquals(argv.maybe, null); - const argv2 = parse(["--maybe"], { - boolean: "maybe", - default: { maybe: null }, - }); - assertEquals(argv2.maybe, true); -}); diff --git a/std/flags/dotted_test.ts b/std/flags/dotted_test.ts deleted file mode 100755 index aed394198..000000000 --- a/std/flags/dotted_test.ts +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { assertEquals } from "../testing/asserts.ts"; -import { parse } from "./mod.ts"; - -Deno.test("dottedAlias", function (): void { - const argv = parse(["--a.b", "22"], { - default: { "a.b": 11 }, - alias: { "a.b": "aa.bb" }, - }); - assertEquals(argv.a.b, 22); - assertEquals(argv.aa.bb, 22); -}); - -Deno.test("dottedDefault", function (): void { - const argv = parse([], { default: { "a.b": 11 }, alias: { "a.b": "aa.bb" } }); - assertEquals(argv.a.b, 11); - assertEquals(argv.aa.bb, 11); -}); - -Deno.test("dottedDefaultWithNoAlias", function (): void { - const argv = parse([], { default: { "a.b": 11 } }); - assertEquals(argv.a.b, 11); -}); diff --git a/std/flags/kv_short_test.ts b/std/flags/kv_short_test.ts deleted file mode 100755 index 453d8a956..000000000 --- a/std/flags/kv_short_test.ts +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { assertEquals } from "../testing/asserts.ts"; -import { parse } from "./mod.ts"; - -Deno.test("short", function (): void { - const argv = parse(["-b=123"]); - assertEquals(argv, { b: 123, _: [] }); -}); - -Deno.test("multiShort", function (): void { - const argv = parse(["-a=whatever", "-b=robots"]); - assertEquals(argv, { a: "whatever", b: "robots", _: [] }); -}); diff --git a/std/flags/long_test.ts b/std/flags/long_test.ts deleted file mode 100755 index b10d101f6..000000000 --- a/std/flags/long_test.ts +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { assertEquals } from "../testing/asserts.ts"; -import { parse } from "./mod.ts"; - -Deno.test("longOpts", function (): void { - assertEquals(parse(["--bool"]), { bool: true, _: [] }); - assertEquals(parse(["--pow", "xixxle"]), { pow: "xixxle", _: [] }); - assertEquals(parse(["--pow=xixxle"]), { pow: "xixxle", _: [] }); - assertEquals(parse(["--host", "localhost", "--port", "555"]), { - host: "localhost", - port: 555, - _: [], - }); - assertEquals(parse(["--host=localhost", "--port=555"]), { - host: "localhost", - port: 555, - _: [], - }); -}); diff --git a/std/flags/mod.ts b/std/flags/mod.ts deleted file mode 100644 index 7c066f900..000000000 --- a/std/flags/mod.ts +++ /dev/null @@ -1,355 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. - -import { assert } from "../_util/assert.ts"; - -export interface Args { - /** Contains all the arguments that didn't have an option associated with - * them. */ - _: Array<string | number>; - // deno-lint-ignore no-explicit-any - [key: string]: any; -} - -export interface ArgParsingOptions { - /** When `true`, populate the result `_` with everything before the `--` and - * the result `['--']` with everything after the `--`. Here's an example: - * - * // $ deno run example.ts -- a arg1 - * import { parse } from "https://deno.land/std/flags/mod.ts"; - * console.dir(parse(Deno.args, { "--": false })); - * // output: { _: [ "a", "arg1" ] } - * console.dir(parse(Deno.args, { "--": true })); - * // output: { _: [], --: [ "a", "arg1" ] } - * - * Defaults to `false`. - */ - "--"?: boolean; - - /** An object mapping string names to strings or arrays of string argument - * names to use as aliases */ - alias?: Record<string, string | string[]>; - - /** 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`) */ - boolean?: boolean | string | string[]; - - /** An object mapping string argument names to default values. */ - default?: Record<string, unknown>; - - /** When `true`, populate the result `_` with everything after the first - * non-option. */ - stopEarly?: boolean; - - /** A string or array of strings argument names to always treat as strings. */ - string?: string | string[]; - - /** 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`. */ - unknown?: (arg: string, key?: string, value?: unknown) => unknown; -} - -interface Flags { - bools: Record<string, boolean>; - strings: Record<string, boolean>; - unknownFn: (arg: string, key?: string, value?: unknown) => unknown; - allBools: boolean; -} - -interface NestedMapping { - [key: string]: NestedMapping | unknown; -} - -function get<T>(obj: Record<string, T>, key: string): T | undefined { - if (Object.prototype.hasOwnProperty.call(obj, key)) { - return obj[key]; - } -} - -function getForce<T>(obj: Record<string, T>, key: string): T { - const v = get(obj, key); - assert(v != null); - return v; -} - -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)); -} - -function hasKey(obj: NestedMapping, keys: string[]): boolean { - let o = obj; - keys.slice(0, -1).forEach((key) => { - o = (get(o, key) ?? {}) as NestedMapping; - }); - - const key = keys[keys.length - 1]; - return key in o; -} - -/** 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); - */ -export function parse( - args: string[], - { - "--": doubleDash = false, - alias = {}, - boolean = false, - default: defaults = {}, - stopEarly = false, - string = [], - unknown = (i: string): unknown => i, - }: ArgParsingOptions = {}, -): Args { - const flags: Flags = { - bools: {}, - strings: {}, - unknownFn: unknown, - allBools: false, - }; - - if (boolean !== undefined) { - if (typeof boolean === "boolean") { - flags.allBools = !!boolean; - } else { - const booleanArgs = typeof boolean === "string" ? [boolean] : boolean; - - for (const key of booleanArgs.filter(Boolean)) { - flags.bools[key] = true; - } - } - } - - const aliases: Record<string, string[]> = {}; - if (alias !== undefined) { - for (const key in alias) { - const val = getForce(alias, key); - if (typeof val === "string") { - aliases[key] = [val]; - } else { - aliases[key] = val; - } - for (const alias of getForce(aliases, key)) { - aliases[alias] = [key].concat(aliases[key].filter((y) => alias !== y)); - } - } - } - - if (string !== undefined) { - const stringArgs = typeof string === "string" ? [string] : string; - - for (const key of stringArgs.filter(Boolean)) { - flags.strings[key] = true; - const alias = get(aliases, key); - if (alias) { - for (const al of alias) { - flags.strings[al] = true; - } - } - } - } - - const argv: Args = { _: [] }; - - function argDefined(key: string, arg: string): boolean { - return ( - (flags.allBools && /^--[^=]+$/.test(arg)) || - get(flags.bools, key) || - !!get(flags.strings, key) || - !!get(aliases, key) - ); - } - - function setKey(obj: NestedMapping, keys: string[], value: unknown): void { - let o = obj; - keys.slice(0, -1).forEach(function (key): void { - if (get(o, key) === undefined) { - o[key] = {}; - } - o = get(o, key) as NestedMapping; - }); - - const key = keys[keys.length - 1]; - if ( - get(o, key) === undefined || - get(flags.bools, key) || - typeof get(o, key) === "boolean" - ) { - o[key] = value; - } else if (Array.isArray(get(o, key))) { - (o[key] as unknown[]).push(value); - } else { - o[key] = [get(o, key), value]; - } - } - - function setArg( - key: string, - val: unknown, - arg: string | undefined = undefined, - ): void { - if (arg && flags.unknownFn && !argDefined(key, arg)) { - if (flags.unknownFn(arg, key, val) === false) return; - } - - const value = !get(flags.strings, key) && isNumber(val) ? Number(val) : val; - setKey(argv, key.split("."), value); - - const alias = get(aliases, key); - if (alias) { - for (const x of alias) { - setKey(argv, x.split("."), value); - } - } - } - - function aliasIsBoolean(key: string): boolean { - return getForce(aliases, key).some( - (x) => typeof get(flags.bools, x) === "boolean", - ); - } - - for (const key of Object.keys(flags.bools)) { - setArg(key, defaults[key] === undefined ? false : defaults[key]); - } - - let notFlags: string[] = []; - - // all args after "--" are not parsed - if (args.includes("--")) { - notFlags = args.slice(args.indexOf("--") + 1); - args = args.slice(0, args.indexOf("--")); - } - - for (let i = 0; i < args.length; i++) { - const arg = args[i]; - - if (/^--.+=/.test(arg)) { - const m = arg.match(/^--([^=]+)=(.*)$/s); - assert(m != null); - const [, key, value] = m; - - if (flags.bools[key]) { - const booleanValue = value !== "false"; - setArg(key, booleanValue, arg); - } else { - setArg(key, value, arg); - } - } else if (/^--no-.+/.test(arg)) { - const m = arg.match(/^--no-(.+)/); - assert(m != null); - setArg(m[1], false, arg); - } else if (/^--.+/.test(arg)) { - const m = arg.match(/^--(.+)/); - assert(m != null); - const [, key] = m; - const next = args[i + 1]; - if ( - next !== undefined && - !/^-/.test(next) && - !get(flags.bools, key) && - !flags.allBools && - (get(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, get(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], get(flags.strings, letters[j]) ? "" : true, arg); - } - } - - const [key] = arg.slice(-1); - if (!broken && key !== "-") { - if ( - args[i + 1] && - !/^(-|--)[^-]/.test(args[i + 1]) && - !get(flags.bools, key) && - (get(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, get(flags.strings, key) ? "" : true, arg); - } - } - } else { - if (!flags.unknownFn || flags.unknownFn(arg) !== false) { - argv._.push(flags.strings["_"] ?? !isNumber(arg) ? arg : Number(arg)); - } - if (stopEarly) { - argv._.push(...args.slice(i + 1)); - break; - } - } - } - - for (const key of Object.keys(defaults)) { - if (!hasKey(argv, key.split("."))) { - setKey(argv, key.split("."), defaults[key]); - - if (aliases[key]) { - for (const x of aliases[key]) { - setKey(argv, x.split("."), defaults[key]); - } - } - } - } - - if (doubleDash) { - argv["--"] = []; - for (const key of notFlags) { - argv["--"].push(key); - } - } else { - for (const key of notFlags) { - argv._.push(key); - } - } - - return argv; -} diff --git a/std/flags/num_test.ts b/std/flags/num_test.ts deleted file mode 100755 index b57aed409..000000000 --- a/std/flags/num_test.ts +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { assertEquals } from "../testing/asserts.ts"; -import { parse } from "./mod.ts"; - -Deno.test("nums", function (): void { - const argv = parse([ - "-x", - "1234", - "-y", - "5.67", - "-z", - "1e7", - "-w", - "10f", - "--hex", - "0xdeadbeef", - "789", - ]); - assertEquals(argv, { - x: 1234, - y: 5.67, - z: 1e7, - w: "10f", - hex: 0xdeadbeef, - _: [789], - }); - assertEquals(typeof argv.x, "number"); - assertEquals(typeof argv.y, "number"); - assertEquals(typeof argv.z, "number"); - assertEquals(typeof argv.w, "string"); - assertEquals(typeof argv.hex, "number"); - assertEquals(typeof argv._[0], "number"); -}); - -Deno.test("alreadyNumber", function (): void { - const argv = parse(["-x", "1234", "789"]); - assertEquals(argv, { x: 1234, _: [789] }); - assertEquals(typeof argv.x, "number"); - assertEquals(typeof argv._[0], "number"); -}); diff --git a/std/flags/parse_test.ts b/std/flags/parse_test.ts deleted file mode 100755 index 5b36d0857..000000000 --- a/std/flags/parse_test.ts +++ /dev/null @@ -1,206 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { assertEquals } from "../testing/asserts.ts"; -import { parse } from "./mod.ts"; - -Deno.test("parseArgs", function (): void { - assertEquals(parse(["--no-moo"]), { moo: false, _: [] }); - assertEquals(parse(["-v", "a", "-v", "b", "-v", "c"]), { - v: ["a", "b", "c"], - _: [], - }); -}); - -Deno.test("comprehensive", function (): void { - assertEquals( - parse([ - "--name=meowmers", - "bare", - "-cats", - "woo", - "-h", - "awesome", - "--multi=quux", - "--key", - "value", - "-b", - "--bool", - "--no-meep", - "--multi=baz", - "-f=abc=def", - "--foo=---=\\n--+34-=/=", - "-e==", - "--", - "--not-a-flag", - "eek", - ]), - { - c: true, - a: true, - t: true, - e: "=", - f: "abc=def", - foo: "---=\\n--+34-=/=", - s: "woo", - h: "awesome", - b: true, - bool: true, - key: "value", - multi: ["quux", "baz"], - meep: false, - name: "meowmers", - _: ["bare", "--not-a-flag", "eek"], - }, - ); -}); - -Deno.test("flagBoolean", function (): void { - const argv = parse(["-t", "moo"], { boolean: "t" }); - assertEquals(argv, { t: true, _: ["moo"] }); - assertEquals(typeof argv.t, "boolean"); -}); - -Deno.test("flagBooleanValue", function (): void { - const argv = parse(["--verbose", "false", "moo", "-t", "true"], { - boolean: ["t", "verbose"], - default: { verbose: true }, - }); - - assertEquals(argv, { - verbose: false, - t: true, - _: ["moo"], - }); - - assertEquals(typeof argv.verbose, "boolean"); - assertEquals(typeof argv.t, "boolean"); -}); - -Deno.test("newlinesInParams", function (): void { - const args = parse(["-s", "X\nX"]); - assertEquals(args, { _: [], s: "X\nX" }); - - // reproduce in bash: - // VALUE="new - // line" - // deno program.js --s="$VALUE" - const args2 = parse(["--s=X\nX"]); - assertEquals(args2, { _: [], s: "X\nX" }); -}); - -Deno.test("strings", function (): void { - const s = parse(["-s", "0001234"], { string: "s" }).s; - assertEquals(s, "0001234"); - assertEquals(typeof s, "string"); - - const x = parse(["-x", "56"], { string: "x" }).x; - assertEquals(x, "56"); - assertEquals(typeof x, "string"); -}); - -Deno.test("stringArgs", function (): void { - const s = parse([" ", " "], { string: "_" })._; - assertEquals(s.length, 2); - assertEquals(typeof s[0], "string"); - assertEquals(s[0], " "); - assertEquals(typeof s[1], "string"); - assertEquals(s[1], " "); -}); - -Deno.test("emptyStrings", function (): void { - const s = parse(["-s"], { string: "s" }).s; - assertEquals(s, ""); - assertEquals(typeof s, "string"); - - const str = parse(["--str"], { string: "str" }).str; - assertEquals(str, ""); - assertEquals(typeof str, "string"); - - const letters = parse(["-art"], { - string: ["a", "t"], - }); - - assertEquals(letters.a, ""); - assertEquals(letters.r, true); - assertEquals(letters.t, ""); -}); - -Deno.test("stringAndAlias", function (): void { - const x = parse(["--str", "000123"], { - string: "s", - alias: { s: "str" }, - }); - - assertEquals(x.str, "000123"); - assertEquals(typeof x.str, "string"); - assertEquals(x.s, "000123"); - assertEquals(typeof x.s, "string"); - - const y = parse(["-s", "000123"], { - string: "str", - alias: { str: "s" }, - }); - - assertEquals(y.str, "000123"); - assertEquals(typeof y.str, "string"); - assertEquals(y.s, "000123"); - assertEquals(typeof y.s, "string"); -}); - -Deno.test("slashBreak", function (): void { - assertEquals(parse(["-I/foo/bar/baz"]), { I: "/foo/bar/baz", _: [] }); - assertEquals(parse(["-xyz/foo/bar/baz"]), { - x: true, - y: true, - z: "/foo/bar/baz", - _: [], - }); -}); - -Deno.test("alias", function (): void { - const argv = parse(["-f", "11", "--zoom", "55"], { - alias: { z: "zoom" }, - }); - assertEquals(argv.zoom, 55); - assertEquals(argv.z, argv.zoom); - assertEquals(argv.f, 11); -}); - -Deno.test("multiAlias", function (): void { - const argv = parse(["-f", "11", "--zoom", "55"], { - alias: { z: ["zm", "zoom"] }, - }); - assertEquals(argv.zoom, 55); - assertEquals(argv.z, argv.zoom); - assertEquals(argv.z, argv.zm); - assertEquals(argv.f, 11); -}); - -Deno.test("nestedDottedObjects", function (): void { - const argv = parse([ - "--foo.bar", - "3", - "--foo.baz", - "4", - "--foo.quux.quibble", - "5", - "--foo.quux.oO", - "--beep.boop", - ]); - - assertEquals(argv.foo, { - bar: 3, - baz: 4, - quux: { - quibble: 5, - oO: true, - }, - }); - assertEquals(argv.beep, { boop: true }); -}); - -Deno.test("flagBuiltinProperty", function (): void { - const argv = parse(["--toString", "--valueOf", "foo"]); - assertEquals(argv, { toString: true, valueOf: "foo", _: [] }); - assertEquals(typeof argv.toString, "boolean"); - assertEquals(typeof argv.valueOf, "string"); -}); diff --git a/std/flags/short_test.ts b/std/flags/short_test.ts deleted file mode 100755 index 9f7936f98..000000000 --- a/std/flags/short_test.ts +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { assertEquals } from "../testing/asserts.ts"; -import { parse } from "./mod.ts"; - -Deno.test("numbericShortArgs", function (): void { - assertEquals(parse(["-n123"]), { n: 123, _: [] }); - assertEquals(parse(["-123", "456"]), { 1: true, 2: true, 3: 456, _: [] }); -}); - -Deno.test("short", function (): void { - assertEquals(parse(["-b"]), { b: true, _: [] }); - assertEquals(parse(["foo", "bar", "baz"]), { _: ["foo", "bar", "baz"] }); - assertEquals(parse(["-cats"]), { c: true, a: true, t: true, s: true, _: [] }); - assertEquals(parse(["-cats", "meow"]), { - c: true, - a: true, - t: true, - s: "meow", - _: [], - }); - assertEquals(parse(["-h", "localhost"]), { h: "localhost", _: [] }); - assertEquals(parse(["-h", "localhost", "-p", "555"]), { - h: "localhost", - p: 555, - _: [], - }); -}); - -Deno.test("mixedShortBoolAndCapture", function (): void { - assertEquals(parse(["-h", "localhost", "-fp", "555", "script.js"]), { - f: true, - p: 555, - h: "localhost", - _: ["script.js"], - }); -}); - -Deno.test("shortAndLong", function (): void { - assertEquals(parse(["-h", "localhost", "-fp", "555", "script.js"]), { - f: true, - p: 555, - h: "localhost", - _: ["script.js"], - }); -}); diff --git a/std/flags/stop_early_test.ts b/std/flags/stop_early_test.ts deleted file mode 100755 index f7e23881a..000000000 --- a/std/flags/stop_early_test.ts +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { assertEquals } from "../testing/asserts.ts"; -import { parse } from "./mod.ts"; - -// stops parsing on the first non-option when stopEarly is set -Deno.test("stopParsing", function (): void { - const argv = parse(["--aaa", "bbb", "ccc", "--ddd"], { - stopEarly: true, - }); - - assertEquals(argv, { - aaa: "bbb", - _: ["ccc", "--ddd"], - }); -}); diff --git a/std/flags/test.ts b/std/flags/test.ts deleted file mode 100644 index 590417055..000000000 --- a/std/flags/test.ts +++ /dev/null @@ -1,2 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import "./mod.ts"; diff --git a/std/flags/unknown_test.ts b/std/flags/unknown_test.ts deleted file mode 100755 index 4f79215b6..000000000 --- a/std/flags/unknown_test.ts +++ /dev/null @@ -1,110 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { assertEquals } from "../testing/asserts.ts"; -import { parse } from "./mod.ts"; - -Deno.test("booleanAndAliasIsNotUnknown", function (): void { - const unknown: unknown[] = []; - function unknownFn(arg: string, k?: string, v?: unknown): boolean { - unknown.push({ arg, k, v }); - return false; - } - const aliased = ["-h", "true", "--derp", "true"]; - const regular = ["--herp", "true", "-d", "false"]; - const opts = { - alias: { h: "herp" }, - boolean: "h", - unknown: unknownFn, - }; - parse(aliased, opts); - parse(regular, opts); - - assertEquals(unknown, [ - { arg: "--derp", k: "derp", v: "true" }, - { arg: "-d", k: "d", v: "false" }, - ]); -}); - -Deno.test( - "flagBooleanTrueAnyDoubleHyphenArgumentIsNotUnknown", - function (): void { - const unknown: unknown[] = []; - function unknownFn(arg: string, k?: string, v?: unknown): boolean { - unknown.push({ arg, k, v }); - return false; - } - const argv = parse(["--honk", "--tacos=good", "cow", "-p", "55"], { - boolean: true, - unknown: unknownFn, - }); - assertEquals(unknown, [ - { arg: "--tacos=good", k: "tacos", v: "good" }, - { arg: "cow", k: undefined, v: undefined }, - { arg: "-p", k: "p", v: "55" }, - ]); - assertEquals(argv, { - honk: true, - _: [], - }); - }, -); - -Deno.test("stringAndAliasIsNotUnkown", function (): void { - const unknown: unknown[] = []; - function unknownFn(arg: string, k?: string, v?: unknown): boolean { - unknown.push({ arg, k, v }); - return false; - } - const aliased = ["-h", "hello", "--derp", "goodbye"]; - const regular = ["--herp", "hello", "-d", "moon"]; - const opts = { - alias: { h: "herp" }, - string: "h", - unknown: unknownFn, - }; - parse(aliased, opts); - parse(regular, opts); - - assertEquals(unknown, [ - { arg: "--derp", k: "derp", v: "goodbye" }, - { arg: "-d", k: "d", v: "moon" }, - ]); -}); - -Deno.test("defaultAndAliasIsNotUnknown", function (): void { - const unknown: unknown[] = []; - function unknownFn(arg: string, k?: string, v?: unknown): boolean { - unknown.push({ arg, k, v }); - return false; - } - const aliased = ["-h", "hello"]; - const regular = ["--herp", "hello"]; - const opts = { - default: { h: "bar" }, - alias: { h: "herp" }, - unknown: unknownFn, - }; - parse(aliased, opts); - parse(regular, opts); - - assertEquals(unknown, []); -}); - -Deno.test("valueFollowingDoubleHyphenIsNotUnknown", function (): void { - const unknown: unknown[] = []; - function unknownFn(arg: string, k?: string, v?: unknown): boolean { - unknown.push({ arg, k, v }); - return false; - } - const aliased = ["--bad", "--", "good", "arg"]; - const opts = { - "--": true, - unknown: unknownFn, - }; - const argv = parse(aliased, opts); - - assertEquals(unknown, [{ arg: "--bad", k: "bad", v: true }]); - assertEquals(argv, { - "--": ["good", "arg"], - _: [], - }); -}); diff --git a/std/flags/whitespace_test.ts b/std/flags/whitespace_test.ts deleted file mode 100755 index bb9aab1f4..000000000 --- a/std/flags/whitespace_test.ts +++ /dev/null @@ -1,7 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { assertEquals } from "../testing/asserts.ts"; -import { parse } from "./mod.ts"; - -Deno.test("whitespaceShouldBeWhitespace", function (): void { - assertEquals(parse(["-x", "\t"]).x, "\t"); -}); |