From 7772b00c1d616a7a2b016000ecfb8e2de0dcdaee Mon Sep 17 00:00:00 2001 From: Constantine Antonakos Date: Mon, 30 Jan 2017 12:01:36 -0500 Subject: [PATCH] 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. --- src/cli/index.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/cli/index.js b/src/cli/index.js index ef98355354..679fd9c0dc 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -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]'); @@ -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);