4

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

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.

3
  • msdn.microsoft.com/en-us/library/vstudio/acy3edy3.aspx Commented Aug 2, 2013 at 1:22
  • What's the problem with this code? Do you know how to read a string from Console? That's the only thing you are missing, really. That, and a loop around the read and the switch. Commented Aug 2, 2013 at 1:23
  • The code is okay but I don't know how to loop the read... New to c# Commented Aug 2, 2013 at 1:25

3 Answers 3

13

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 switch to keep things running. Here's a quick sample based on what you wrote above:

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;
           }
        }
     }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

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.

1 Comment

This is not what I am looking for... I want user to type in Console Application a command like /help and Console writes other commands...
-1

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

1 Comment

I know that, but I want something simple like using switch

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.