I am creating a CMD like program where the user manage his "drive" with commands. I would like to know if there is a way of doing this type of application in a easier/better way. Actually I'm doing like this:
Listing the commands
I created an enum with all commands to be easy to add and manage commands
Executing the command line
To know what command the user is trying to execute I get a list with the enum values and proceed to get the command in the list.
var list = ((Commands[])Enum.GetValues(typeof(Commands))).ToList();
var commandText = inputLine.Split(' ')[0];
if (list.Select(x => x.ToString().ToLower()).Contains(commandText))
{
var command = list.FirstOrDefault(x => x.ToString().ToLower() == commandText);
switch (command)
{
case Commands.List:
Listar();
break;
case Commands.Go:
Go();
break;
}
}
Edit:
The question came out wrong, I'm not trying to find a better way to solve the switch problem, I'm using it because it is a limited number of commands, so it make sense to use it. The answer I wanted was just a better way to make a Command Line Parser. Actually I'm in a competition where I don't have internet access, so no NuGet.
The way of getting the parameters seems bad too:
var temp = _line.Split('"').ToList();
var vals = new List<string>();
for (int i = 0; i < temp.Count; i++)
{
if (i % 2 == 0)
vals.AddRange(temp[i].Split('-').Select(x => x.Trim()));
else
vals.Add(temp[i]);
}
var path = _currentDirectory;
if (vals.Contains("p"))
path = vals[vals.IndexOf("p") + 1];