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

Mas*_*Net 3 c# command-line-parser visual-studio-2015

我试图查看使用CommandLineParser 包和 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;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我期待看到一些有关导致optionsAreValid设置为false的问题的有用信息出现在Debug > Windows > Output窗口中。但是,没有显示任何内容......我做错了什么,我找错了地方,或者在我看到这些信息之前是否需要切换其他设置?

更新 #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));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Sim*_*mon 11

我知道这是一个非常古老的问题,但对于我的特定搜索,它仍然在 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));
    });
Run Code Online (Sandbox Code Playgroud)

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

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);
    });
Run Code Online (Sandbox Code Playgroud)

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


Mas*_*Net 3

我错误地认为该HelpText属性会将信息发送到 Visual StudioOutput窗口;然而,我错了。相反,它是 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;
}
Run Code Online (Sandbox Code Playgroud)

  • 这不是很有用,因为 Error 类不会重写 ToString()。 (5认同)