|
| 1 | +/** |
| 2 | + * An object whose all properties have the same type, where each key is a string. |
| 3 | + */ |
| 4 | +interface Dictionary<T = any> { |
| 5 | + [key: string]: T; |
| 6 | +} |
| 7 | +type ArgsInput = string | any[]; |
| 8 | +type ArgsOutput = (string | number)[]; |
| 9 | +interface Arguments { |
| 10 | + /** Non-option arguments */ |
| 11 | + _: ArgsOutput; |
| 12 | + /** Arguments after the end-of-options flag `--` */ |
| 13 | + "--"?: ArgsOutput; |
| 14 | + /** All remaining options */ |
| 15 | + [argName: string]: any; |
| 16 | +} |
| 17 | +interface DetailedArguments { |
| 18 | + /** An object representing the parsed value of `args` */ |
| 19 | + argv: Arguments; |
| 20 | + /** Populated with an error object if an exception occurred during parsing. */ |
| 21 | + error: Error | null; |
| 22 | + /** The inferred list of aliases built by combining lists in opts.alias. */ |
| 23 | + aliases: Dictionary<string[]>; |
| 24 | + /** Any new aliases added via camel-case expansion. */ |
| 25 | + newAliases: Dictionary<boolean>; |
| 26 | + /** Any new argument created by opts.default, no aliases included. */ |
| 27 | + defaulted: Dictionary<boolean>; |
| 28 | + /** The configuration loaded from the yargs stanza in package.json. */ |
| 29 | + configuration: Configuration; |
| 30 | +} |
| 31 | +interface Configuration { |
| 32 | + /** Should variables prefixed with --no be treated as negations? Default is `true` */ |
| 33 | + "boolean-negation": boolean; |
| 34 | + /** Should hyphenated arguments be expanded into camel-case aliases? Default is `true` */ |
| 35 | + "camel-case-expansion": boolean; |
| 36 | + /** Should arrays be combined when provided by both command line arguments and a configuration file? Default is `false` */ |
| 37 | + "combine-arrays": boolean; |
| 38 | + /** Should keys that contain `.` be treated as objects? Default is `true` */ |
| 39 | + "dot-notation": boolean; |
| 40 | + /** Should arguments be coerced into an array when duplicated? Default is `true` */ |
| 41 | + "duplicate-arguments-array": boolean; |
| 42 | + /** Should array arguments be coerced into a single array when duplicated? Default is `true` */ |
| 43 | + "flatten-duplicate-arrays": boolean; |
| 44 | + /** Should arrays consume more than one positional argument following their flag? Default is `true` */ |
| 45 | + "greedy-arrays": boolean; |
| 46 | + /** Should parsing stop at the first text argument? This is similar to how e.g. ssh parses its command line. Default is `false` */ |
| 47 | + "halt-at-non-option": boolean; |
| 48 | + /** Should nargs consume dash options as well as positional arguments? Default is `false` */ |
| 49 | + "nargs-eats-options": boolean; |
| 50 | + /** The prefix to use for negated boolean variables. Default is `'no-'` */ |
| 51 | + "negation-prefix": string; |
| 52 | + /** Should positional values that look like numbers be parsed? Default is `true` */ |
| 53 | + "parse-positional-numbers": boolean; |
| 54 | + /** Should keys that look like numbers be treated as such? Default is `true` */ |
| 55 | + "parse-numbers": boolean; |
| 56 | + /** Should unparsed flags be stored in -- or _? Default is `false` */ |
| 57 | + "populate--": boolean; |
| 58 | + /** Should a placeholder be added for keys not set via the corresponding CLI argument? Default is `false` */ |
| 59 | + "set-placeholder-key": boolean; |
| 60 | + /** Should a group of short-options be treated as boolean flags? Default is `true` */ |
| 61 | + "short-option-groups": boolean; |
| 62 | + /** Should aliases be removed before returning results? Default is `false` */ |
| 63 | + "strip-aliased": boolean; |
| 64 | + /** Should dashed keys be removed before returning results? This option has no effect if camel-case-expansion is disabled. Default is `false` */ |
| 65 | + "strip-dashed": boolean; |
| 66 | + /** Should unknown options be treated like regular arguments? An unknown option is one that is not configured in opts. Default is `false` */ |
| 67 | + "unknown-options-as-args": boolean; |
| 68 | +} |
| 69 | +type ArrayOption = string | { |
| 70 | + key: string; |
| 71 | + boolean?: boolean; |
| 72 | + string?: boolean; |
| 73 | + number?: boolean; |
| 74 | + integer?: boolean; |
| 75 | +}; |
| 76 | +type CoerceCallback = (arg: any) => any; |
| 77 | +type ConfigCallback = (configPath: string) => { |
| 78 | + [key: string]: any; |
| 79 | +} | Error; |
| 80 | +interface Options { |
| 81 | + /** An object representing the set of aliases for a key: `{ alias: { foo: ['f']} }`. */ |
| 82 | + alias: Dictionary<string | string[]>; |
| 83 | + /** |
| 84 | + * Indicate that keys should be parsed as an array: `{ array: ['foo', 'bar'] }`. |
| 85 | + * Indicate that keys should be parsed as an array and coerced to booleans / numbers: |
| 86 | + * { array: [ { key: 'foo', boolean: true }, {key: 'bar', number: true} ] }`. |
| 87 | + */ |
| 88 | + array: ArrayOption | ArrayOption[]; |
| 89 | + /** Arguments should be parsed as booleans: `{ boolean: ['x', 'y'] }`. */ |
| 90 | + boolean: string | string[]; |
| 91 | + /** Indicate a key that represents a path to a configuration file (this file will be loaded and parsed). */ |
| 92 | + config: string | string[] | Dictionary<boolean | ConfigCallback>; |
| 93 | + /** configuration objects to parse, their properties will be set as arguments */ |
| 94 | + configObjects: Dictionary<any>[]; |
| 95 | + /** Provide configuration options to the yargs-parser. */ |
| 96 | + configuration: Partial<Configuration>; |
| 97 | + /** |
| 98 | + * Provide a custom synchronous function that returns a coerced value from the argument provided (or throws an error), e.g. |
| 99 | + * `{ coerce: { foo: function (arg) { return modifiedArg } } }`. |
| 100 | + */ |
| 101 | + coerce: Dictionary<CoerceCallback>; |
| 102 | + /** Indicate a key that should be used as a counter, e.g., `-vvv = {v: 3}`. */ |
| 103 | + count: string | string[]; |
| 104 | + /** Provide default values for keys: `{ default: { x: 33, y: 'hello world!' } }`. */ |
| 105 | + default: Dictionary<any>; |
| 106 | + /** Environment variables (`process.env`) with the prefix provided should be parsed. */ |
| 107 | + envPrefix?: string; |
| 108 | + /** Specify that a key requires n arguments: `{ narg: {x: 2} }`. */ |
| 109 | + narg: Dictionary<number>; |
| 110 | + /** `path.normalize()` will be applied to values set to this key. */ |
| 111 | + normalize: string | string[]; |
| 112 | + /** Keys should be treated as strings (even if they resemble a number `-x 33`). */ |
| 113 | + string: string | string[]; |
| 114 | + /** Keys should be treated as numbers. */ |
| 115 | + number: string | string[]; |
| 116 | + /** i18n handler, defaults to util.format */ |
| 117 | + __: (format: any, ...param: any[]) => string; |
| 118 | + /** alias lookup table defaults */ |
| 119 | + key: Dictionary<any>; |
| 120 | +} |
| 121 | +interface Parser { |
| 122 | + (args: ArgsInput, opts?: Partial<Options>): Arguments; |
| 123 | + detailed(args: ArgsInput, opts?: Partial<Options>): DetailedArguments; |
| 124 | + camelCase(str: string): string; |
| 125 | + decamelize(str: string, joinString?: string): string; |
| 126 | + looksLikeNumber(x: null | undefined | number | string): boolean; |
| 127 | +} |
| 128 | +declare const yargsParser: Parser; |
| 129 | +export = yargsParser; |
0 commit comments