我正在使用VisualStudioforMacOS2022v17.4和System.CommandLine(2.0.0-beta4.22272.1)编写一个C#控制台应用程序项目(.NET 7.0).
我正在try 向我的一个命令添加自定义验证器,并在项目的GitHub站点上找到了以下代码片段:
this.AddValidator(commandResult =>
{
if (commandResult.Children.Contains("one") &&
commandResult.Children.Contains("two"))
{
commandResult.ErrorMessage = "Options '--one' and '--two' cannot be used together.";
}
});
但是,由于错误CS1929,它不会在我的命令中编译:‘IReadOnlyList’不包含‘Containers’的定义,并且最佳扩展方法重载‘M一带一路’(ReadOnlySpan,字符串)需要类型为‘ReadOnlySpan’的接收器(CS1929).
我在各种System.CommandLine命名空间中寻找了合适的扩展方法,但我什么也没看到.(是的,我包括了System.Linq命名空间.)
有谁能建议一下哪里出了问题吗?
编辑:我borrow 的原始代码是这样的:
command.AddValidator(commandResult =>
{
if (commandResult.Children.Contains("one") &&
commandResult.Children.Contains("two"))
{
return "Options '--one' and '--two' cannot be used together.";
}
return null;
});
请注意,此委托返回值,而更新后的委托不返回值,而是设置ErrorMessage.这是System.CommandLine中的一个突破性更改,这里的问题可能是由于另一个突破性更 retrofit 成的.System.CommandLine项目does指出,该项目处于不断变化的状态,并且可能会发生变化.