HelpVerbOption 不工作 - 命令行解析器 C#
HelpVerbOption is not working - Command Line Parser C#
我有一个 class:
class Options
{
// Remainder omitted (verb1, verb2, verb3)
[HelpVerbOption]
public string GetUsage(string verb)
{
return HelpText.AutoBuild(this, verb);
}
}
docs 说:
[...] The parser will pass null to master class GetUsage(string) also if
the user requested the help index with:
$ git help
or the verb command if the user requested explicitly
instructions on how to use a particular verb:
$ git help commit
[...]
然后,我输入了 MyApp.exe help verb1
,但我只能看到基本帮助(看起来我输入了错误的动词,或 help
动词,或其他)。相反,我希望它显示与指定动词相关的帮助消息。为什么它不能正常工作?
对我来说,它可以使用上述方法工作,但前提是我调用我的应用程序时没有使用 --help
-选项(例如 MyApp batch
)。当我使用 MyApp --help batch
时,行为如您所述。
然而,我们似乎无法对 help
选项进行同样的工作。
编辑:我设法通过使用以下内容修改 Commandline.Parser.cs
的代码来实现此功能:
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
var verb = args.FirstOrDefault(); // <----- change this to args[1];
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;
}
问题出现在
行
var verb = args.FirstOrDefault();
因为第一个参数 (args[0]
) 被解释为动词或更好的 action(如文档中所述)verb
将始终在这里help
。所以我们用 args[1]
替换它,其中包含 actual 动词,例如 commit
.
EDIT2:为了使 --help
工作,我们还应该 trim 来自 -
-character
的第一个 arg (args[0]
)
if (string.Compare(args[0].Trim('-'), helpInfo.Right.LongName, GetStringComparison(_settings)) == 0)
我有一个 class:
class Options
{
// Remainder omitted (verb1, verb2, verb3)
[HelpVerbOption]
public string GetUsage(string verb)
{
return HelpText.AutoBuild(this, verb);
}
}
docs 说:
[...] The parser will pass null to master class GetUsage(string) also if the user requested the help index with:
$ git help
or the verb command if the user requested explicitly instructions on how to use a particular verb:
$ git help commit
[...]
然后,我输入了 MyApp.exe help verb1
,但我只能看到基本帮助(看起来我输入了错误的动词,或 help
动词,或其他)。相反,我希望它显示与指定动词相关的帮助消息。为什么它不能正常工作?
对我来说,它可以使用上述方法工作,但前提是我调用我的应用程序时没有使用 --help
-选项(例如 MyApp batch
)。当我使用 MyApp --help batch
时,行为如您所述。
然而,我们似乎无法对 help
选项进行同样的工作。
编辑:我设法通过使用以下内容修改 Commandline.Parser.cs
的代码来实现此功能:
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
var verb = args.FirstOrDefault(); // <----- change this to args[1];
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;
}
问题出现在
行var verb = args.FirstOrDefault();
因为第一个参数 (args[0]
) 被解释为动词或更好的 action(如文档中所述)verb
将始终在这里help
。所以我们用 args[1]
替换它,其中包含 actual 动词,例如 commit
.
EDIT2:为了使 --help
工作,我们还应该 trim 来自 -
-character
args[0]
)
if (string.Compare(args[0].Trim('-'), helpInfo.Right.LongName, GetStringComparison(_settings)) == 0)