UNPKG

3.94 kBTypeScriptView Raw
1// Type definitions for optimist
2// Project: https://github.com/substack/node-optimist
3// Definitions by: Carlos Ballesteros Velasco <https://github.com/soywiz>, Christopher Brown <https://github.com/chbrown>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5
6
7
8declare namespace optimist {
9 interface Opt {
10 alias?: string | string[];
11 default?: any;
12 demand?: string | number | string[];
13 describe?: string;
14 type?: string;
15 }
16
17 interface Parser {
18 /** Implicitly use process.argv array to construct the argv object */
19 argv: any;
20 /** Pass in the process.argv yourself */
21 (args: string[]): any;
22 /** Use .parse() to do the same thing as treating optimist as a function */
23 parse(args: string[]): any;
24
25 // The types below follow the order and documentation of https://github.com/substack/node-optimist
26
27 /** Set key names as equivalent such that updates to a key will propagate to aliases and vice-versa. */
28 alias(key: string, alias: string | string[]): Parser;
29 /** Take an object that maps keys to aliases. */
30 alias(aliases: { [index: string]: string | string[] }): Parser;
31
32 /** Set argv[key] to value if no option was specified on process.argv */
33 default(key: string, value: any): Parser;
34 /** Take an object that maps keys to default values */
35 default(defaults: { [index: string]: any }): Parser;
36
37 /** Show the usage information and exit if key wasn't specified in process.argv */
38 demand(key: string): Parser;
39 /** Demand at least as many non-option arguments, which show up in argv._ */
40 demand(key: number): Parser;
41 /** Demand each element in key */
42 demand(key: string[]): Parser;
43
44 /** Describe a key for the generated usage information */
45 describe(key: string, desc: string): Parser;
46 /** Take an object that maps keys to descriptions */
47 describe(descriptions: { [index: string]: string }): Parser;
48
49 /** Instead of chaining together, e.g. optimist.alias().demand().default()...,
50 you can specify keys in opt for each of the chainable methods. */
51 options(key: string, opt: Opt): Parser;
52 /** Take an object that maps keys to opt parameters */
53 options(options: { [index: string]: Opt }): Parser;
54
55 /** Set a usage message to show which commands to use. Inside message,
56 the string $0 will get interpolated to the current script name or node
57 command for the present script similar to how $0 works in bash or perl. */
58 usage(message: string): Parser;
59
60 /** Check that certain conditions are met in the provided arguments. If fn
61 throws or returns false, show the thrown error, usage information, and exit.
62 */
63 check(fn: (argv: any) => any): Parser;
64
65 /** Interpret key as a boolean. If a non-flag option follows key in process.argv,
66 that string won't get set as the value of key. If key never shows up as a
67 flag in process.arguments, argv[key] will be false. */
68 boolean(key: string): Parser;
69 /** Interpret all the elements as booleans. */
70 boolean(key: string[]): Parser;
71
72 /** Tell the parser logic not to interpret key as a number or boolean. This can be useful if you need to preserve leading zeros in an input. */
73 string(key: string): Parser;
74 /** Interpret all the elements as strings */
75 string(key: string[]): Parser;
76
77 /** Format usage output to wrap at columns many columns. */
78 wrap(columns: number): Parser;
79
80 /** Return the generated usage string. */
81 help(): string;
82 /** Print the usage data using fn for printing (defaults to console.error). */
83 showHelp(fn?: (message: string) => void): void;
84 }
85}
86
87declare var optimist: optimist.Parser;
88export = optimist;