Skip to content

Commit

Permalink
Fix yarn version which yields same output as yarn --version (#249…
Browse files Browse the repository at this point in the history
…1) (#2510)

* Fix `yarn version` which yields same output as `yarn --version` (#2491)

There was a conflict when commander attempts to parse the incoming args
between the command executed and the options since the name `version`
was shared. In other words, executing the command `yarn version`
would yield the same output as `yarn --version`. This relates to PR #2268.

* Shift first arg that shares name with an option to circumvent conflicting 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
lovelypuppy0607 committed Feb 1, 2017
1 parent 44267a6 commit 59c2976
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 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 @@ -172,6 +178,11 @@ if (commandName === 'help' || args.indexOf('--help') >= 0 || args.indexOf('-h')

// parse flags
args.unshift(commandName);

if (ARGS_THAT_SHARE_NAMES_WITH_OPTIONS.indexOf(commandName) >= 0 && args[0] === commandName) {
args.shift();
}

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

Expand Down

0 comments on commit 59c2976

Please sign in to comment.