Exception Thrown Rather than Error Message And Help When Command Has Both Custom Parser and Validator
还没有人认领这个 Issue。
评估
调研方向
从提供的 C# 复现代码开始,重点关注自定义解析器、附加到 rootCmdArgs 的验证回调,以及 Parse(args)/InvokeAsync() 入口点。跟踪在两个回调同时存在时,验证和转换如何处理解析器添加的错误。完成标准是:无效的 --dats 输入会报告解析器错误以及命令帮助,而不是抛出未处理的 InvalidOperationException。
由索引模型根据 Issue 内容生成。
描述
In the code below, running (with SDK 10.0.100-rc.1.25451.107)
dotnet run ./TestOne.cs 12 23 34 --dats ab bc cd
Leads to an exception being thrown
Unhandled exception. System.InvalidOperationException: Argument "--dats" is not two characters long
at System.CommandLine.Binding.ArgumentConverter.GetValueOrDefault[T](ArgumentConversionResult result)
at System.CommandLine.Parsing.ArgumentResult.GetValueOrDefault[T]()
at Program.<>c.<<Main>$>b__0_3(ArgumentResult argRes) in [HIDDEN]\TestOne.cs:line 52
at System.CommandLine.Parsing.ArgumentResult.ValidateAndConvert(Boolean useValidators)
at System.CommandLine.Parsing.ArgumentResult.GetArgumentConversionResult()
at System.CommandLine.Parsing.CommandResult.ValidateArgumentsAndAddDefaultResults(Boolean completeValidation)
at System.CommandLine.Parsing.CommandResult.Validate(Boolean isInnermostCommand)
at System.CommandLine.Parsing.ParseOperation.ValidateAndAddDefaultResults()
at System.CommandLine.Parsing.ParseOperation.Parse()
at System.CommandLine.Parsing.CommandLineParser.Parse(Command command, IReadOnlyList`1 arguments, String rawInput, ParserConfiguration configuration)
at System.CommandLine.Parsing.CommandLineParser.Parse(Command command, IReadOnlyList`1 args, ParserConfiguration configuration)
at System.CommandLine.Command.Parse(IReadOnlyList`1 args, ParserConfiguration configuration)
at Program.<Main>$(String[] args) in [HIDDEN]\TestOne.cs:line 71
at Program.<Main>(String[] args)
Rather than the expected, and more helpful:
Argument "--dats" is not two characters long
Description:
Test app for System.CommandLine
Usage:
TestOne <TestOne>... [options]
Arguments:
<TestOne> Root command arguments. One or more pairs of characters separated by space
Options:
--data <data> Option data items
-?, -h, --help Show help and usage information
--version Show version information
I noticed this when typo'ing the command line in a rather more complex project.
The problem seems to be that the validator triggers the parser, so there is nothing to check for errors reported by the parser. Without the custom parser the expected error & help is reported.
#:package [email protected]
#nullable enable
using System.CommandLine;
using System.CommandLine.Parsing;
using System.Diagnostics;
string[] ParseArguments(ArgumentResult argRes) {
List<string> res = new();
foreach (var a in argRes.Tokens) {
var s = a.Value;
if (s.Length != 2) {
argRes.AddError($"Argument \"{s}\" is not two characters long");
} else {
res.Add(s);
}
}
return res.ToArray();
}
void ValidateArguments(string[] rawValues, Action<string> reportError) {
if (rawValues.Length == 0) {
reportError("No arguments provided");
return;
}
foreach (var rawValue in rawValues) {
if (rawValue.Length != 2) {
reportError($"Argument \"{rawValue}\" is not two characters long");
} else if (rawValue[0] >= rawValue[1]) {
reportError($"In argument \"{rawValue}\" the first letter does not precede, in Unicode code point order, the second");
}
}
}
var dataOption = new Option<string[]>("--data") {
Description = "Option data items",
Arity = ArgumentArity.OneOrMore,
Required = false,
AllowMultipleArgumentsPerToken = true
};
dataOption.Validators.Add(optRes => {
var args = optRes.GetValueOrDefault<string[]>();
ValidateArguments(args, msg => optRes.AddError(msg));
});
var rootCmdArgs = new Argument<string[]>("TestOne") {
Description = "Root command arguments. One or more pairs of characters separated by space",
Arity = ArgumentArity.OneOrMore
};
rootCmdArgs.CustomParser = ParseArguments;
rootCmdArgs.Validators.Add(argRes => {
var args = argRes.GetValueOrDefault<string[]>();
ValidateArguments(args, msg => argRes.AddError(msg));
});
var rootCmd = new RootCommand("Test app for System.CommandLine") {
dataOption,
rootCmdArgs
};
rootCmd.SetAction(parseResult => {
var args = parseResult.GetValue(rootCmdArgs);
var dataArgs = parseResult.GetValue(dataOption);
Debug.Assert(args is not null);
Debug.Assert(dataArgs is not null);
Console.WriteLine("Root command");
Console.WriteLine($" args: {String.Join(", ", args.Select(a => $"\"{a}\""))}");
Console.WriteLine($" data: {String.Join(", ", dataArgs.Select(a => $"\"{a}\""))}");
});
var parse = rootCmd.Parse(args);
await parse.InvokeAsync();
- 主要语言
- C#
- 星标
- 3.7k
- 派生
- 434
- 平均合并
- 4 小时 46 分钟
- 30 天内合并 PR
- 1
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
dotnet/command-line-api 的其他 Issue
-
German localization is incomplete可能已有人在做 @b-v-d-e-v 于 8 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 65/100
dotnet/command-line-api#2852 ·
-
Incomplete French (fr) translation: RequiredOptionWasNotProvided not translated可能已有人在做 @JPBlanc 于 106 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 65/100
dotnet/command-line-api#2822 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 62/100
dotnet/command-line-api#2792 · 2 条评论 · 16 个 reaction ·
-
难度 2/5 1-3 小时 新手友好度 62/100
dotnet/command-line-api#2704 ·
-
GetCompletions should check exit code of invoked application可能已有人在做 @baradgur 于 1277 天前认领。 未关闭Area-Completions bug help wanted
难度 2/5 1-3 小时 新手友好度 72/100
dotnet/command-line-api#2137 · 1 条评论 · 3 个 reaction ·
查看 dotnet/command-line-api 的全部 Issue
相似的 Issue
-
type/automation type/tech-debt
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
has-image has-readme needs-attention new-tool repo-verified
难度 1/5 1-3 小时 新手友好度 62/100
shanselman/TinyToolTown#844 · 2 条评论 ·
维护者通常 3 天内回复
-
StoreImmediately test is flaky and fails on Linux and macOS可能已有人在做 @jcannon98188 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
-
area:frontend FE P3
难度 2/5 1-3 小时 新手友好度 74/100
klasolsson81/jobbliggaren#2067 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
grame-cncm/faust#1344 · 1 条评论 ·
维护者通常 1 天内回复