C# 如何在WinForms项目中使用CommandLineParser?如何构建自定义帮助MessageBox?
我正试图在WinForms应用程序中使用2.5.0版中的C# 如何在WinForms项目中使用CommandLineParser?如何构建自定义帮助MessageBox?,c#,winforms,command-line-arguments,C#,Winforms,Command Line Arguments,我正试图在WinForms应用程序中使用2.5.0版中的CommandLineParser库 除了帮助屏幕(在这种情况下是MessageBox)之外,它工作得非常好 我已经发现我需要创建一个自己的解析器,并至少将HelpWriter属性设置为null以创建自定义帮助屏幕 但是,当使用--help参数调用应用程序时,我的“错误处理程序”只需获取一个错误实例,该实例的标记类型为CommandLine.ErrorType,值为HelpRequestedError 现在如何构建自定义帮助屏幕 本网站建
CommandLineParser
库
除了帮助屏幕(在这种情况下是MessageBox)之外,它工作得非常好
我已经发现我需要创建一个自己的解析器,并至少将HelpWriter
属性设置为null
以创建自定义帮助屏幕
但是,当使用--help
参数调用应用程序时,我的“错误处理程序”只需获取一个错误实例,该实例的标记类型为CommandLine.ErrorType
,值为HelpRequestedError
现在如何构建自定义帮助屏幕
本网站建议使用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;
}
}
}
名称空间myWorkspace
{
使用制度;
使用System.Collections.Generic;
利用制度全球化;
使用System.Windows.Forms;
使用命令行;
使用DevExpress.XtraEditors;
使用选项;
内部静态类程序
{
///
///应用程序的主要入口点。
///
[状态线程]
内部静态int Main(字符串[]args)
{
AppDomain.CurrentDomain.SetupInformation.PrivateBinPath=“bin”;
WindowsFormsSettings.EnableFormSkins();
WindowsFormsSettings.EnableMdiFormSkins();
WindowsFormsSettings.ForceDirectXPaint();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var解析器=新解析器(配置=>
{
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;
});
返回Parser.Default.ParseArguments(args)
.MapResult(
运行并返回ExitCode,
RunParsingFailedAndReturnExitCode);
}
专用静态int runandreturnexitcode(RunOptions选项)
{
尝试
{
运行(新的MainForm());
}
抓住
{
返回-1;
}
返回0;
}
私有静态int RunParsingFailedAndReturnExitCode(IEnumerable errs)
{
foreach(errs中的var err)
{
var-locErr=err;
}
返回1;
}
}
}
在线var-locErr=err代码>我不知道如何才能获得可以在MessageBox等中显示的帮助屏幕消息
CommandLineParser
似乎为help
或--help
支持开箱即用的控制台输出,但我这里没有控制台应用程序。好的,我现在想出了一种方法。这似乎不是最好的方法,但它确实有效
我创建了一个StringBuilder
实例,并将其放入StringWriter
实例中
private static StringBuilder helpTextBuilder = new StringBuilder();
private static StringWriter helpTextWriter = new StringWriter(helpTextBuilder);
然后,我使用(至少此)选项创建一个新的解析器:
如果出现错误,我现在可以使用写入helpTextBuilder
的内容显示消息框
private static int RunParsingFailedAndReturnExitCode(IEnumerable<Error> errs)
{
MessageBox.Show(helpTextBuilder.ToString());
return 1;
}
private static int RunParsingFailedAndReturnExitCode(IEnumerable errs)
{
Show(helpTextBuilder.ToString());
返回1;
}
所以这对我来说很有用。请发布你没有的代码working@amura.cxg我相应地编辑了我的问题
private static int RunParsingFailedAndReturnExitCode(IEnumerable<Error> errs)
{
MessageBox.Show(helpTextBuilder.ToString());
return 1;
}