1
-
2
1
# [ shellcraft.js] ( https://www.npmjs.org/package/shellcraft )
3
2
4
3
[ ![ npm version] ( https://badge.fury.io/js/shellcraft.svg )] ( http://badge.fury.io/js/shellcraft )
8
7
9
8
Simple CLI and shell for Node.js based on [ commander] [ 1 ] and [ inquirer] [ 2 ] .
10
9
11
- This module provides a way in order to use * commander * and * inquirer * together.
10
+ This module provides a way in order to use _ commander _ and _ inquirer _ together.
12
11
There are a CLI and a shell mode, and the same commands can be used everywhere.
13
12
14
13
** shellcraft.js** supports the command history and the auto-completion (in shell
@@ -27,15 +26,15 @@ $ npm install shellcraft
27
26
### Hello, World
28
27
29
28
``` javascript
30
- var shellcraft = require (' shellcraft' );
29
+ var shellcraft = require (' shellcraft' );
31
30
32
31
var options = {
33
- version: ' 0.1.0'
32
+ version: ' 0.1.0' ,
34
33
};
35
34
36
- shellcraft .begin (options, function (err , results ) {
35
+ shellcraft .begin (options, function (err , results ) {
37
36
if (results) {
38
- console .log (results);
37
+ console .log (results);
39
38
}
40
39
});
41
40
```
@@ -45,6 +44,7 @@ shellcraft.begin (options, function (err, results) {
45
44
There are only two public API in order to play with shellcraft.
46
45
47
46
---
47
+
48
48
#### shellcraft.begin (options, callback)
49
49
50
50
✤ options
@@ -53,8 +53,8 @@ There are only two public API in order to play with shellcraft.
53
53
options = {
54
54
version: ' 0.1.0' ,
55
55
prompt: ' >' ,
56
- promptFixed: false
57
- }
56
+ promptFixed: false ,
57
+ };
58
58
```
59
59
60
60
The ` version ` is used by Commander with the ` -V, --version ` parameter.
@@ -78,17 +78,21 @@ mode. This behavior will change in the future.
78
78
##### Example
79
79
80
80
``` javascript
81
- shellcraft .begin ({
82
- version: ' 0.0.1' ,
83
- prompt: ' orc>'
84
- }, function (err , results ) {
85
- if (results) {
86
- console .log (results);
81
+ shellcraft .begin (
82
+ {
83
+ version: ' 0.0.1' ,
84
+ prompt: ' orc>' ,
85
+ },
86
+ function (err , results ) {
87
+ if (results) {
88
+ console .log (results);
89
+ }
87
90
}
88
- } );
91
+ );
89
92
```
90
93
91
94
shell mode
95
+
92
96
```
93
97
$ node myShell.js
94
98
orc> _
102
106
```
103
107
104
108
CLI mode
109
+
105
110
```
106
111
$ node myShell.js -h
107
112
117
122
```
118
123
119
124
---
125
+
120
126
#### shellcraft.registerExtension (shellExt, callback);
121
127
122
128
There are two builtin commands ` help ` and ` exit ` . For more commands you must
@@ -128,11 +134,11 @@ export two functions (`register()` and `unregister()`).
128
134
The path on the ` .js ` file where the ` register ` and ` unregister ` methods are
129
135
exported. An ` extension ` argument is passed with the ` register ` call. This
130
136
object has four methods, ` command ` , ` option ` , ` remove ` and ` reload ` . It looks
131
- like the * commander * API in some ways.
137
+ like the _ commander _ API in some ways.
132
138
133
139
``` javascript
134
140
extension
135
- .command (' foo' , ' foo description' , options, function (callback , args ) {
141
+ .command (' foo' , ' foo description' , options, function (callback , args ) {
136
142
/*
137
143
* callback (wizard, function (answers) {})
138
144
* Is called in order to return to the prompt (or exit if CLI). The wizard
@@ -145,7 +151,7 @@ extension
145
151
* Are the arguments provided with the command.
146
152
*/
147
153
})
148
- .option (' -f, --foo' , ' foo description' , options, function (callback , args ) {
154
+ .option (' -f, --foo' , ' foo description' , options, function (callback , args ) {
149
155
/*
150
156
* callback ()
151
157
*
@@ -155,13 +161,14 @@ extension
155
161
});
156
162
157
163
/* Remove a specific command or option */
158
- extension .remove (' foo' );
164
+ extension .remove (' foo' );
159
165
160
166
/* Reload for autocomplete (for example after removing a command) */
161
- extension .reload ();
167
+ extension .reload ();
162
168
```
163
169
164
170
The options can be (for ` command ` ):
171
+
165
172
``` javascript
166
173
options : {
167
174
wizard : false , /* when it needs Inquirer */
@@ -188,6 +195,7 @@ options : {
188
195
```
189
196
190
197
The options can be (for ` option ` ):
198
+
191
199
``` javascript
192
200
options : {
193
201
params : {
@@ -213,24 +221,26 @@ var cmd = {};
213
221
var opt = {};
214
222
215
223
cmd .hello = function (callback , args ) {
216
- console .log (zog + ' tells "Hello, ' + args .join (' ' ) + ' "' );
217
- callback ();
224
+ console .log (zog + ' tells "Hello, ' + args .join (' ' ) + ' "' );
225
+ callback ();
218
226
};
219
227
220
228
cmd .wizard = function (callback ) {
221
- var wizard = [{
222
- /* Inquirer definition... */
223
- type: ' input' ,
224
- name: ' zog' ,
225
- message: ' tell ' + zog
226
- }];
227
-
228
- callback (wizard, function (answers ) {
229
+ var wizard = [
230
+ {
231
+ /* Inquirer definition... */
232
+ type: ' input' ,
233
+ name: ' zog' ,
234
+ message: ' tell ' + zog,
235
+ },
236
+ ];
237
+
238
+ callback (wizard, function (answers ) {
229
239
/* stuff on answers */
230
240
if (answers .zog === zog) {
231
- console .log (zog + ' ' + zog);
241
+ console .log (zog + ' ' + zog);
232
242
} else {
233
- console .log (' lokthar?' );
243
+ console .log (' lokthar?' );
234
244
}
235
245
236
246
/*
@@ -249,34 +259,49 @@ opt.foobar = function (callback, args) {
249
259
} else {
250
260
zog = ' lokthar' ;
251
261
}
252
- callback ();
262
+ callback ();
253
263
};
254
264
255
265
exports .register = function (extension , callback ) {
256
266
extension
257
- .command (' hello' , ' print Hello, John' , {
258
- wizard: false ,
259
- params: {
260
- required: ' name' ,
261
- optional: ' etc...'
262
- }
263
- }, cmd .hello )
264
- .command (' wizard' , ' begins a wizard' , {
265
- wizard: true ,
266
- scope: ' warcraft'
267
- }, cmd .wizard )
268
- .option (' -f, --foobar' , ' zog is foobar' , {
269
- params: {
270
- required: ' who'
271
- }
272
- }, opt .foobar );
273
-
274
- callback ();
267
+ .command (
268
+ ' hello' ,
269
+ ' print Hello, John' ,
270
+ {
271
+ wizard: false ,
272
+ params: {
273
+ required: ' name' ,
274
+ optional: ' etc...' ,
275
+ },
276
+ },
277
+ cmd .hello
278
+ )
279
+ .command (
280
+ ' wizard' ,
281
+ ' begins a wizard' ,
282
+ {
283
+ wizard: true ,
284
+ scope: ' warcraft' ,
285
+ },
286
+ cmd .wizard
287
+ )
288
+ .option (
289
+ ' -f, --foobar' ,
290
+ ' zog is foobar' ,
291
+ {
292
+ params: {
293
+ required: ' who' ,
294
+ },
295
+ },
296
+ opt .foobar
297
+ );
298
+
299
+ callback ();
275
300
};
276
301
277
302
exports .unregister = function (callback ) {
278
303
/* internal stuff */
279
- callback ();
304
+ callback ();
280
305
};
281
306
```
282
307
@@ -285,19 +310,19 @@ exports.unregister = function (callback) {
285
310
``` javascript
286
311
' use strict' ;
287
312
288
- var path = require (' path' );
289
- var shellcraft = require (' ../' );
313
+ var path = require (' path' );
314
+ var shellcraft = require (' ../' );
290
315
291
316
var options = {
292
317
version: ' 0.0.1' ,
293
- prompt: ' orc>'
318
+ prompt: ' orc>' ,
294
319
};
295
- var shellExt = path .join (__dirname , ' myShellExtension.js' );
320
+ var shellExt = path .join (__dirname , ' myShellExtension.js' );
296
321
297
- shellcraft .registerExtension (shellExt, function () {
298
- shellcraft .begin (options, function (err , results ) {
322
+ shellcraft .registerExtension (shellExt, function () {
323
+ shellcraft .begin (options, function (err , results ) {
299
324
if (results) {
300
- console .log (results);
325
+ console .log (results);
301
326
}
302
327
});
303
328
});
@@ -306,6 +331,7 @@ shellcraft.registerExtension (shellExt, function () {
306
331
---
307
332
308
333
shell mode
334
+
309
335
```
310
336
$ node myShell.js
311
337
orc> _
322
348
```
323
349
324
350
CLI mode
351
+
325
352
```
326
353
$ node myShell.js -h
327
354
@@ -402,6 +429,5 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
402
429
SOFTWARE.
403
430
```
404
431
405
-
406
432
[ 1 ] : https://www.npmjs.org/package/commander
407
433
[ 2 ] : https://www.npmjs.org/package/inquirer
0 commit comments