我已经定义了如下选项:
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; }
}
Run Code Online (Sandbox Code Playgroud)
然后像这样解析它们:
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);
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试使用'help configure'运行我的exe,它将打印出整个帮助,在GetUsage(字符串)方法中,只有'help'命令显示在调试器中.
这是一个错误还是什么?
小智 6
这是一个错误.
我检查了一个类似于你的程序并且有相同的(错误的)行为,然后切换到命令行项目本身,有相同但我认为我发现了问题.
如果您使用项目中嵌入的命令行分析器的"源"版本,您可以按如下方式修复它(以下代码来自类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;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,如果你直接链接到命令行解析器DLL我不认为有任何解决方法.在这种情况下,只有作者可以修复它...
| 归档时间: |
|
| 查看次数: |
3230 次 |
| 最近记录: |