Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions lib/commander.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,20 @@ Command.prototype.action = function(fn){
self.unknownOption(parsed.unknown[0]);
}

// Any remaining arguments in parsed.args were originally thought
// to be tied to options, but ended up being tied to the original
// command.
if (parsed.args.length > 0) {
parsed.args.forEach(function(arg, index) {
// For each argument that is inserted, the insertionIndex gets
// offset by one for future arguments. In order to compensate
// for this, simply add in the index of this argument in
// parsed.args to the insertionIndex, as this represents how
// many arguments have been inserted prior to this one.
args.splice(arg.insertionIndex + index - 1, 0, arg.value);
});
}

self.args.forEach(function(arg, i){
if (arg.required && null == args[i]) {
self.missingArgument(arg.name);
Expand Down Expand Up @@ -461,7 +475,10 @@ Command.prototype.parseOptions = function(argv){
continue;
}

if (literal) {
// Object arguments were parameters that were guessed
// to be tied to an option, but actually turned out to
// be arguments to the command. Pass them on as such.
if (literal || 'object' == typeof arg) {
args.push(arg);
continue;
}
Expand All @@ -475,11 +492,16 @@ Command.prototype.parseOptions = function(argv){
if (option.required) {
arg = argv[++i];
if (null == arg) return this.optionMissingArgument(option);
if ('object' == typeof arg) arg = arg.value;
if ('-' == arg[0]) return this.optionMissingArgument(option, arg);
this.emit(option.name(), arg);
// optional arg
} else if (option.optional) {
arg = argv[i+1];
if ('object' == typeof arg) {
arg = arg.value;
}

if (null == arg || '-' == arg[0]) {
arg = null;
} else {
Expand All @@ -501,7 +523,15 @@ Command.prototype.parseOptions = function(argv){
// an argument for this option, we pass it on.
// If it isn't, then it'll simply be ignored
if (argv[i+1] && '-' != argv[i+1][0]) {
unknownOptions.push(argv[++i]);
// This option could take no parameters, at which point
// the next argument is really for the command rather than
// the option. Thus, maintain the argument along with the
// location to insert it into the args array if necessary
// later on.
unknownOptions.push({
value: argv[++i],
insertionIndex: args.length
});
}
continue;
}
Expand Down