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

Posted

技术标签:

【中文标题】如何在 Visual Studio 2015 中查看 CommandLineParser 解析错误?【英文标题】:How to view CommandLineParser parsing errors in Visual Studio 2015? 【发布时间】: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。 你如何建议我明确调用 DebugTrace 类,以便他们可以“看到”CommandLineParser 正在识别的验证错误?我原以为CommandLine.Parser 类型的HelpWriter 将是实现这一目标的关键,但我不确定如何完成您的建议。 “他们”是应用程序的最终用户吗?你说得对,HelpWriter 是关键。如果解析器无法解析参数,它将显示帮助文本 - 这与使用“cmd --help”运行 cmd 时获得的信息相同。我提到了 Debug/Trace,因为您询问在 Visual Studio 输出窗口中查看信息。 【参考方案1】:

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

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

using System;
using CommandLine;
using CommandLine.Text;

...

var result = Parser.Default.ParseArguments<Options>(args);
result
    .WithParsed(opt => ...)
    .WithNotParsed(errors => 
        var sentenceBuilder = SentenceBuilder.Create();
        foreach (var error in errors)
            Console.WriteLine(sentenceBuilder.FormatError(error));
    );

要打印包括遇到的错误在内的完整帮助信息,请使用以下命令:

using System;
using CommandLine;
using CommandLine.Text;

...

var result = Parser.Default.ParseArguments<Options>(args);
result
    .WithParsed(opt => ...)
    .WithNotParsed(errors => 
        var helpText = HelpText.AutoBuild(result,
                                          h => HelpText.DefaultParsingErrorsHandler(result, h), 
                                          e => e);
        Console.WriteLine(helpText);
    );

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

【讨论】:

【参考方案2】:

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

这里是一些样板代码(针对 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;

【讨论】:

这不是很有用,因为Error类没有覆盖ToString()。

以上是关于如何在 Visual Studio 2015 中查看 CommandLineParser 解析错误?的主要内容,如果未能解决你的问题,请参考以下文章