C# 控制台应用程序中的用户输入命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18007246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:01:50  来源:igfitidea点击:

User input commands in Console application

c#console-application

提问by TheNeosrb

I would like my console application to have commands like user types /helpand console writes help. I would like it to use switchlike:

我希望我的控制台应用程序具有诸如用户类型/help和控制台写入帮助之类的命令。我希望它像这样使用switch

switch (command)
{
    case "/help":
        Console.WriteLine("This should be help.");
        break;

    case "/version":
        Console.WriteLine("This should be version.");
        break;

    default:
        Console.WriteLine("Unknown Command " + command);
        break;
}

How can I achieve this? Thanks in advance.

我怎样才能做到这一点?提前致谢。

采纳答案by Ken White

Based on your comment to errata's answer, it appears you want to keep looping until you're told not to do so, instead of getting input from the command line at startup. If that's the case, you need to loop outside the switchto keep things running. Here's a quick sample based on what you wrote above:

根据您对errata's answer 的评论,您似乎想继续循环直到被告知不要这样做,而不是在启动时从命令行获取输入。如果是这种情况,您需要在 外部循环switch以保持运行。这是基于您上面写的内容的快速示例:

namespace ConsoleApplicationCSharp1
{
  class Program
  {
    static void Main(string[] args)
    {
        string command;
        bool quitNow = false;
        while(!quitNow)
        {
           command = Console.ReadLine();
           switch (command)
           {
              case "/help":
                Console.WriteLine("This should be help.");
                 break;

               case "/version":
                 Console.WriteLine("This should be version.");
                 break;

                case "/quit":
                  quitNow = true;
                  break;

                default:
                  Console.WriteLine("Unknown Command " + command);
                  break;
           }
        }
     }
  }
}

回答by errata

Something along these lines might work:

沿着这些路线的东西可能会起作用:

// cmdline1.cs
// arguments: A B C
using System;
public class CommandLine
{
   public static void Main(string[] args)
   {
       // The Length property is used to obtain the length of the array. 
       // Notice that Length is a read-only property:
       Console.WriteLine("Number of command line parameters = {0}",
          args.Length);
       for(int i = 0; i < args.Length; i++)
       {
           Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
       }
   }
}

run the command: cmdline1 A B C

运行命令:cmdline1 ABC

Output:

输出:

 Number of command line parameters = 3
    Arg[0] = [A]
    Arg[1] = [B]
    Arg[2] = [C]

I don't do c# much(any) anymore but hope this helps.

我不再做 c# 很多(任何),但希望这会有所帮助。

回答by Tarik

There exist .open source projects like http://www.codeproject.com/Articles/63374/C-NET-Command-Line-Argument-Parser-Reloadedthat take care of this. Why reinventing the wheel?

存在像http://www.codeproject.com/Articles/63374/C-NET-Command-Line-Argument-Parser-Reloaded这样的.open source 项目来解决这个问题。为什么要重新发明轮子?