Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Visual Studio 2015中查看CommandLineParser解析错误?_C#_Visual Studio 2015_Command Line Parser - Fatal编程技术网

C# 如何在Visual Studio 2015中查看CommandLineParser解析错误?

C# 如何在Visual Studio 2015中查看CommandLineParser解析错误?,c#,visual-studio-2015,command-line-parser,C#,Visual Studio 2015,Command Line Parser,我试图查看使用和Visual Studio Professional 2015(更新3)解析命令行参数时出现的错误。以下是我正在使用的代码: using System; using System.IO; namespace SampleParser { class Program { static void Main(string[] args) { // Set the CommandLineParser configu

我试图查看使用和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

以下是在(成功)解析命令行选项后对其建模的类:


我错误地认为
HelpText
属性会将信息发送到VisualStudio
Output
窗口;然而,我错了。相反,它是CommandLineParser如何获取将信息写入控制台窗口所需的信息的,该窗口在运行控制台应用程序项目(tx)时弹出

下面是一些样板代码(针对v2.1.1-beta NuGet包),虽然仍然没有提供我想要的关于解析器错误的更多信息,但以一种更易于我以编程方式处理的方式公开了它们

// Set the CommandLineParser configuration options.
var commandLineParser = new CommandLine.Parser(x =>
{
    x.HelpWriter = null;
    x.IgnoreUnknownArguments = false;
    //x.CaseSensitive = false;
});

// Parse the command-line arguments.
CommandLineOptions options;
var errors = new List<CommandLine.Error>();

var parserResults = commandLineParser.ParseArguments<CommandLineOptions>(args)
    .WithNotParsed(x => errors = x.ToList())
    .WithParsed(x => options = x)
;

if (errors.Any())
{
    errors.ForEach(x => Console.WriteLine(x.ToString()));
    Console.ReadLine();
    return;
}
//设置CommandLineParser配置选项。
var commandLineParser=newcommandline.Parser(x=>
{
x、 HelpWriter=null;
x、 IgnoreUnknownArguments=假;
//x、 区分大小写=错误;
});
//解析命令行参数。
命令行选项;
var errors=新列表();
var parserResults=commandLineParser.parserarguments(args)
.WithNotParsed(x=>errors=x.ToList())
.WithParsed(x=>options=x)
;
if(errors.Any())
{
errors.ForEach(x=>Console.WriteLine(x.ToString());
Console.ReadLine();
返回;
}

我知道这是一个非常古老的问题,但它仍然是我在谷歌搜索中排名第一的搜索结果。由于没有令人满意的解决方案,我将添加一个:

要仅打印错误,可以使用库本身提供的
SentenceBuilder

var result=Parser.Default.ParseArguments(args);
结果
.WithParsed(opt=>…)
.WithNotParsed(错误=>{
var sentenceBuilder=sentenceBuilder.Create();
foreach(错误中的var错误)
Console.WriteLine(sentenceBuilder.FormatError(error));
});
要打印完整的帮助信息(包括遇到的错误),请使用以下命令:

var result=Parser.Default.ParseArguments(args);
结果
.WithParsed(opt=>…)
.WithNotParsed(错误=>{
var helpText=helpText.AutoBuild(结果,
h=>HelpText.DefaultParsingErrorsHandler(结果,h),
e=>e);
Console.WriteLine(帮助文本);
});

我希望这对将来的人有帮助

您是否在
CommandLineOptions
类中定义了规则/选项?是的--我刚刚更新了OP以包含我正在使用的规则/选项类。通常,您必须显式调用调试或跟踪类方法才能在visual studio的输出窗口中进行输出。要使您的
选项sarevalid
为true,您应该使用“cmd--preview”或不带参数的“cmd”调用cmd。您如何建议我显式调用Debug或Trace类,以便它们可以“查看”CommandLineParser识别的验证错误?我本以为
命令行.Parser
类型的
HelpWriter
将是实现这一点的关键,但我不确定如何实现您的建议。“他们”是应用程序的最终用户吗?您认为
HelpWriter
是关键,这是正确的。如果解析器无法解析参数,它将显示帮助文本-这与使用“cmd--help”运行cmd时获得的信息相同。我提到了Debug/Trace,因为您要求在visual studio输出窗口中查看信息。这不是很有用,因为Error类不重写ToString()。'h'变量(在第二个代码段中)来自哪里?它似乎应该是
h=>HelpText.DefaultParsingErrorsHandler(result,h)
@MartinDráb是的,似乎在格式化时丢失了这个。我已经调整了答案。
// Set the CommandLineParser configuration options.
var commandLineParser = new CommandLine.Parser(x =>
{
    x.HelpWriter = null;
    x.IgnoreUnknownArguments = false;
    //x.CaseSensitive = false;
});

// Parse the command-line arguments.
CommandLineOptions options;
var errors = new List<CommandLine.Error>();

var parserResults = commandLineParser.ParseArguments<CommandLineOptions>(args)
    .WithNotParsed(x => errors = x.ToList())
    .WithParsed(x => options = x)
;

if (errors.Any())
{
    errors.ForEach(x => Console.WriteLine(x.ToString()));
    Console.ReadLine();
    return;
}