如何将可选参数传递给 winform 命令行
How to pass optional arguments to winform commandline
所以我过去编写了一些工具,我将命令行参数传递给 winforms c# 工具。如何传递可选的特定参数。
Static void test(string name="bill", int age=5, string location = "home")
{
Console.writeline (name)
Console.writeline (age)
}
简单来说,我希望用户能够通过年龄或姓名或两者来调用此函数命令行。
例如...
测试名称:"JOEY"
测试地点:"bed"age:5
也许有人推荐我编写命令行参数的方式,我以可以传递可选参数的方式进行解析。欢迎提出建议。
一个建议是使用命令行解析库,例如 Command Line Parser Library or the Fluent Command Line Parser,两者都可以通过 NuGet 获得。
据我了解并 devdigital had suggested, you can use the Command Line Parser Library(可使用 NuGet)。
我在我的项目中使用它以在不同状态下启动应用程序。
首先,您将定义所有接受的参数(可以将其中一些设置为可选参数,有关库文档的更多信息)
public class CommandLineArgs
{
[Option('t', "type", Required = false, HelpText = "Type of the application [safedispatch, safenet]")]
public string AppType { get; set; }
[Option('c', null, HelpText = "Enable the console for this application")]
public bool Console { get; set; }
[Option('l', null, HelpText = "Enable the logs for this application")]
public bool Log { get; set; }
[Option('h', null, HelpText = "Help for this command line")]
public bool Help { get; set; }
[HelpOption]
public string GetUsage()
{
// this without using CommandLine.Text
// or using HelpText.AutoBuild
var usage = HelpText.AutoBuild(this);
return usage.ToString();
}
}
接下来,在您 Program.cs
class 的 main 函数中,您将创建一个 CommandLineArgs
对象并解析接收到的参数。最后,您将根据传递给您的参数做出决定。
static void Main(string[] args)
{
var cmdArgs = new CommandLineArgs();
if (args.Length > 0 && CommandLine.Parser.Default.ParseArguments(args, cmdArgs))
{
// display the help
if (cmdArgs.Help)
{
Utils.WriteLine(cmdArgs.GetUsage());
Console.ReadKey();
}
// display the console
if (!cmdArgs.Console)
{
// hide the console window
setConsoleWindowVisibility(false, Console.Title);
}
// verify other console parameters and run your test function
}
else if (args.Length == 0)
{
// no command line args specified
}
// other lines ...
}
希望对您有所帮助。
所以我过去编写了一些工具,我将命令行参数传递给 winforms c# 工具。如何传递可选的特定参数。
Static void test(string name="bill", int age=5, string location = "home")
{
Console.writeline (name)
Console.writeline (age)
}
简单来说,我希望用户能够通过年龄或姓名或两者来调用此函数命令行。 例如...
测试名称:"JOEY" 测试地点:"bed"age:5
也许有人推荐我编写命令行参数的方式,我以可以传递可选参数的方式进行解析。欢迎提出建议。
一个建议是使用命令行解析库,例如 Command Line Parser Library or the Fluent Command Line Parser,两者都可以通过 NuGet 获得。
据我了解并 devdigital had suggested, you can use the Command Line Parser Library(可使用 NuGet)。 我在我的项目中使用它以在不同状态下启动应用程序。 首先,您将定义所有接受的参数(可以将其中一些设置为可选参数,有关库文档的更多信息)
public class CommandLineArgs
{
[Option('t', "type", Required = false, HelpText = "Type of the application [safedispatch, safenet]")]
public string AppType { get; set; }
[Option('c', null, HelpText = "Enable the console for this application")]
public bool Console { get; set; }
[Option('l', null, HelpText = "Enable the logs for this application")]
public bool Log { get; set; }
[Option('h', null, HelpText = "Help for this command line")]
public bool Help { get; set; }
[HelpOption]
public string GetUsage()
{
// this without using CommandLine.Text
// or using HelpText.AutoBuild
var usage = HelpText.AutoBuild(this);
return usage.ToString();
}
}
接下来,在您 Program.cs
class 的 main 函数中,您将创建一个 CommandLineArgs
对象并解析接收到的参数。最后,您将根据传递给您的参数做出决定。
static void Main(string[] args)
{
var cmdArgs = new CommandLineArgs();
if (args.Length > 0 && CommandLine.Parser.Default.ParseArguments(args, cmdArgs))
{
// display the help
if (cmdArgs.Help)
{
Utils.WriteLine(cmdArgs.GetUsage());
Console.ReadKey();
}
// display the console
if (!cmdArgs.Console)
{
// hide the console window
setConsoleWindowVisibility(false, Console.Title);
}
// verify other console parameters and run your test function
}
else if (args.Length == 0)
{
// no command line args specified
}
// other lines ...
}
希望对您有所帮助。