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

Allow single dash as a value #669

Merged
merged 1 commit into from
Jul 24, 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
6 changes: 6 additions & 0 deletions src/CommandLine/Core/Tokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ private static IEnumerable<Token> TokenizeShortName(
string value,
Func<string, NameLookupResult> nameLookup)
{
//Allow single dash as a value
if (value.Length == 1 && value[0] == '-')
{
yield return Token.Value(value);
yield break;
}
if (value.Length > 1 && value[0] == '-' && value[1] != '-')
{
var text = value.Substring(1);
Expand Down
20 changes: 20 additions & 0 deletions tests/CommandLine.Tests/Unit/Core/TokenizerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,26 @@ public void Should_return_error_if_option_format_with_equals_is_not_correct()
Assert.Equal(ErrorType.BadFormatTokenError, tokens.First().Tag);
Assert.Equal(ErrorType.BadFormatTokenError, tokens.Last().Tag);
}


[Theory]
[InlineData(new[] { "-a", "-" }, 2,"a","-")]
[InlineData(new[] { "--file", "-" }, 2,"file","-")]
[InlineData(new[] { "-f-" }, 2,"f", "-")]
[InlineData(new[] { "--file=-" }, 2, "file", "-")]
[InlineData(new[] { "-a", "--" }, 1, "a", "a")]
public void single_dash_as_a_value(string[] args, int countExcepted,string first,string last)
{
//Arrange
//Act
var result = Tokenizer.Tokenize(args, name => NameLookupResult.OtherOptionFound, token => token);
var tokens = result.SucceededWith().ToList();
//Assert
tokens.Should().NotBeNull();
tokens.Count.Should().Be(countExcepted);
tokens.First().Text.Should().Be(first);
tokens.Last().Text.Should().Be(last);
}
}

}