Skip to content

Commit

Permalink
Merge pull request #70 from appcelerator/timob-15608
Browse files Browse the repository at this point in the history
[TIMOB-15608] Fixed bug with empty or non-existant option values. Also f...
  • Loading branch information
ayeung committed Nov 7, 2013
2 parents 6ca7912 + d648f98 commit 4c816f2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
8 changes: 7 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ CLI.prototype.validate = function validate(next) {
if (ex instanceof GracefulShutdown) {
// exit the validation and do NOT run the command
_t.command.module.run = function () {};
return next();
return next(); // purposely call next()
}
throw ex;
}
Expand Down Expand Up @@ -936,6 +936,12 @@ CLI.prototype.prompt = function prompt(items, done) {
if (opt.prompt && typeof opt.prompt == 'function') {
opt.prompt(function (field) {
if (!field) return callback();

// if this option had a bad value and caused an error, then disable auto selecting
if (opt._err && field.autoSelectOne) {
field.autoSelectOne = false
}

field.prompt(function (err, value) {
if (err) {
errs.push(err);
Expand Down
26 changes: 15 additions & 11 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -717,20 +717,24 @@ Context.prototype.parse = function parse(args, commands, skipCallbacks) {
// --flag or --option
name = arg.match(/^(?:--|—)(.+)/)[1];
next = args[i + 1];
if (next != void 0 && !next.match(/^-/) && !this.flags[name] && (this.aliases[name] ? !this.flags[this.aliases[name]] : true)) {
if (!this.flags[name] && (this.aliases[name] ? !this.flags[this.aliases[name]] : true)) {
// --option value

// if we have an array of known commands and we haven't encountered
// our first argument, then we don't know if this is truly an option
// or flag, so we check if the value is a known command
if (Array.isArray(commands) && this.argv._.length == 0 && commands.indexOf(next) != -1) {
// treat this as a flag and the value as an argument/command
setArg(name, true);
argv._.push(next);
if (next != void 0 && !next.match(/^-/)) {
// if we have an array of known commands and we haven't encountered
// our first argument, then we don't know if this is truly an option
// or flag, so we check if the value is a known command
if (Array.isArray(commands) && this.argv._.length == 0 && commands.indexOf(next) != -1) {
// treat this as a flag and the value as an argument/command
setArg(name, true);
argv._.push(next);
} else {
setArg(name, next);
}
i++;
} else {
setArg(name, next);
// no next value, just set it to an empty string
setArg(name, '');
}
i++;
} else if (/true|false/.test(next)) {
// --flag true
setArg(name, next == 'true');
Expand Down

0 comments on commit 4c816f2

Please sign in to comment.