C# 如何将可选参数传递给winform命令行

C# 如何将可选参数传递给winform命令行,c#,command-line-arguments,C#,Command Line Arguments,因此,我以前编写过一些工具,将命令行参数传递给winforms c#工具。如何传递可选的特定参数 Static void test(string name="bill", int age=5, string location = "home") { Console.writeline (name) Console.writeline (age) } 简单来说,我希望用户能够通过年龄或姓名或两者调用此函数命令行。 例如 测试名称:“乔伊” 测试地点:“卧床”年龄:5

因此,我以前编写过一些工具,将命令行参数传递给winforms c#工具。如何传递可选的特定参数

 Static void test(string name="bill", int age=5, string location = "home")
 {
      Console.writeline (name)
      Console.writeline (age)
 }
简单来说,我希望用户能够通过年龄或姓名或两者调用此函数命令行。 例如

测试名称:“乔伊” 测试地点:“卧床”年龄:5岁


也许有人建议我写命令行参数的方式,我可以通过传递可选参数的方式解析命令行参数。欢迎提出建议

一个建议是使用命令行解析库,如或,这两个库都可以通过NuGet获得。

据我所知和建议,您可以使用(使用NuGet获得)。 我在项目中使用它是为了在不同的状态下启动应用程序。 首先,您将定义所有接受的参数(可以将其中一些参数设置为可选参数,更多关于库文档的信息)

接下来,在
Program.cs
类中,在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 ...
}

希望这能有所帮助。

你的问题很模糊。是否要从命令行接收这些参数?或者希望其他用户调用此方法?为什么不为方法添加重载?我将查找重载。我从未听说过或使用过它们。是的,我想通过命令行传递它们来运行函数。谢谢你的帮助。这有助于我取得足够的进展,使一些工作得以开展,并进行更多的研究
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 ...
}