1

I need to split user input on spaces in a console application I am making, but I'm not quite sure how to do it. I can't just blindly split it because it will have quoted strings and stuff like that. What is a quick way to do this?

Or is there some way I can access the windows command-line parser and split it up using that?

Arlen Beiler
  • 15,336
  • 34
  • 92
  • 135
  • Commandline arguments are usually what @James referred to in his answer; the arguments passed in to the application. What are you looking to parse? Just the user input into the console? – NominSim Jul 16 '12 at 18:09
  • Yes, user input. I've edited my question. – Arlen Beiler Jul 16 '12 at 18:10
  • Ah, ok. This regex should parse your input: [link](http://stackoverflow.com/questions/2148587/regex-quoted-string-with-escaped-quotes-in-c-sharp) or perhaps this is more useful: [link](http://www.codeproject.com/Articles/63374/C-NET-Command-Line-Argument-Parser-Reloaded) – James Jul 16 '12 at 18:16
  • My answer below is how you would handle your "own" console type application. You have to take the input like you normally would and parse it yourself. You need to provide support for quoted strings, no strings, or whatever else you want to support... – Pete Jul 16 '12 at 18:23
  • About the downvote, sorry about the way this question was written, but is it good now or could it still be improved? – Arlen Beiler Jul 16 '12 at 18:49

5 Answers5

3

When you create a new console application in Visual Studio, you get something like this:

class Program
{
    static void Main(string[] args)
    {
    }
}

The command line arguments passed in to the application will be in the 'args' parameter.

James
  • 2,823
  • 22
  • 17
  • I've edited my question to make it clearer. This is not what I am talking about. – Arlen Beiler Jul 16 '12 at 17:54
  • 2
    Actually this does answer your question as it seems to be phrased. You parse the arguments by examining the elements of that array. What specifically do you want to know about parsing, because your question is general and this answer answers it. – Omaha Jul 16 '12 at 18:08
  • Ok, edited my question. I'm talking about user input in the console. – Arlen Beiler Jul 16 '12 at 18:11
1

User input in a console application is simply: Console.ReadLine().

You may want to try something like this:

static void Main(string[] args)
{
    Console.WriteLine("Input please:");
    string input = Console.ReadLine();
    // Parse input with Regex (This splits based on spaces, but ignores quotes)
    Regex regex = new Regex(@"\w+|""[\w\s]*""");
}
NominSim
  • 8,447
  • 3
  • 28
  • 38
  • @ArlenBeiler OK. What do you need to parse? This gives you a string, which you can use [Regex](http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx) with, or look at [Substring()](http://msdn.microsoft.com/en-us/library/aka44szs.aspx)...there is no one method of "Parsing", it is highly dependent on what you want. – NominSim Jul 16 '12 at 18:18
  • Ok, maybe I wasn't being very clear. I've updated my question, hopefully that helps. – Arlen Beiler Jul 16 '12 at 18:25
  • @ArlenBeiler I edited my answer to reflect your newest edit. The regex here will split the string based on spaces, but will ignore spaces within quotes. – NominSim Jul 16 '12 at 18:33
  • will this recognize nested quotes or just alternate every quote it passes. – Arlen Beiler Jul 16 '12 at 18:36
  • 1
    It will only recognize a quote as one `"` followed by text followed by another `"`. You could make it look for others, but I don't know how you would determine whether it was a nested quote or not, e.g.: ("Is this "a quote" that is nested") Does that mean the whole thing is a quote, or does it imply two quotes with text in between? Looking at it seems easy for us, but programming that recognition would be exceedingly difficult for a computer without knowing what to expect. – NominSim Jul 16 '12 at 18:44
0

To read the input as a string I would use:

string stringInput = Console.ReadLine();
HELP_ME
  • 2,619
  • 3
  • 24
  • 31
0

Well, you have to build your own "transducer". Basically, in your main, you can just have a switch statement and a Console.ReadLine(). When the user runs your executable you can output something like Enter Command: and wait for the users input. Then just capture the users input and switch on it.

class Program
{
    static void Main(string[] args)
    {       
        string cmd = Console.ReadLine();
        switch(cmd)
        {
            case "DoStuff":
                someClass.DoStuff();
                break;
            case "DoThis":
                otherClass.DoThis();
                break;
        }       
    }
}

And if you want to continue receiving input commands from the user then just wrap something like that in a while loop and when the user wants to Quit break out of the loop and terminate the program.

Pete
  • 10,651
  • 9
  • 52
  • 74
0

Thanks to this answer, I finally got it. This checks for quotes, but does not worry about nested quotes. As per the comments to that answer, how is it going to know whether it is two quotes or nested quotes. You can't really go by spaces, because strings can start or end with spaces.

using System.Text.RegularExpressions;
...
Console.Write("Home>");
string command = Console.ReadLine();
Regex argReg = new Regex(@"\w+|""[\w\s]*""");
string[] cmds = new string[argReg.Matches(command).Count];
int i = 0;
foreach (var enumer in argReg.Matches(command))
{
    cmds[i] = (string)enumer.ToString();
    i++;
}
Community
  • 1
  • 1
Arlen Beiler
  • 15,336
  • 34
  • 92
  • 135