Skip to content

Commit

Permalink
Allow single dash as a value (#669)
Browse files Browse the repository at this point in the history
  • Loading branch information
moh-hassan authored Jul 24, 2020
1 parent ca7d933 commit 2807896
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
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);
}
}

}

0 comments on commit 2807896

Please sign in to comment.