C++ – Parses command-line strings into argv format

Parses command-line strings into argv format… here is a solution to the problem.

Parses command-line strings into argv format

I need to parse the command line string to argv format in order to pass it to execvpe. It is basically the equivalent of CommandLineToArgvW() in Windows. Can I call any function or library to do this? Or do I have to write my own parser? (If I need to do this, I wish I could steal from BASH because my program is GPL…)

Example:
I have three variables:

const char* file = "someapplication";
const char* parameters = "param1 -option1 param2";
const char* environment[] = { "Something=something", NULL };

I want to pass it to execvpe:

execvpe(file, /* parsed parameters */, environment);

PS: I don’t want to extend the filename, but I want to quote and escape

Solution

I used the link provided by rve in the comments ( http://bbgen.net/blog/2011/06/string-to-argc-argv), this solved my problem. Agree with his comment, not my answer!

Related Problems and Solutions