I am working on a program that mimics a shell terminal, and I ran into an implementation problem that is harder than I expected. Basically, I am trying to separate the arguments, just as the shell goes to its executable. So, presenting the input as:
$> ./foo some arguments
One would expect the arguments passed to the program to be similar to an array (assuming C / C ++):
char ** argv = {"foo", "some" "arguments"}
However, if the arguments were:
$> ./foo "My name is foo" bar
The array will be:
char ** argv = {"foo", "My name is foo", "bar"}
Can anyone suggest an efficient way to implement this, so the interface looks like this:
vector<string> splitArgs(string allArgs); or string[] splitArgs(string allArgs);
Of course, I can simply iterate over and switch between the states of "reading words" / "reading quoted text", but I feel that it is not as effective as it could be. I also played with the idea of ββregex, but I'm not good enough at how this is done in C ++. I also have boost libraries installed for this project, if that helps.
Thanks! Rr
source share