3

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

我期待在窗口中看到一些有关导致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));
        }
    }
}
4

2 回答 2

13

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

要打印错误,您可以使用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);
    });

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

于 2020-06-02T08:54:47.593 回答
2

我错误地认为该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;
}
于 2017-02-06T18:24:48.050 回答