Skip to content

Commit 8a0abe0

Browse files
committed
默认忽略未知选项(仍然要求不可忽略未知位置参数)
1 parent 2bfe8bf commit 8a0abe0

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

src/DotNetCampus.CommandLine/CommandLineParsingOptions.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,39 @@ namespace DotNetCampus.Cli;
66
public readonly record struct CommandLineParsingOptions
77
{
88
/// <inheritdoc cref="CommandLineStyle.Flexible" />
9-
public static CommandLineParsingOptions Flexible => new() { Style = CommandLineStyle.Flexible };
9+
public static CommandLineParsingOptions Flexible => new()
10+
{
11+
Style = CommandLineStyle.Flexible,
12+
UnknownArgumentsHandling = UnknownCommandArgumentHandling.IgnoreUnknownOptionalArguments,
13+
};
1014

1115
/// <inheritdoc cref="CommandLineStyle.DotNet" />
12-
public static CommandLineParsingOptions DotNet => new() { Style = CommandLineStyle.DotNet };
16+
public static CommandLineParsingOptions DotNet => new()
17+
{
18+
Style = CommandLineStyle.DotNet,
19+
UnknownArgumentsHandling = UnknownCommandArgumentHandling.IgnoreUnknownOptionalArguments,
20+
};
1321

1422
/// <inheritdoc cref="CommandLineStyle.Gnu" />
15-
public static CommandLineParsingOptions Gnu => new() { Style = CommandLineStyle.Gnu };
23+
public static CommandLineParsingOptions Gnu => new()
24+
{
25+
Style = CommandLineStyle.Gnu,
26+
UnknownArgumentsHandling = UnknownCommandArgumentHandling.IgnoreUnknownOptionalArguments,
27+
};
1628

1729
/// <inheritdoc cref="CommandLineStyle.Posix" />
18-
public static CommandLineParsingOptions Posix => new() { Style = CommandLineStyle.Posix };
30+
public static CommandLineParsingOptions Posix => new()
31+
{
32+
Style = CommandLineStyle.Posix,
33+
UnknownArgumentsHandling = UnknownCommandArgumentHandling.IgnoreUnknownOptionalArguments,
34+
};
1935

2036
/// <inheritdoc cref="CommandLineStyle.Windows" />
21-
public static CommandLineParsingOptions Windows => new() { Style = CommandLineStyle.Windows };
37+
public static CommandLineParsingOptions Windows => new()
38+
{
39+
Style = CommandLineStyle.Windows,
40+
UnknownArgumentsHandling = UnknownCommandArgumentHandling.IgnoreUnknownOptionalArguments,
41+
};
2242

2343
/// <inheritdoc cref="CommandLineStyle.Windows" />
2444
[Obsolete("为避免理解歧义,已弃用此名称,请使用 Windows 代替。")]

tests/DotNetCampus.CommandLine.Tests/CommandLineStyleTestingExtensions.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@
22

33
namespace DotNetCampus.Cli.Tests;
44

5+
using UH = UnknownCommandArgumentHandling;
6+
57
internal static class CommandLineStyleTestingExtensions
68
{
79
public static CommandLineParsingOptions ToParsingOptions(this TestCommandLineStyle style) => style switch
810
{
9-
TestCommandLineStyle.Flexible => CommandLineParsingOptions.Flexible,
10-
TestCommandLineStyle.DotNet => CommandLineParsingOptions.DotNet,
11-
TestCommandLineStyle.Gnu => CommandLineParsingOptions.Gnu,
12-
TestCommandLineStyle.Posix => CommandLineParsingOptions.Posix,
13-
TestCommandLineStyle.Windows => CommandLineParsingOptions.Windows,
14-
TestCommandLineStyle.Url => CommandLineParsingOptions.Flexible with { SchemeNames = ["test"] },
11+
TestCommandLineStyle.Flexible => CommandLineParsingOptions.Flexible with { UnknownArgumentsHandling = UH.AllArgumentsMustBeRecognized },
12+
TestCommandLineStyle.DotNet => CommandLineParsingOptions.DotNet with { UnknownArgumentsHandling = UH.AllArgumentsMustBeRecognized },
13+
TestCommandLineStyle.Gnu => CommandLineParsingOptions.Gnu with { UnknownArgumentsHandling = UH.AllArgumentsMustBeRecognized },
14+
TestCommandLineStyle.Posix => CommandLineParsingOptions.Posix with { UnknownArgumentsHandling = UH.AllArgumentsMustBeRecognized },
15+
TestCommandLineStyle.Windows => CommandLineParsingOptions.Windows with { UnknownArgumentsHandling = UH.AllArgumentsMustBeRecognized },
16+
TestCommandLineStyle.Url => CommandLineParsingOptions.Flexible with
17+
{
18+
SchemeNames = ["test"],
19+
UnknownArgumentsHandling = UH.AllArgumentsMustBeRecognized,
20+
},
1521
_ => throw new ArgumentOutOfRangeException(nameof(style), style, null),
1622
};
1723
}

0 commit comments

Comments
 (0)