forked from auth0/nested-yargs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
265 lines (204 loc) · 7.48 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
var Bluebird = require('bluebird');
var Colors = require('colors');
var Yargs = require('yargs');
var _ = require('lodash');
var Cli = module.exports;
function Category (name, description, options) {
this.commands = {};
this.name = name || '$0';
this.description = description || '';
this.options = options || {};
this.parent = null;
Object.defineProperty(this, 'path', {
enumerable: true,
get: function () {
return this.parent
? this.parent.path.concat([this.name])
: [this.name];
}
});
}
Category.prototype.command = function (command) {
this.commands[command.name] = command;
command.parent = this;
return this;
};
Category.prototype.run = function (yargs) {
var self = this;
var errorHandler = createErrorHandler(yargs);
_.forEach(this.commands, function (command) {
yargs.command(command.name, command.description, command.run.bind(command));
});
if (this.options.setup) this.options.setup(yargs);
if (this.options.options) yargs.options(this.options.options);
if (this.options.examples) _.forEach(this.options.examples, yargs.example.bind(yargs));
if (this.options.version) yargs.version(this.options.version);
yargs
.usage('Usage: ' + this.path.join(' ') + ' <command>')
.check(function (argv) {
var commandName = argv._[self.path.length - 1];
var command = self.commands[commandName];
if (!commandName) throw Cli.usageError('Please enter a valid command.');
if (!command) throw Cli.usageError('No such command `'
+ self.path.slice(1).join(' ')+ ' '
+ commandName + '`');
return true;
})
.demand(self.path.length, 'Please enter a valid command.')
.fail(errorHandler);
yargs.help('help');
var argv = yargs.argv;
return argv;
};
function Command (name, description, options) {
this.name = name || '$0';
this.description = description || '';
this.parent = null;
this.options = _.defaultsDeep(options || {}, {
params: '',
});
Object.defineProperty(this, 'path', {
enumerable: true,
get: function () {
return this.parent
? this.parent.path.concat([this.name])
: [this.name];
}
});
}
Command.prototype.run = function (yargs) {
var self = this;
var errorHandler = createErrorHandler(yargs);
if (this.options.setup) this.options.setup(yargs);
if (this.options.options) yargs.options(this.options.options);
if (this.options.examples) _.forEach(this.options.examples, yargs.example.bind(yargs));
if (this.options.version) yargs.version(this.options.version);
yargs
.check(function (argv) {
// We can't use `yargs.strict()` because it is possible that
// `options.setup` changes the options during execution and this
// seems to interfere with the timing for strict mode.
// Additionally, `yargs.strict()` does not seem to handle pre-
// negated params like `--no-parse`.
checkForUnknownArguments(yargs, argv);
if (self.options.params) parseParams(yargs, argv, self);
return true;
})
.fail(errorHandler)
.usage('Usage: ' + this.path.join(' ')
+ ' [options]'
+ (this.options.params ? ' ' + this.options.params : ''));
yargs.help('help');
var argv = yargs.argv;
if (this.options.handler)
Bluebird.try(this.options.handler.bind(this, argv))
.catch(errorHandler);
return argv;
};
function createErrorHandler (yargs) {
return function (err) {
if (!err || !(err instanceof Error) || err.isUsageError)
yargs.showHelp();
console.log((err.message || err).red);
process.exit(1);
};
}
// Adapted from: https://github.com/bcoe/yargs/blob/master/lib/validation.js#L83-L110
function checkForUnknownArguments (yargs, argv) {
var aliasLookup = {};
var descriptions = yargs.getUsageInstance().getDescriptions();
var demanded = yargs.getDemanded();
var unknown = [];
Object.keys(yargs.parsed.aliases || {}).forEach(function (key) {
yargs.parsed.aliases[key].forEach(function (alias) {
aliasLookup[alias] = key;
});
});
Object.keys(argv).forEach(function (key) {
if (key !== '$0' && key !== '_' && key !== 'params' &&
!descriptions.hasOwnProperty(key) &&
!demanded.hasOwnProperty(key) &&
!aliasLookup.hasOwnProperty('no-' + key) &&
!aliasLookup.hasOwnProperty(key)) {
unknown.push(key);
}
});
if (unknown.length === 1) {
throw Cli.usageError('Unknown argument: ' + unknown[0]);
} else if (unknown.length > 1) {
throw Cli.usageError('Unknown arguments: ' + unknown.join(', '));
}
}
function parseParams (yargs, argv, command) {
var required = 0;
var optional = 0;
var variadic = false;
argv.params = {};
command.options.params.replace(/(<[^>]+>|\[[^\]]+\])/g,
function (match) {
if (variadic)
throw Cli.applicationError('Variadic parameters must the final parameter.');
var isRequired = match[0] === '<';
var param = match
.slice(1, -1)
.replace(/(.*)\.\.\.$/, function (m, param) {
variadic = true;
return param;
});
var value;
if (isRequired) required++;
else optional++;
if (variadic) {
value = argv._.slice(command.path.length - 2 + required + optional)
.map(String);
if (isRequired && !value.length) throw Cli.usageError('Parameter '
+ '`' + param + '` is must have at least one item.');
} else {
if (isRequired && optional > 0)
throw Cli.applicationError('Optional parameters must be specified last');
value = argv._[command.path.length - 2 + required + optional];
if (value) value = String(value);
if (isRequired && typeof value === 'undefined') throw Cli.usageError('Parameter '
+ '`' + param + '` is required.');
}
argv.params[param] = value;
});
}
Cli.createApp = function (options) {
return new Category('$', '', options);
};
Cli.createCategory = function (name, description, options) {
if (_.isObject(description)) {
options = description;
description = '';
}
return new Category(name, description, options);
};
Cli.createCommand = function (name, description, options) {
if (_.isObject(name)) {
options = name;
name = '$0';
description = '';
}
if (_.isObject(description)) {
options = description;
description = '';
}
return new Command(name, description, options);
};
Cli.run = function (command, yargs) {
var argv = command.run(yargs || Yargs);
return argv;
};
Cli.usageError = function (message, data) {
var error = new Error(message ? message : undefined);
error.data = data || null;
error.isUsageError = true;
return error;
};
Cli.applicationError = function (message, data) {
var error = new Error(message ? message : undefined);
error.data = data || null;
error.isApplicationError = true;
return error;
};