如何在 WinForms 项目中使用 CommandLineParser?如何构建自定义帮助消息框?
How to use CommandLineParser in a WinForms Project? How to build a custom Help MessageBox?
我正在尝试在 WinForms 应用程序中使用版本 2.5.0 中的 CommandLineParser
库。
除了帮助屏幕(在那种情况下是 MessageBox)外,它工作得很好。
我已经发现我需要创建自己的解析器并至少将 HelpWriter
属性 设置为 null
以创建自定义帮助屏幕。
但是当使用 --help
参数调用应用程序时,我的 "Error handler" 只得到一个错误实例,其中 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
控制台输出,但我这里没有控制台应用程序。
好的,我现在想出了一个办法。这似乎不是最好的方法,但它确实有效。
我创建了一个 StringBuilder
实例并将其放入 StringWriter
实例中
private static StringBuilder helpTextBuilder = new StringBuilder();
private static StringWriter helpTextWriter = new StringWriter(helpTextBuilder);
然后我用(至少这个)选项创建一个新的解析器:
var parser = new Parser(config =>
{
config.HelpWriter = helpTextWriter;
});
在出现错误的情况下,我现在可以使用写入 helpTextBuilder
的内容来显示消息框。
private static int RunParsingFailedAndReturnExitCode(IEnumerable<Error> errs)
{
MessageBox.Show(helpTextBuilder.ToString());
return 1;
}
所以这对我有用。
我正在尝试在 WinForms 应用程序中使用版本 2.5.0 中的 CommandLineParser
库。
除了帮助屏幕(在那种情况下是 MessageBox)外,它工作得很好。
我已经发现我需要创建自己的解析器并至少将 HelpWriter
属性 设置为 null
以创建自定义帮助屏幕。
但是当使用 --help
参数调用应用程序时,我的 "Error handler" 只得到一个错误实例,其中 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
控制台输出,但我这里没有控制台应用程序。
好的,我现在想出了一个办法。这似乎不是最好的方法,但它确实有效。
我创建了一个 StringBuilder
实例并将其放入 StringWriter
实例中
private static StringBuilder helpTextBuilder = new StringBuilder();
private static StringWriter helpTextWriter = new StringWriter(helpTextBuilder);
然后我用(至少这个)选项创建一个新的解析器:
var parser = new Parser(config =>
{
config.HelpWriter = helpTextWriter;
});
在出现错误的情况下,我现在可以使用写入 helpTextBuilder
的内容来显示消息框。
private static int RunParsingFailedAndReturnExitCode(IEnumerable<Error> errs)
{
MessageBox.Show(helpTextBuilder.ToString());
return 1;
}
所以这对我有用。