I have been working on a program that mimics a shell terminal, and I've come across an implementation issue that is harder than I anticipated. Basically, I'm trying to split arguments, much like how the shell does to pass to its executable. So, imagining an input like:
$> ./foo some arguments
One would expect the arguments passed to the program to be an array like (assuming C/C++):
char ** argv = {"foo", "some" "arguments"}
However, if the arguments were:
$> ./foo "My name is foo" bar
The array would be:
char ** argv = {"foo", "My name is foo", "bar"}
Can anyone suggest an efficient way to implement this, such that the interface is like:
vector<string> splitArgs(string allArgs);
or string[] splitArgs(string allArgs);
I can, of course, simply iterate and switch between states of 'reading words'/'reading quoted text', but I feel that that's not as effective as it could be. I also toyed with the idea of regex, but I'm not familiar enough with how this is done in C++. For this project, I do have the boost libraries installed too, if that helps.
Thanks! RR