Skip to content

Remove support for argument values beginning with an unquoted forward slash #58

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

Merged
merged 4 commits into from
Nov 23, 2019
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
11 changes: 10 additions & 1 deletion build/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ set -e
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__branch=$(git branch --no-color | grep -E '^\*' | awk '{print $2}')

dotnet-sonarscanner begin /key:"jpdillingham_Utility.CommandLine.Arguments" /o:jpdillingham-github /d:sonar.host.url="https://sonarcloud.io" /d:sonar.exclusions="**/*examples*/**" /d:sonar.branch.name=${__branch} /d:sonar.login="${TOKEN_SONARCLOUD}" /d:sonar.cs.opencover.reportsPaths="tests/opencover.xml"
export MSYS2_ARG_CONV_EXCL="*"

dotnet-sonarscanner begin \
/key:"jpdillingham_Utility.CommandLine.Arguments" \
/o:jpdillingham-github \
/d:sonar.host.url="https://sonarcloud.io" \
/d:sonar.exclusions="**/*examples*/**" \
/d:sonar.branch.name=${__branch} \
/d:sonar.cs.opencover.reportsPaths="tests/opencover.xml" \
/d:sonar.login="${TOKEN_SONARCLOUD}" \

. "${__dir}/build.sh"
. "${__dir}/test.sh"
Expand Down
9 changes: 1 addition & 8 deletions src/Utility.CommandLine.Arguments/Arguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public class Arguments
/// <summary>
/// The regular expression with which to parse the command line string.
/// </summary>
private const string ArgumentRegEx = "(?:[-]{1,2}|\\/)([^=: ]+)[=: ]?(\\/?[^-'\"]\\S*|\\\"[^\"]*\\\"|\\\'[^']*\\\')?|([^ ([^'\\\"]+|\"[^\\\"]+\"|\\\'[^']+\\\')";
private const string ArgumentRegEx = "(?:[-]{1,2}|\\/)([^=: ]+)[=: ]?([^-'\"\\/]\\S*|\\\"[^\"]*\\\"|\\\'[^']*\\\')?|([^ ([^'\\\"]+|\"[^\\\"]+\"|\\\'[^']+\\\')";

/// <summary>
/// The regular expression with which to parse argument-value groups.
Expand Down Expand Up @@ -474,13 +474,6 @@ public static void Populate(Type type, Arguments arguments, bool clearExistingVa
}
else
{
// if the target property Type is an atomic (non-array or list) Type, convert the value and populate it,
// but not if the value is an array or list.
if (valueIsList)
{
throw new InvalidCastException($"Multiple values were specified for argument '{propertyName}', however it is not backed by an array or List<T>. Specify only one value.");
}

convertedValue = ChangeType(value, propertyName, propertyType);
}

Expand Down
62 changes: 61 additions & 1 deletion tests/Utility.CommandLine.Arguments.Tests/ArgumentsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public void ParseStringOfLongs()
[Fact]
public void ParseValueBeginningWithSlash()
{
Dictionary<string, object> test = Arguments.Parse("--file=/mnt/data/test.xml").ArgumentDictionary;
Dictionary<string, object> test = Arguments.Parse("--file='/mnt/data/test.xml'").ArgumentDictionary;

Assert.Equal("/mnt/data/test.xml", test["file"]);
}
Expand Down Expand Up @@ -1083,4 +1083,64 @@ public void PopulateBoolFollowedByTwoOperands()
Assert.Equal("operand2", Operands[1]);
}
}

[Collection("Arguments")]
public class TestWithAllBools
{
[Argument('a', "aa")]
private static bool A { get; set; }

[Argument('b', "bb")]
private static bool B { get; set; }

[Argument('c', "cc")]
private static bool C { get; set; }

[Theory]
[InlineData("-abc")]
[InlineData("-a -b -c")]
[InlineData("--aa --bb --cc")]
[InlineData("/a /b /c")]
[InlineData("/aa /bb /cc")]
[InlineData("-a --bb /c")]
[InlineData("--aa -b /c")]
[InlineData("/a /b -c")]
[InlineData("-a /bb --cc")]
public void PopulateStackedBools(string str)
{
Arguments.Populate(GetType(), str);

Assert.True(A && B && C);
}
}

[Collection("Arguments")]
public class TestListProp
{
[Argument('a', "aa")]
private static string A { get; set; }

[Argument('b', "bb")]
private static List<string> B { get; set; }

[Fact]
public void PopulateSingle()
{
var ex = Record.Exception(() => Arguments.Populate(GetType(), "--aa one --aa two"));

Assert.Null(ex);
Assert.Equal("two", A);
}

[Fact]
public void PopulateList()
{
var ex = Record.Exception(() => Arguments.Populate(GetType(), "--bb one --bb two"));

Assert.Null(ex);
Assert.Equal(2, B.Count);
Assert.Equal("one", B[0]);
Assert.Equal("two", B[1]);
}
}
}