Best way to cycle through possibilities in a string array?

So I am making a text based game which is controlled via text commands.
How would one cycle through commands which contain multiple words with some starting with the same words?

string RawInput = ObjectInput.GetComponent<InputField>().text.ToLower();
            string[] ParsedInput = RawInput.Split(' ');
            int words = ParsedInput.Length;
            switch(words)
            {
                default: break;
                case 1:
                    if(ParsedInput[0]=="help")
                    {
                       
                    }
                    break;
                case 2:
                    if(ParsedInput[0]=="show")
                    {
                        if(ParsedInput[1]=="help")
                        {

                        }
                    }
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
            }

This is how I am currently doing it but as you can tell this is the worst possible way I can think of. Also, it’s the only way. So how can I do this more efficiently?

Well, this isn’t exaclty how I would do it… but it’s a fairly simple way to do it.

void Process()
{
    string rawInput = ObjectInput.GetComponent<InputField>().text.ToLower();
    ParseCommand(rawInput);
}

void ParseCommand(string cmnd)
{
    if(string.IsNullOrEmpty(cmnd)) return; //do nothing
    if(cmnd.StartsWith(' ')) cmnd = cmnd.Trim(); //clean the string if needed
   
    int i = cmnd.IndexOf(' ');
    string nm;
    string args;
    if(i >= 0)
    {
        nm = cmnd.Substring(0, i); //break off name of command
        args = cmnd.Substring(i).Trim(); //break off rest
    }
    else
    {
        nm = cmnd; //there were no spaces so they typed a single command
        args = string.Empty;
    }
   
    switch(nm)
    {
        case "show":
            this.DoShow(args);
            break;
        case "help"
            this.DoShow(args);
            break;
        //** other commands *//
    }
}


void DoShow(string args)
{
    //what can show do? what are valid args for show?
    //test for those valid args and react accordingly.
}

void DoHelp(string args)
{
    //what can help do? what are valid args for help?
    //test for those valid args and react accordingly.
}

Basically… have methods for each command type, test for the first word in the command, and pass along to the appropriate method.

If one of those methods passes back on itself, do that. For instance, show might call ParseCommand again.

You might implement it where each method returns a “string” result, a sort of response message. So this way say you had a “print” command, and that just calls ParseCommand again.

Something like this:

void Process()
{
    string rawInput = ObjectInput.GetComponent<InputField>().text.ToLower();
    ParseCommand(rawInput);
}

string ParseCommand(string cmnd)
{
    if(string.IsNullOrEmpty(cmnd)) return; //do nothing
    if(cmnd.StartsWith(' ')) cmnd = cmnd.Trim(); //clean the string if needed
   
    int i = cmnd.IndexOf(' ');
    string nm;
    string args;
    if(i >= 0)
    {
        nm = cmnd.Substring(0, i); //break off name of command
        args = cmnd.Substring(i).Trim(); //break off rest
    }
    else
    {
        nm = cmnd; //there were no spaces so they typed a single command
        args = string.Empty;
    }
   
    switch(nm)
    {
        case "show":
            this.DoShow(args);
            break;
        case "help"
            this.DoShow(args);
            break;
        case "print"
            this.DoPrint(args);
            break;
        //** other commands *//
    }
}


string DoShow(string args)
{
    //what can show do? what are valid args for show?
    //test for those valid args and react accordingly.
    return "OUTPUT";
}

string DoHelp(string args)
{
    //what can help do? what are valid args for help?
    //test for those valid args and react accordingly.
    return "OUTPUT";
}

string DoPrint(string args)
{
    if(args.StartsWith('"'))
    {
        int i = args.IndexOf('"', 1);
        string msg = i >= 0 ? args.Substring(1, i - 1) : args.Substring(1);
        Debug.Log(msg);
        return "SUCCESS";
    }
    else
    {
        Debug.Log(ParseCommand(args));
        return "SUCCESS";
    }
}

I don’t fully know though… not sure what all your commands are, nor how complex they get.

Like I said, this isn’t exactly how I’d go… but not sure to what degree of complexity you want.

To give you an example, here is how I actually go about parsing arithmetic (with functions):

This of course is designed to quickly parse arithmetic formulas while also generating as little garbage as possible.