/** * Parse the given string / array of strings as an arguments list into a structure of arguments and options. The * function parses the data in the following way: * - If it's a string without leading dashes and the previous argument doesn't start with a dash, then it's considered * as an argument (i.e. "arg1", "arg2" is `["arg1", "arg2"]`) * - If it's a string with a single leading dash, then it's considered as a flag array (i.e. "-abc" is `{a: true, * b: true, c: true}`). * - If it's a single char with a single leading dash, then it's considered as a flag. If the next argument doesn't * start with a dash, then the next argument is a value of the flag (i.e. "-a" is a `{a: true}`, "-a b" and "-a=b" is * `{a: "b"}`) * - If it's a string with a double leading dash, then it's considered as a single option with optional value that * goes after the option (i.e. "--user" is `{user: true}`, "--user name" is `{user: "name"}`, "--user=name" is * `{user: "name"}`) * - If it's a "--" string, then all next strings are considered as arguments * @param args Arguments to parse. * @returns Parsed arguments. * @example * Parsing a string * ```ts * const data = parse("abc -d val1 -e=val2 -fg def --opt1 --opt2 val2 --opt3=val3 --opt4 \"a b c\" -- --opt5"); * data == { * args: ["abc", "def", "--opt5"], * opts: {d: "val1", e: "val2", f: true, g: true, opt1: true, opt2: "val2", opt3: "val3", opt4: "a b c"} * } * ``` * @example * Parsing an array * ```ts * const data = parse(["abc", "-d", "val1", "-e=val2", "-fg", "def", "--opt1", "--opt2", "val2", "--opt3=val3", "--opt4=\"a b c\"", "--", "--opt5"]); * data == { * args: ["abc", "def", "--opt5"], * opts: {d: "val1", e: "val2", f: true, g: true, opt1: true, opt2: "val2", opt3: "val3", opt4: "a b c"} * } * ``` * @example * Using generic type inference * ```ts * const data = parse<"opt1" | "opt2">("..."); * data.opts; // It could have "opt1" and "opt2" properties * ``` */ export declare function parse(args: string | string[], options?: Partial): ArgsInfo; type Options = { /** * Consider options that start with "--no-" prefix as flags with `false` value. * @defaultValue `true`. * @example * ```ts * parse("--no-data", {no: false}).opts == {"no-data": true} * parse("--no-data", {no: true}).opts == {data: false} * ``` */ no: boolean; /** * Consider arguments that start with single dash as an array of flags. Otherwise those arguments are treated the * same way as the arguments with double dash. * @defaultValue `true`. * @example * ```ts * parse("-key", {flagArray: false}).opts == {key: true} * parse("-key", {flagArray: true}).opts == {k: true, e: true, y: true} * ``` */ flagArray: boolean; /** * Allow options to occure more than once. In that case, instead of overriding, an array of values is created. * @defaultValue `false`. * @example * ```ts * parse("-a 1 -a 2", {multiple: false}).opts == {a: "2"} * parse("-a 1 -a 2", {multiple: true}).opts == {a: ["1", "2"]} * ``` */ multiple: boolean; }; type ArgsInfo = { /** * An array of positional arguments. */ args: string[]; /** * A map of options. */ opts: { [K in T]?: boolean | string | string[]; }; }; export {};