【发布时间】:2019-11-15 11:41:41
【问题描述】:
我正在尝试在 WinForms 应用程序中使用 2.5.0 版中的 CommandLineParser 库。
除了帮助屏幕(在这种情况下为 MessageBox)外,它的效果很好。
我已经发现我需要创建一个自己的解析器并将至少HelpWriter 属性设置为null 以创建自定义帮助屏幕。
但是当应用程序使用--help 参数调用时,我的“错误处理程序”只会得到一个错误实例,其类型为Tag,值为CommandLine.ErrorType,值为HelpRequestedError
现在如何构建自定义帮助屏幕?
https://github.com/commandlineparser/commandline/wiki/Generating-Help-and-Usage-information
该站点建议使用 CommandLine.Text 命名空间中的类型,但如何使用?如何做到这一点的例子为零。
这里有人做过这样的事吗?
我有以下代码:
namespace myWorkspace
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms;
using CommandLine;
using DevExpress.XtraEditors;
using Options;
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
internal static int Main(string[] args)
{
AppDomain.CurrentDomain.SetupInformation.PrivateBinPath = "bin";
WindowsFormsSettings.EnableFormSkins();
WindowsFormsSettings.EnableMdiFormSkins();
WindowsFormsSettings.ForceDirectXPaint();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var parser = new Parser(config =>
{
config.AutoHelp = true;
config.AutoVersion = true;
config.CaseInsensitiveEnumValues = false;
config.CaseSensitive = false;
config.EnableDashDash = true;
config.HelpWriter = null;
config.IgnoreUnknownArguments = true;
//config.MaximumDisplayWidth
config.ParsingCulture = CultureInfo.InvariantCulture;
});
return Parser.Default.ParseArguments<RunOptions>(args)
.MapResult(
RunRunAndReturnExitCode,
RunParsingFailedAndReturnExitCode);
}
private static int RunRunAndReturnExitCode(RunOptions opts)
{
try
{
Application.Run(new MainForm());
}
catch
{
return -1;
}
return 0;
}
private static int RunParsingFailedAndReturnExitCode(IEnumerable<Error> errs)
{
foreach (var err in errs)
{
var locErr = err;
}
return 1;
}
}
}
在var locErr = err; 线上,我不知道该怎么做才能获得可以在 MessageBox 等中显示的帮助屏幕消息。
CommandLineParser 似乎支持 help 或 --help 的开箱即用控制台输出,但我这里没有控制台应用程序。
【问题讨论】:
-
请发布您的无效代码
-
@amura.cxg 我相应地编辑了我的问题
标签: c# winforms command-line-arguments