Skip to content

Commit 751f10f

Browse files
committed
Replace string tests with patterns
1 parent 4e1fcaa commit 751f10f

File tree

4 files changed

+46
-47
lines changed

4 files changed

+46
-47
lines changed

src/DocoptNet/Docopt.cs

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -232,28 +232,33 @@ internal static IList<LeafPattern> ParseArgv(Tokens tokens, ICollection<Option>
232232
var parsed = new List<LeafPattern>();
233233
while (tokens.Current() is { } token)
234234
{
235-
if (token == "--")
235+
switch (token)
236236
{
237-
parsed.AddRange(tokens.Select(Argument.Unnamed));
238-
return parsed;
239-
}
240-
241-
if (token.StartsWith("--"))
242-
{
243-
parsed.AddRange(ParseLong(tokens, options));
244-
}
245-
else if (token.StartsWith("-") && tokens.Current() != "-")
246-
{
247-
parsed.AddRange(ParseShorts(tokens, options));
248-
}
249-
else if (optionsFirst)
250-
{
251-
parsed.AddRange(tokens.Select(Argument.Unnamed));
252-
return parsed;
253-
}
254-
else
255-
{
256-
parsed.Add(Argument.Unnamed(tokens.Move()));
237+
case "--":
238+
{
239+
parsed.AddRange(tokens.Select(Argument.Unnamed));
240+
return parsed;
241+
}
242+
case ['-', '-', ..]:
243+
{
244+
parsed.AddRange(ParseLong(tokens, options));
245+
break;
246+
}
247+
case ['-', not '-', ..]:
248+
{
249+
parsed.AddRange(ParseShorts(tokens, options));
250+
break;
251+
}
252+
case var _ when optionsFirst:
253+
{
254+
parsed.AddRange(tokens.Select(Argument.Unnamed));
255+
return parsed;
256+
}
257+
case var _:
258+
{
259+
parsed.Add(Argument.Unnamed(tokens.Move()));
260+
break;
261+
}
257262
}
258263
}
259264
return parsed;
@@ -369,21 +374,19 @@ static IEnumerable<Pattern> ParseAtom(Tokens tokens, ICollection<Option> options
369374
result.Add(new OptionsShortcut());
370375
break;
371376
default:
372-
if (token.StartsWith("--") && token != "--")
373-
{
374-
return ParseLong(tokens, options);
375-
}
376-
if (token.StartsWith("-") && token != "-" && token != "--")
377-
{
378-
return ParseShorts(tokens, options);
379-
}
380-
if ((token.StartsWith("<") && token.EndsWith(">")) || token.All(char.IsUpper))
381-
{
382-
result.Add(new Argument(tokens.Move()));
383-
}
384-
else
377+
switch (token)
385378
{
386-
result.Add(new Command(tokens.Move()));
379+
case ['-', '-', _, ..]:
380+
return ParseLong(tokens, options);
381+
case ['-', not '-', ..]:
382+
return ParseShorts(tokens, options);
383+
case ['<', _, .., '>']:
384+
case var t when t.All(char.IsUpper):
385+
result.Add(new Argument(tokens.Move()));
386+
break;
387+
default:
388+
result.Add(new Command(tokens.Move()));
389+
break;
387390
}
388391
break;
389392
}
@@ -395,7 +398,7 @@ static IEnumerable<Option> ParseShorts(Tokens tokens, ICollection<Option> option
395398
// shorts ::= '-' ( chars )* [ [ ' ' ] chars ] ;
396399

397400
var token = tokens.Move() ?? throw new NullReferenceException();
398-
Debug.Assert(token.StartsWith("-") && !token.StartsWith("--"));
401+
Debug.Assert(token is ['-', not '-', ..]);
399402
var left = token.TrimStart(['-']);
400403
var parsed = new List<Option>();
401404
while (left != "")
@@ -458,7 +461,7 @@ static IEnumerable<Option> ParseLong(Tokens tokens, ICollection<Option> options)
458461
(var ln, "", _) => (ln, false, null),
459462
var (ln, _, vs) => (ln, true, vs)
460463
};
461-
Debug.Assert(longName.StartsWith("--"));
464+
Debug.Assert(longName is ['-', '-', ..]);
462465
var similar = options.Where(o => o.LongName == longName).ToList();
463466
if (tokens.ThrowsInputError && similar.Count == 0)
464467
{

src/DocoptNet/Internals/Option.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,11 @@ public static Option Parse(string optionDescription)
6363
var (options, _, description) = optionDescription.Trim().Partition(DescSeparator);
6464
foreach (var s in options.Split(OptionDelimiters, StringSplitOptions.RemoveEmptyEntries))
6565
{
66-
if (s.StartsWith("--"))
67-
longName = s;
68-
else if (s.StartsWith("-"))
66+
switch (s)
6967
{
70-
shortName = s;
71-
}
72-
else
73-
{
74-
argCount = 1;
68+
case ['-', '-', ..]: longName = s ; break;
69+
case ['-', ..] : shortName = s; break;
70+
default : argCount = 1 ; break;
7571
}
7672
}
7773
if (argCount > 0)

tests/DocoptNet.Tests/CodeGeneration/LanguageAgnosticTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ e is AssertionException
100100
actual = "\"user-error\"";
101101
}
102102

103-
if (expected.StartsWith("{"))
103+
if (expected is ['{', ..])
104104
{
105105
var expectedDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(expected);
106106
Debug.Assert(expectedDict is not null);

tests/DocoptNet.Tests/LanguageAgnosticTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void Test(string doc, string cmdLine, string expected)
3434
actual = "\"user-error\"";
3535
}
3636

37-
if (expected.StartsWith("{"))
37+
if (expected is ['{', ..])
3838
{
3939
var expectedDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(expected);
4040
var actualDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(actual);

0 commit comments

Comments
 (0)