1

I'm trying to interpret string commands to run associated scripts. This is what I have so far:

        // A typical command that can come from a TextBox control
        string command = "Create";

        // Remove all white space
        command = System.Text.RegularExpressions.Regex.Replace(command, @"\s", "");

        // Make case insesitive by converting to lowercase
        command = command.ToLower();

        switch (command)
        {
            case "create": /* Run create script*/ break;
            case "delete": /* Run delete script*/ break;
            // etc.
        }

This works fine until I introduce parameters associated with a particular command.

I thought that this could be denoted by round brackets so a more complex command would look like:

string command = "Create(paramA, paramB, etc.)"

Assuming I'm on the correct path with this approach, what would be a good method for detecting and interpreting the parameters?

Rules for the command are:

'(' ')' opens and closes the set of parameters ',' separates each parameter

In other words how can I detect the beginning and end of the parameters and correctly separate each one?

The other problem of course is to separate the command itself from the parameters.

I thought about using:

command.StartsWith("create"); // etc.

but this will not work within a switch case conditional structure.

2
  • You could use a lot of string magic, which is possible but requires effort. Or you could look into existing parsing frameworks such as Sprache: github.com/sprache/sprache Commented Apr 12, 2013 at 13:07
  • Parsing. That was the keyword I was looking for. I shall update the title of the question. Thank you. Commented Apr 12, 2013 at 13:13

2 Answers 2

2

You have to create an interpreter class, which splits up the command into separate parts, stored in a Command class. You can then switch on the Command.Name for example.

Some pseudo-code to indicate what I mean:

public static class Interpreter
{
    public static Command CreateCommand(string commandLine)
    {
        Command newCommand = new Command();
        newCommand.Name = commandLine.Split(' ')[0];
        newCommand.Parameters.Add(...)

        return newCommand
    } 
}

You would of course have to check the syntax of the command line exactly. The command class stores the information about the command, like it's name, a list of parameters, return type, etc.

Once you have a Command object, you can perform switch statements on any of it's properties if applicable. Later on, you can write methods to execute the command and have all the information neatly stored, without the need to interpret it again.

Sign up to request clarification or add additional context in comments.

Comments

0

in your code just change the

switch (command)

with

switch (command.Substring(0, command.IndexOf('(')))

for the parameters use a split by ','

Comments

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.