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

标签 c# command

我定义的选项如下:

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/

相关文章:

C# - 将 HTML 无序列表转换为数组

c# - 如何在 C# 中从 Google map 获取高程点?

c# - 实体模型会在其数据更改时触发事件吗?

c# - 在单个 BackgroundWorker 上运行多个 DoWork 函数是否安全?

python - linux 命令产生 Python OSError : [Errno 2] No such file or directory

c# - 统一错误 : Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption

shell - tar:存档中出现意外的 EOF

regex - 使用 bash 脚本逐行读取文件

callback - 使用 2 个命令运行 cron,等待第一个完成以运行第二个

batch-file - 批处理文件(ForFiles 逻辑的多个条件命令)?