Skip to content

Commit 7ab10dc

Browse files
committed
Merge pull request DefinitelyTyped#8635 from iplabs/cli
Add type definitions for https://www.npmjs.com/package/cli
2 parents bf77095 + 7c1a4fd commit 7ab10dc

File tree

2 files changed

+247
-0
lines changed

2 files changed

+247
-0
lines changed

cli/cli-tests.ts

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/// <reference path="./cli.d.ts" />
2+
3+
import * as cli from "cli";
4+
5+
// ========================================================================
6+
// Example: https://github.com/node-js-libs/cli/blob/master/examples/cat.js
7+
// ========================================================================
8+
9+
var output_file = function(file: string) {
10+
cli.withInput(file, function (line, sep, eof) {
11+
if (!eof) {
12+
cli.output(line + sep);
13+
} else if (cli.args.length) {
14+
output_file(cli.args.shift());
15+
}
16+
});
17+
};
18+
19+
if (cli.args.length) {
20+
output_file(cli.args.shift());
21+
}
22+
23+
// ============================================================================
24+
// Example: https://github.com/node-js-libs/cli/blob/master/examples/command.js
25+
// ============================================================================
26+
27+
cli.parse(null, ['install', 'test', 'edit', 'remove', 'uninstall', 'ls']);
28+
29+
console.log('Command is: ' + cli.command);
30+
31+
32+
// ============================================================================
33+
// Example: https://github.com/node-js-libs/cli/blob/master/examples/echo.js
34+
// ============================================================================
35+
36+
cli.parse({
37+
newline: ['n', 'Do not output the trailing newline'],
38+
escape: ['e', 'Enable interpretation of backslash escapes'],
39+
separator: ['s', 'Separate arguments using this value', 'string', ' '],
40+
output: [false, 'Write to FILE rather than the console', 'file']
41+
});
42+
43+
cli.main(function (args, options) {
44+
var output = '', i: any, j: any, l: number, output_stream: NodeJS.WritableStream;
45+
46+
if (this.argc) {
47+
if (options.escape) {
48+
var replace: any = {'\\n':'\n','\\r':'\r','\\t':'\t','\\e':'\e','\\v':'\v','\\f':'\f','\\c':'\c','\\b':'\b','\\a':'\a','\\\\':'\\'};
49+
var escape = function (str: string) {
50+
str += '';
51+
for (j in replace) {
52+
str = str.replace(i, replace[i]);
53+
}
54+
return str;
55+
}
56+
for (i = 0, l = this.argc; i < l; i++) {
57+
args[i] = escape(args[i]);
58+
}
59+
options.separator = escape(options.separator);
60+
}
61+
output += args.join(options.separator);
62+
}
63+
64+
if (!options.newline) {
65+
output += '\n';
66+
}
67+
68+
try {
69+
if (options.output) {
70+
output_stream = this.native.fs.createWriteStream(options.output)
71+
} else {
72+
output_stream = process.stdout;
73+
}
74+
output_stream.write(output);
75+
} catch (e) {
76+
this.fatal('Could not write to output stream');
77+
}
78+
});
79+
80+
81+
// =========================================================================
82+
// Example: https://github.com/node-js-libs/cli/blob/master/examples/glob.js
83+
// =========================================================================
84+
85+
cli.enable('glob');
86+
87+
//Running `./glob.js *.js` will output a list of .js files in this directory
88+
console.log(cli.args);
89+
90+
91+
// ==============================================================================
92+
// Example: https://github.com/node-js-libs/cli/blob/master/examples/long_desc.js
93+
// ==============================================================================
94+
95+
//You can (optionally) boost the width of output with:
96+
cli.width = 120;
97+
98+
//You can also adjust the width of the options/command definitions
99+
cli.option_width = 25;
100+
101+
var long_desc = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s '
102+
+ 'standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make'
103+
+ ' a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, '
104+
+ 'remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing '
105+
+ 'Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions'
106+
+ ' of Lorem Ipsum.';
107+
108+
cli.parse({
109+
foo: ['f', long_desc]
110+
});
111+
112+
113+
// =============================================================================
114+
// Example: https://github.com/node-js-libs/cli/blob/master/examples/progress.js
115+
// =============================================================================
116+
117+
var i = 0, interval = setInterval(function () {
118+
cli.progress(++i / 100);
119+
if (i === 100) {
120+
clearInterval(interval);
121+
cli.ok('Finished!');
122+
}
123+
}, 50);
124+
125+
126+
// =========================================================================
127+
// Example: https://github.com/node-js-libs/cli/blob/master/examples/sort.js
128+
// =========================================================================
129+
130+
var options = cli.parse({
131+
numeric: ['n', 'Compare using a numeric sort'],
132+
reverse: ['r', 'Reverse the results']
133+
});
134+
135+
cli.withStdinLines(function (lines, newline) {
136+
lines.sort(!options.numeric ? null : function (a, b) {
137+
return parseInt(a) - parseInt(b);
138+
});
139+
if (options.reverse) {
140+
lines.reverse();
141+
}
142+
this.output(lines.join(newline));
143+
});
144+
145+
146+
// ============================================================================
147+
// Example: https://github.com/node-js-libs/cli/blob/master/examples/spinner.js
148+
// ============================================================================
149+
150+
cli.spinner('Working..');
151+
152+
setTimeout(function () {
153+
cli.spinner('Working.. done!', true); //End the spinner
154+
}, 3000);
155+
156+
157+
// ===========================================================================
158+
// Example: https://github.com/node-js-libs/cli/blob/master/examples/static.js
159+
// ===========================================================================
160+
161+
cli.parse({
162+
log: ['l', 'Enable logging'],
163+
port: ['p', 'Listen on this port', 'number', 8080],
164+
serve: [false, 'Serve static files from PATH', 'path', './public']
165+
});
166+
167+
cli.main(function (args, options) {
168+
var server: any, middleware: any = [];
169+
170+
if (options.log) {
171+
this.debug('Enabling logging');
172+
middleware.push(require('creationix/log')());
173+
}
174+
175+
this.debug('Serving files from ' + options.serve);
176+
middleware.push(require('creationix/static')('/', options.serve, 'index.html'));
177+
178+
server = this.createServer(middleware).listen(options.port);
179+
180+
this.ok('Listening on port ' + options.port);
181+
});

cli/cli.d.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Type definitions for cli v0.11.2
2+
// Project: https://www.npmjs.com/package/cli
3+
// Definitions by: Klaus Reimer <https://github.com/kayahr>
4+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5+
6+
/// <reference path="../node/node.d.ts" />
7+
8+
declare module "cli" {
9+
interface CLI {
10+
app: string;
11+
version: string;
12+
argv: string[];
13+
argc: number;
14+
options: any;
15+
args: string[];
16+
command: string;
17+
width: number;
18+
option_width: number;
19+
native: any;
20+
output(message?: any, ...optionalParams: any[]): void;
21+
exit(code: number): void;
22+
no_color: boolean;
23+
enable(...plugins: string[]): CLI;
24+
disable(...plugins: string[]): CLI;
25+
setArgv(argv: string | Array<any>, keepArg0?: boolean): void;
26+
next(): string;
27+
parse(opts?: { [long: string]: { 0: string | boolean, 1: string, 2?: string, 3?: any } },
28+
commands?: { [name: string]: string } | string[]): any;
29+
autocompleteCommand(command: string): string;
30+
info(msg: string): void;
31+
error(msg: string): void;
32+
ok(msg: string): void;
33+
debug(msg: string): void;
34+
fatal(msg: string): void;
35+
setApp(appName: string, version: string): CLI;
36+
setApp(packageJson: string): CLI;
37+
parsePackageJson(path?: string): void;
38+
setUsage(usage: string): CLI;
39+
getUsage(code?: number): void;
40+
getOptError(expects: string, type: string): string;
41+
getValue(defaultVal: string, validateFunc: (value: any) => any, errMsg: string): void;
42+
getInt(defaultVal: number): number;
43+
getDate(defaultVal: Date): Date;
44+
getFloat(defaultVal: number): number;
45+
getUrl(defautltVal: string, identifier?: string): string;
46+
getEmail(defaultVal: string): string;
47+
getIp(defaultVal: string): string;
48+
getPath(defaultVal: string, identifier?: string): string;
49+
getArrayValue<T>(arr: T[], defaultVal: T): T;
50+
withStdin(callback: (data: string) => void): void;
51+
withStdin(encoding: string, callback: (text: string) => void): void;
52+
withStdinLines(callback: (lines: string[], newline: string) => void): void;
53+
withInput(file: string, encoding: string, callback: (line: string, newline: string, eof: boolean) => void): void;
54+
withInput(file: string, callback: (line: string, newline: string, eof: boolean) => void): void;
55+
withInput(callback: (line: string, newline: string, eof: boolean) => void): void;
56+
toType(object: any): string;
57+
daemon(arg: string, callback: () => void): void;
58+
main(callback: (args: string[], options: any) => void): void;
59+
createServer(...args: any[]): any;
60+
exec(cmd: string, callback?: (lines: string[]) => void, errback?: (err: any, stdout: string) => void): void;
61+
progress(progress: number, decimals?: number, stream?: NodeJS.WritableStream): void;
62+
spinner(prefix?: string | boolean, end?: boolean, stream?: NodeJS.WritableStream): void;
63+
}
64+
const cli: CLI;
65+
export = cli;
66+
}

0 commit comments

Comments
 (0)