Skip to content

Disable forward slash as argument delimiter by default #63

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 3 commits into from
Aug 18, 2020
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
14 changes: 13 additions & 1 deletion src/Utility.CommandLine.Arguments/Arguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,12 @@ 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 ArgumentRegExWithoutForwardSlash = "(?:[-]{1,2})([^=: ]+)[=: ]?([^-'\"]\\S*|\\\"[^\"]*\\\"|\\\'[^']*\\\')?|([^ ([^'\\\"]+|\"[^\\\"]+\"|\\\'[^']+\\\')";

/// <summary>
/// The regular expression with which to parse the command line string, including the ability to specify forward slashes.
/// </summary>
private const string ArgumentRegExWithForwardSlash = "(?:[-]{1,2}|\\/)([^=: ]+)[=: ]?([^-'\"\\/]\\S*|\\\"[^\"]*\\\"|\\\'[^']*\\\')?|([^ ([^'\\\"]+|\"[^\\\"]+\"|\\\'[^']+\\\')";

/// <summary>
/// The regular expression with which to parse argument-value groups.
Expand Down Expand Up @@ -207,6 +212,11 @@ private Arguments(string commandLineString, List<KeyValuePair<string, string>> a
TargetType = targetType;
}

/// <summary>
/// Gets or sets a value indicating whether forward slashes are processed as argument delimiters.
/// </summary>
public static bool EnableForwardSlash { get; set; } = false;

/// <summary>
/// Gets a dictionary containing the arguments and values specified in the command line arguments with which the
/// application was started.
Expand Down Expand Up @@ -242,6 +252,8 @@ private Arguments(string commandLineString, List<KeyValuePair<string, string>> a
/// </summary>
public Type TargetType { get; }

private static string ArgumentRegEx => EnableForwardSlash ? ArgumentRegExWithForwardSlash : ArgumentRegExWithoutForwardSlash;

/// <summary>
/// Gets the argument value corresponding to the specified <paramref name="index"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
Expand All @@ -11,7 +11,7 @@

<PropertyGroup>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>4.0.0</Version>
<Version>5.0.0</Version>
<Authors>JP Dillingham</Authors>
<Product>Utility.CommandLine.Arguments</Product>
<PackageProjectUrl>https://github.com/jpdillingham/Utility.CommandLine.Arguments</PackageProjectUrl>
Expand Down
24 changes: 16 additions & 8 deletions tests/Utility.CommandLine.Arguments.Tests/ArgumentsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public void ParseInnerQuotedStrings()
[Fact]
public void ParseLongAndShortMix()
{
Dictionary<string, object> test = Arguments.Parse("--one=1 -ab 2 /three:3 -4 4").ArgumentDictionary;
Dictionary<string, object> test = Arguments.Parse("--one=1 -ab 2 --three:3 -4 4").ArgumentDictionary;

Assert.Equal("1", test["one"]);
Assert.True(test.ContainsKey("a"));
Expand Down Expand Up @@ -355,7 +355,7 @@ public void ParseStrictOperandsStart()
[Fact]
public void ParseStringOfLongs()
{
Dictionary<string, object> test = Arguments.Parse("--one 1 --two=2 /three:3 --four \"4 4\" --five='5 5'").ArgumentDictionary;
Dictionary<string, object> test = Arguments.Parse("--one 1 --two=2 --three:3 --four \"4 4\" --five='5 5'").ArgumentDictionary;

Assert.NotEmpty(test);
Assert.Equal(5, test.Count);
Expand Down Expand Up @@ -395,6 +395,15 @@ public void ParseValueWithQuotedPeriod()
[InlineData("\\foo\\bar")]
[InlineData("\\\\foo")]
[InlineData("\\\\foo\\bar")]
[InlineData("..//")]
[InlineData(".//foo")]
[InlineData("..//foo")]
[InlineData("..//..")]
[InlineData("//")]
[InlineData("//foo")]
[InlineData("//foo//bar")]
[InlineData("////foo")]
[InlineData("////foo//bar")]
public void ParseValueStartingWithNonWord(string value)
{
Dictionary<string, object> test = Arguments.Parse($"-f {value}").ArgumentDictionary;
Expand Down Expand Up @@ -1100,12 +1109,11 @@ public class TestWithAllBools
[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")]
[InlineData("-a -b -c")]
[InlineData("--aa --bb --cc")]
[InlineData("-a --bb -c")]
[InlineData("--aa -b -c")]
[InlineData("-a --bb --cc")]
public void PopulateStackedBools(string str)
{
Arguments.Populate(GetType(), str);
Expand Down