Function parseArgs
- parse
Args <TArgs, TDoubleDash, TBooleans, TStrings, TCollectable, TNegatable, TDefaults, TAliases, TAliasArgNames, TAliasNames>(args, options?): Args<TArgs, TDoubleDash> Type Parameters
- TArgs extends Record<string, unknown> & AddAliases<SpreadDefaults<UnionToIntersection<Extract<TStrings, TCollectable> extends string
? (Exclude<TStrings, TCollectable> extends never
? Record<never, never>
: MapTypes<Exclude<(...), (...)>, string, TNegatable>) & ((string & Extract<(...), (...)>) extends never
? Record<never, never>
: RecursiveRequired<MapTypes<(...), (...), (...)>>)
: MapTypes<TStrings, string, TNegatable>> & RecursiveRequired<UnionToIntersection<Extract<TBooleans, TCollectable> extends string
? (Exclude<(...), (...)> extends never
? Record<(...), (...)>
: MapTypes<(...), (...), (...)>) & (((...) & (...)) extends never
? Record<(...), (...)>
: RecursiveRequired<(...)>)
: MapTypes<TBooleans, boolean, undefined>>> & UnionToIntersection<TCollectable extends TBooleans & TStrings
? Record<never, never>
: DedotRecord<Record<Exclude<(...), (...)>, (...)[]> & Record<Exclude<(...), (...)>, (...) | (...)>>>, DedotRecord<TDefaults>>, TAliases> | Record<string, any>Type of result.
- TDoubleDash extends undefined | boolean = undefined
Used by
TArgsfor the result. - TBooleans extends BooleanType = undefined
Used by
TArgsfor the result. - TStrings extends StringType = undefined
Used by
TArgsfor the result. - TCollectable extends Collectable = undefined
Used by
TArgsfor the result. - TNegatable extends Negatable = undefined
Used by
TArgsfor the result. - TDefaults extends undefined | Record<string, unknown> = undefined
Used by
TArgsfor the result. - TAliases extends undefined | Partial<Record<Extract<TAliasArgNames, string>, TAliasNames | readonly TAliasNames[]>> = undefined
Used by
TArgsfor the result. - TAliasArgNames extends string = string
Used by
TArgsfor the result. - TAliasNames extends string = string
Used by
TArgsfor the result.
Parameters
- args: string[]
An array of command line arguments.
Optionaloptions: ParseOptions<TBooleans, TStrings, TCollectable, TNegatable, TDefaults, TAliases, TDoubleDash>Options for the parse function.
Returns Args<TArgs, TDoubleDash>
The parsed arguments.
Example: Usage
import { parseArgs } from "@std/cli/parse-args";
import { assertEquals } from "@std/assert/equals";
// For proper use, one should use `parseArgs(Deno.args)`
assertEquals(parseArgs(["--foo", "--bar=baz", "./quux.txt"]), {
foo: true,
bar: "baz",
_: ["./quux.txt"],
});Example: `collect` option
collectoption tells the parser to treat the option as an array. All values will be collected into one array. If a non-collectable option is used multiple times, the last value is used.import { parseArgs } from "@std/cli/parse-args";
import { assertEquals } from "@std/assert/equals";
const args = parseArgs(["--foo", "bar", "--foo", "baz"], {
collect: ["foo"],
});
assertEquals(args, { foo: ["bar", "baz"], _: [] });Example: `negatable` option
negatableoption tells the parser to treat the option can be negated by prefixing them with--no-, like--no-config.import { parseArgs } from "@std/cli/parse-args";
import { assertEquals } from "@std/assert/equals";
const args = parseArgs(["--no-foo"], {
boolean: ["foo"],
negatable: ["foo"],
});
assertEquals(args, { foo: false, _: [] });- TArgs extends Record<string, unknown> & AddAliases<SpreadDefaults<UnionToIntersection<Extract<TStrings, TCollectable> extends string
Take a set of command line arguments, optionally with a set of options, and return an object representing the flags found in the passed arguments.
By default, any arguments starting with
-or--are considered boolean flags. If the argument name is followed by an equal sign (=) it is considered a key-value pair. Any arguments which could not be parsed are available in the_property of the returned object.By default, this module tries to determine the type of all arguments automatically and the return type of this function will have an index signature with
anyas value ({ [x: string]: any }).If the
string,booleanorcollectoption is set, the return value of this function will be fully typed and the index signature of the return type will change to{ [x: string]: unknown }.Any arguments after
'--'will not be parsed and will end up inparsedArgs._.Numeric-looking arguments will be returned as numbers unless
options.stringoroptions.booleanis set for that argument name.See
ParseOptionsfor more information.