From 2f35fabcfb1f2376a25e2a2a95a31cda3e46306f Mon Sep 17 00:00:00 2001 From: Marijn van der Zee Date: Sat, 15 Aug 2015 12:39:05 +0200 Subject: [PATCH] Refactoring of special cases, for #428 --- src/GitVersionExe/ArgumentParser.cs | 60 ++++++++++++++++++----------- 1 file changed, 38 insertions(+), 22 deletions(-) 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();