0

I have researched on Youtube this but nothing has come up, also on Google. Could anyone please show how use an enum for a Console Command Library, or a better way to process commands. Greatly Appreciated.

  • 1
    See [Command Pattern](http://en.wikipedia.org/wiki/Command_pattern), forget about enum. – Sriram Sakthivel May 12 '15 at 07:55
  • So you cannot have the console search for the command itself? Must it be in a if,else if,else if tree? –  May 12 '15 at 08:01
  • I'm not sure I understand what you mean. – Sriram Sakthivel May 12 '15 at 08:02
  • The program cannot parse the input, then lookup to see if it know what the command is, then use the function according to it? Without the use of a long if statement –  May 12 '15 at 08:06
  • No. You need a if-else ladder, or switch statement or some kind of dictionary mapping. Refer my related [answer here](http://stackoverflow.com/a/30105055/2530848) for almost related question. Answer is same;Use command pattern with factory pattern. – Sriram Sakthivel May 12 '15 at 08:07

1 Answers1

0

Parsing console commands is difficult to do well, and it is a common problem that has been solved many times. Look for a library to do this for you, or at least to provide some examples.

If you want to parse commands being entered into your program (ie if you are writing a shell of some kind) GNU Readline is an option with available source code (though it is in C). Information here: http://web.mit.edu/gnu/doc/html/rlman_1.html

See this post for C# alternatives for Readline: Is there a .Net library similar to GNU readline?

If you are parsing stuff that was passed to your program as command line options, one option would be getopt: http://www.gnu.org/software/libc/manual/html_node/Getopt.html

This post talks about C# alternatives to getopt: GetOpt library for C#

Community
  • 1
  • 1
Barry Gackle
  • 829
  • 4
  • 17
  • I would prefer to do it myself, but thanks for the answer anyway. Without using a third party library. –  May 12 '15 at 08:29
  • Totally understood, especially if your goal is to learn something new. In that case, I second the suggestion made by @ Sriram Sakthivel -- google the "Command Pattern" for a general look at one relatively modern way to approach this sort of problem. – Barry Gackle May 12 '15 at 08:32