如何查看 Visual Studio 2015 中的 CommandLineParser 解析错误?
How to view CommandLineParser parsing errors in Visual Studio 2015?
我正在尝试查看使用 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;
}
}
}
}
我期待看到有关导致 optionsAreValid
设置为 false 的问题的一些有用信息出现在 Debug > Windows > Output
window.但是,没有显示任何内容...我是不是做错了什么,是不是找错地方了,还是需要切换其他设置才能看到此信息?
更新 #1
这里是 class 在被(成功)解析后模拟命令行选项:
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));
}
}
}
我误以为 HelpText
属性 会向 Visual Studio Output
window 发送信息;然而,我错了。相反,它是 CommandLineParser 如何获取将信息写入控制台 window 所需的信息,当 运行 控制台应用程序项目 (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;
}
我知道这是一个非常古老的问题,但在我的特定搜索中,它仍然在 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);
});
我希望这对以后的人有所帮助!
我正在尝试查看使用 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;
}
}
}
}
我期待看到有关导致 optionsAreValid
设置为 false 的问题的一些有用信息出现在 Debug > Windows > Output
window.但是,没有显示任何内容...我是不是做错了什么,是不是找错地方了,还是需要切换其他设置才能看到此信息?
更新 #1
这里是 class 在被(成功)解析后模拟命令行选项:
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));
}
}
}
我误以为 HelpText
属性 会向 Visual Studio Output
window 发送信息;然而,我错了。相反,它是 CommandLineParser 如何获取将信息写入控制台 window 所需的信息,当 运行 控制台应用程序项目 (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;
}
我知道这是一个非常古老的问题,但在我的特定搜索中,它仍然在 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);
});
我希望这对以后的人有所帮助!