【发布时间】:2017-06-20 00:20:19
【问题描述】:
我正在尝试查看使用 CommandLineParser package 和 Visual Studio Professional 2015(更新 3)解析命令行参数时发生的错误。这是我正在使用的代码:
using System;
using System.IO;
namespace SampleParser
{
class Program
{
static void Main(string[] args)
{
// Set the CommandLineParser configuration options.
var commandLineParser = new CommandLine.Parser(x =>
{
x.MutuallyExclusive = true;
x.HelpWriter = Console.Error;
x.IgnoreUnknownArguments = false;
});
// Parse the command-line arguments.
var options = new CommandLineOptions();
var optionsAreValid = commandLineParser.ParseArguments(args, options);
if (!optionsAreValid)
{
return;
}
}
}
}
我期待在Debug > Windows > Output 窗口中看到一些有关导致optionsAreValid 设置为false 的问题的有用信息。但是,什么都没有显示...是我做错了什么,是我看错了地方,还是需要切换其他设置才能看到此信息?
更新 #1
这是在(成功)解析后对命令行选项进行建模的类:
namespace SampleParser
{
class CommandLineOptions
{
[Option(HelpText = @"When set to ""true"", running the application will not make any changes.", Required = false)]
public bool Preview { get; set; }
[HelpOption]
public string GetUsage()
{
return HelpText.AutoBuild(this, current => HelpText.DefaultParsingErrorsHandler(this, current));
}
}
}
【问题讨论】:
-
您是否在
CommandLineOptions类中定义了规则/选项? -
是的——我刚刚更新了我的 OP 以包含我正在使用的规则/选项类。
-
一般你必须显式调用 Debug 或 Trace 类方法才能在 Visual Studio 的输出窗口中输出。要使您的
optionsAreValid为真,您应该使用 'cmd --preview' 或仅使用不带参数的 'cmd' 调用 cmd。 -
你如何建议我明确调用 Debug 或 Trace 类,以便他们可以“看到”CommandLineParser 正在识别的验证错误?我原以为
CommandLine.Parser类型的HelpWriter将是实现这一目标的关键,但我不确定如何完成您的建议。 -
“他们”是应用程序的最终用户吗?你说得对,
HelpWriter是关键。如果解析器无法解析参数,它将显示帮助文本 - 这与使用“cmd --help”运行 cmd 时获得的信息相同。我提到了 Debug/Trace,因为您询问在 Visual Studio 输出窗口中查看信息。
标签: c# visual-studio-2015 command-line-parser