Skip to content

Commit 2807896

Browse files
authored
Allow single dash as a value (#669)
1 parent ca7d933 commit 2807896

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/CommandLine/Core/Tokenizer.cs

+6
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ private static IEnumerable<Token> TokenizeShortName(
135135
string value,
136136
Func<string, NameLookupResult> nameLookup)
137137
{
138+
//Allow single dash as a value
139+
if (value.Length == 1 && value[0] == '-')
140+
{
141+
yield return Token.Value(value);
142+
yield break;
143+
}
138144
if (value.Length > 1 && value[0] == '-' && value[1] != '-')
139145
{
140146
var text = value.Substring(1);

tests/CommandLine.Tests/Unit/Core/TokenizerTests.cs

+20
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,26 @@ public void Should_return_error_if_option_format_with_equals_is_not_correct()
123123
Assert.Equal(ErrorType.BadFormatTokenError, tokens.First().Tag);
124124
Assert.Equal(ErrorType.BadFormatTokenError, tokens.Last().Tag);
125125
}
126+
127+
128+
[Theory]
129+
[InlineData(new[] { "-a", "-" }, 2,"a","-")]
130+
[InlineData(new[] { "--file", "-" }, 2,"file","-")]
131+
[InlineData(new[] { "-f-" }, 2,"f", "-")]
132+
[InlineData(new[] { "--file=-" }, 2, "file", "-")]
133+
[InlineData(new[] { "-a", "--" }, 1, "a", "a")]
134+
public void single_dash_as_a_value(string[] args, int countExcepted,string first,string last)
135+
{
136+
//Arrange
137+
//Act
138+
var result = Tokenizer.Tokenize(args, name => NameLookupResult.OtherOptionFound, token => token);
139+
var tokens = result.SucceededWith().ToList();
140+
//Assert
141+
tokens.Should().NotBeNull();
142+
tokens.Count.Should().Be(countExcepted);
143+
tokens.First().Text.Should().Be(first);
144+
tokens.Last().Text.Should().Be(last);
145+
}
126146
}
127147

128148
}

0 commit comments

Comments
 (0)