gpt4 book ai didi

c# - 命令行解析器动词帮助不起作用?

转载 作者:太空狗 更新时间:2023-10-30 00:30:01 24 4
gpt4 key购买 nike

我定义的选项如下:

public class ArgumentsHeader
{
[VerbOption("configure", HelpText = "Sets configuration on server.")]
public ServerConfigurationArguments ServerConfigurationArguments { get; set; }

[HelpVerbOption]
public string GetUsage(string s)
{
return HelpText.AutoBuild(this, s);//always just 'help' or null showing up here.
}
}
public class ServerConfigurationArguments : ArgumentsBase
{
[Option('f', "filename", HelpText = "Path to JSON configuration file", DefaultValue = "config.json", Required = true)]
public string PathToConfig { get; set; }
}

然后像这样解析它们:

        string invokedVerb = null;
object invokedVerbInstance = null;


var parser = new Parser(x =>
{
x.MutuallyExclusive = true;
});
var options = new ArgumentsHeader();
if (!parser.ParseArguments(args, options,
(verb, subOptions) =>
{
// if parsing succeeds the verb name and correct instance
// will be passed to onVerbCommand delegate (string,object)
invokedVerb = verb;
invokedVerbInstance = subOptions;
}))
{
Exit(ExitStatus.InvalidArguments);
}

但是如果我尝试使用“help configure”运行我的 exe,它只会打印出整个帮助,并且在 GetUsage(string) 方法中,只有“help”命令出现在调试器中。

是bug还是什么?

最佳答案

这是一个错误。

我检查了一个与您的程序类似的程序并且有相同的(错误)行为,然后切换到命令行项目本身,有相同的但我想我发现了问题。

如果您使用的是嵌入在项目中的“源”版本的命令行解析器,您可以按如下方式修复它(下面的代码来自类 commandLine.Parser):

private bool TryParseHelpVerb(string[] args, object options, Pair<MethodInfo, HelpVerbOptionAttribute> helpInfo, OptionMap optionMap)
{
var helpWriter = _settings.HelpWriter;
if (helpInfo != null && helpWriter != null)
{
if (string.Compare(args[0], helpInfo.Right.LongName, GetStringComparison(_settings)) == 0)
{
// User explicitly requested help
// +++ FIX
// var verb = args.FirstOrDefault(); // This looks wrong as the first element is always the help command itself
var verb = args.Length == 1 ? null : args[1]; // Skip the help command and use next argument as verb
// --- FIX
if (verb != null)
{
var verbOption = optionMap[verb];
if (verbOption != null)
{
if (verbOption.GetValue(options) == null)
{
// We need to create an instance also to render help
verbOption.CreateInstance(options);
}
}
}

DisplayHelpVerbText(options, helpInfo, verb);
return true;
}
}

return false;
}

不幸的是,如果您直接链接到命令行解析器 DLL,我认为没有任何解决方法。在这种情况下,只有作者可以修复它......

关于c# - 命令行解析器动词帮助不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43092065/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com