Skip to content

Commit

Permalink
Shift first arg that shares name with an option to circumvent conflic…
Browse files Browse the repository at this point in the history
…ting name bug

Relating to tj/commander.js#346, when an arg shares the same name as an
option, it wrongly ignores the arg command and executes the option
instead. Therefore, executing 'yarn version' would instead translate
to 'yarn --version'. This logic can subsequently be removed once this issue is resolved.
  • Loading branch information
Constantine Antonakos committed Jan 30, 2017
1 parent fa3c4c3 commit 7772b00
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ for (let i = 0; i < args.length; i++) {
}
}

// NOTE: Pending resolution of https://github.com/tj/commander.js/issues/346
// Remove this (and subsequent use in the logic below) after bug is resolved and issue is closed
const ARGS_THAT_SHARE_NAMES_WITH_OPTIONS = [
'version',
];

// set global options
commander.version(pkg.version);
commander.usage('[command] [flags]');
Expand Down Expand Up @@ -170,13 +176,13 @@ if (commandName === 'help' || args.indexOf('--help') >= 0 || args.indexOf('-h')
process.exit(1);
}

if (!command) {
args.unshift(commandName);
command = commands.run;
// parse flags
args.unshift(commandName);

if (ARGS_THAT_SHARE_NAMES_WITH_OPTIONS.indexOf(commandName) >= 0 && args[0] === commandName) {
args.shift();
}
invariant(command, 'missing command');

// parse flags
commander.parse(startArgs.concat(args));
commander.args = commander.args.concat(endArgs);

Expand Down

0 comments on commit 7772b00

Please sign in to comment.