diff --git a/src/GitVersionExe/ArgumentParser.cs b/src/GitVersionExe/ArgumentParser.cs index d5954466e9..10c68d78f8 100644 --- a/src/GitVersionExe/ArgumentParser.cs +++ b/src/GitVersionExe/ArgumentParser.cs @@ -12,7 +12,7 @@ public class ArgumentParser { public static OptionSet GetOptionSet(Arguments arguments) { - return new OptionSet() + return new OptionSet { { "h|?|help", "Show this message and exit", @@ -84,23 +84,12 @@ public static Arguments ParseArguments(string commandLineArguments) public static Arguments ParseArguments(List commandLineArguments) { - if (commandLineArguments.Count == 0) + if (commandLineArguments.Count <= 0) { - return new Arguments - { - TargetPath = Environment.CurrentDirectory - }; - } - - var firstArgument = commandLineArguments.First(); - - if (IsInit(firstArgument)) - { - return new Arguments - { - TargetPath = Environment.CurrentDirectory, - Init = true - }; + return new Arguments() + { + TargetPath = Environment.CurrentDirectory + }; } var arguments = new Arguments(); @@ -108,11 +97,7 @@ public static Arguments ParseArguments(List commandLineArguments) var p = GetOptionSet(arguments); var additionalArguments = p.Parse(commandLineArguments); - if (additionalArguments.Count > 0) - { - // TODO: should not overwrite if --targetPath is specified - arguments.TargetPath = additionalArguments[0]; - } + ParseSpecialArguments(additionalArguments, arguments); return arguments; @@ -210,6 +195,37 @@ public static Arguments ParseArguments(List commandLineArguments) */ } + static void ParseSpecialArguments(List additionalArguments, Arguments arguments) + { + if (additionalArguments.Count <= 0) + { + return; + } + + var firstArgument = additionalArguments[0]; + + if (IsInit(firstArgument)) + { + arguments.TargetPath = Environment.CurrentDirectory; + arguments.Init = true; // should be replaced by --init switch + } + else if (IsSwitch(firstArgument, "")) + { + throw new WarningException(string.Format("Could not parse command line parameter '{0}'.", firstArgument)); + } + else + { + // TODO: should not overwrite if --targetPath is specified + arguments.TargetPath = firstArgument; + } + + if (additionalArguments.Count > 1) + { + // fail on first unknown argument: + throw new WarningException(string.Format("Could not parse command line parameter '{0}'.", additionalArguments[1])); + } + } + static NameValueCollection CollectSwitchesAndValuesFromArguments(List namedArguments) { var args = new NameValueCollection();