Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SimpleCommandLineParser to handle arguments with key and value #220

Merged
merged 2 commits into from
Oct 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</PropertyGroup>

<PropertyGroup>
<VersionPrefix>1.0.4.18</VersionPrefix>
<VersionPrefix>1.0.4.19</VersionPrefix>
</PropertyGroup>

<Choose>
Expand Down
2 changes: 1 addition & 1 deletion GitHubReleaseNotes.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
https://github.com/StefH/GitHubReleaseNotes

GitHubReleaseNotes.exe . --output CHANGELOG.md --skip-empty-releases
GitHubReleaseNotes.exe . --output CHANGELOG.md --skip-empty-releases --version 1.0.4.19
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ A C# .NET version based on [mock4net](https://github.com/alexvictoor/mock4net) w
| &nbsp;&nbsp;**Sonar Bugs** | [![Sonar Bugs](https://sonarcloud.io/api/project_badges/measure?project=wiremock&metric=bugs)](https://sonarcloud.io/project/issues?id=wiremock&resolved=false&types=BUG) |
| &nbsp;&nbsp;**Sonar Code Smells** | [![Sonar Code Smells](https://sonarcloud.io/api/project_badges/measure?project=wiremock&metric=code_smells)](https://sonarcloud.io/project/issues?id=wiremock&resolved=false&types=CODE_SMELL) |
| &nbsp;&nbsp;**Sonar Coverage** | [![Sonar Coverage](https://sonarcloud.io/api/project_badges/measure?project=wiremock&metric=coverage)](https://sonarcloud.io/component_measures?id=wiremock&metric=coverage) |
| &nbsp;&nbsp;**Codecov** | [![codecov](https://codecov.io/gh/WireMock-Net/WireMock.Net/branch/master/graph/badge.svg)](https://codecov.io/gh/WireMock-Net/WireMock.Net) |
| &nbsp;&nbsp;**Coveralls** | [![Coverage Status](https://coveralls.io/repos/github/WireMock-Net/WireMock.Net/badge.svg?branch=master)](https://coveralls.io/github/WireMock-Net/WireMock.Net?branch=master) |
| |
| ***NuGet*** | &nbsp; |
Expand Down
38 changes: 5 additions & 33 deletions src/WireMock.Net.StandAlone/SimpleCommandLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,20 @@ namespace WireMock.Net.StandAlone
internal class SimpleCommandLineParser
{
private const string Sigil = "--";
private const string SigilAzureServiceFabric = "'--";

private enum SigilType
{
Normal,
AzureServiceFabric
}

private IDictionary<string, string[]> Arguments { get; } = new Dictionary<string, string[]>();

public void Parse(string[] args)
public void Parse(string[] arguments)
{
SigilType sigil = SigilType.Normal;
string currentName = null;

var values = new List<string>();
foreach (string arg in args)

// Split a single argument on a space character to fix issue (e.g. Azure Service Fabric) when an argument is supplied like "--x abc" or '--x abc'
foreach (string arg in arguments.SelectMany(arg => arg.Split(' ')))
{
if (arg.StartsWith(Sigil))
{
sigil = SigilType.Normal;

if (!string.IsNullOrEmpty(currentName))
{
Arguments[currentName] = values.ToArray();
Expand All @@ -38,33 +30,13 @@ public void Parse(string[] args)
values.Clear();
currentName = arg.Substring(Sigil.Length);
}
// Azure Service Fabric passes the command line parameter surrounded with single quotes. (https://github.com/Microsoft/service-fabric/issues/234)
else if (arg.StartsWith(SigilAzureServiceFabric))
{
sigil = SigilType.AzureServiceFabric;

if (!string.IsNullOrEmpty(currentName))
{
Arguments[currentName] = values.ToArray();
}

values.Clear();
currentName = arg.Substring(SigilAzureServiceFabric.Length);
}
else if (string.IsNullOrEmpty(currentName))
{
Arguments[arg] = new string[0];
}
else
{
if (sigil == SigilType.Normal)
{
values.Add(arg);
}
else
{
values.Add(arg.Substring(0, arg.Length - 1));
}
values.Add(arg);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public void SimpleCommandLineParser_Parse_Arguments()
}

[Fact]
public void SimpleCommandLineParser_Parse_ArgumentsWithSingleQuotes()
public void SimpleCommandLineParser_Parse_ArgumentsAsCombinedKeyAndValue()
{
// Assign
_parser.Parse(new[] { "'--test1", "one'", "'--test2", "two'", "'--test3", "three'" });
_parser.Parse(new[] { "--test1 one", "--test2 two", "--test3 three" });

// Act
string value1 = _parser.GetStringValue("test1");
Expand All @@ -51,7 +51,7 @@ public void SimpleCommandLineParser_Parse_ArgumentsWithSingleQuotes()
public void SimpleCommandLineParser_Parse_ArgumentsMixed()
{
// Assign
_parser.Parse(new[] { "'--test1", "one'", "--test2", "two", "--test3", "three" });
_parser.Parse(new[] { "--test1 one", "--test2", "two", "--test3 three" });

// Act
string value1 = _parser.GetStringValue("test1");
Expand All @@ -68,7 +68,7 @@ public void SimpleCommandLineParser_Parse_ArgumentsMixed()
public void SimpleCommandLineParser_Parse_GetBoolValue()
{
// Assign
_parser.Parse(new[] { "'--test1", "false'", "--test2", "true" });
_parser.Parse(new[] { "'--test1", "false'", "--test2 true" });

// Act
bool value1 = _parser.GetBoolValue("test1");
Expand All @@ -85,7 +85,7 @@ public void SimpleCommandLineParser_Parse_GetBoolValue()
public void SimpleCommandLineParser_Parse_GetIntValue()
{
// Assign
_parser.Parse(new[] { "'--test1", "42'", "--test2", "55" });
_parser.Parse(new[] { "--test1", "42", "--test2 55" });

// Act
int? value1 = _parser.GetIntValue("test1");
Expand Down