-
Notifications
You must be signed in to change notification settings - Fork 0
/
value-parser.js
34 lines (31 loc) · 995 Bytes
/
value-parser.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
'use strict';
function ValueParser(args) {
this.name = args.name;
this.arg = args.arg;
this.converter = args.converter;
this.validator = args.validator;
this.collector = args.collector;
this.required = args.required;
}
ValueParser.prototype.parse = function parse(iterator, delegate, flag) {
if (this.collector.collected) {
return true;
}
if (iterator.hasArgument()) {
var argument = iterator.shiftArgument();
var value = this.converter.convert(argument, iterator, delegate, flag);
if (this.validator.validate(value, delegate)) {
return this.collector.collect(value, iterator, delegate);
} else {
delegate.error('Invalid: ' + this.arg);
delegate.cursor(-1);
return false;
}
} else if (this.required) {
delegate.error('Expected: ' + this.arg);
delegate.cursor();
return false;
}
return true;
};
module.exports = ValueParser;