Skip to content

Commit

Permalink
Refactoring of special cases, for GitTools#428
Browse files Browse the repository at this point in the history
  • Loading branch information
serra committed Aug 15, 2015
1 parent b1970b8 commit 2f35fab
Showing 1 changed file with 38 additions and 22 deletions.
60 changes: 38 additions & 22 deletions src/GitVersionExe/ArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -84,35 +84,20 @@ public static Arguments ParseArguments(string commandLineArguments)

public static Arguments ParseArguments(List<string> 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();

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;

Expand Down Expand Up @@ -210,6 +195,37 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
*/
}

static void ParseSpecialArguments(List<string> 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<string> namedArguments)
{
var args = new NameValueCollection();
Expand Down

0 comments on commit 2f35fab

Please sign in to comment.