tinyargs
Version:
A tiny and flexible command-line argument parser for Node.js and Deno.
91 lines (89 loc) • 2.67 kB
TypeScript
interface Option<TMeta> {
/**
* The flag or positional argument's name
*
* For example, `name: 'file'` will capture the value of flag `--file`
*/
name: string;
/**
* By default we only capture flags that match the option `name`
* But you can use `flags` to capture flags that match any of the specified values
*
*/
flags?: string[];
/**
* Option type
*
* We use the function to coerce the value
*/
type: OptionSimpleType;
/**
* Allow multiple flags or positional arguments
*
* For flags, this means that the flag can be repeated.
* @example
* ```
* $ node cli.js --foo --foo --foo
* {foo: [true, true, true]}
* $ node cli.js --foo 1 --foo 2 --foo 3
* {foo: ['1', '2', '3']}
* ```
*
* For positional arguments, you can provide multiple values.
* It only captures non-flag value (not starting with a dash `-`) by default
* but you can set it to `include-flags` to capture all values
* @example
* ```
* $ node cli.js a b c
* {some_optiona: ['a', 'b', 'c']}
* ```
*/
multiple?: boolean | "include-flags";
/**
* Mark this optional as a positional argument
*/
positional?: boolean;
/**
* Stop parsing remaining arguments after current option
*
* The remaining arguments will be added to the `_` key
*/
stop?: boolean;
/**
* Allow optional value
*
* For a non-boolean flag, this means that the flag value can be omitted
* @example
* ```bash
* node cli.js --config
* # `config` will be `true` instead of throwing an error
* ```
*
* For a positional argument, this means it could be `undefined` in the returned object
*/
optionalValue?: boolean;
/**
* Only add this option if this function returns true
*
* Already parsed options will be passed to the function
*/
when?: (parsed: Parsed) => boolean;
/**
* Use this to store information about the option
* Useful for building help message
*/
meta?: TMeta;
}
declare type OptionSimpleType = StringConstructor | NumberConstructor | BooleanConstructor | ((value: string) => any);
declare type Parsed = {
_: string[];
[key: string]: any;
};
/**
* Parse command line arguments with give option config
*
* Not that if you're using `process.argv`, you should always omit the first two elements,
* i.e. pass `process.argv.slice(2)` to this function
*/
declare const parse: <TMeta>(args: string[], options: Option<TMeta>[]) => Parsed;
export { Option, Parsed, parse };