Simple C++ command-line parser for Windows
Because Windows doesn't have this built-in. Supports quotes. Remember that the first argument returned by GetCommandLine is the application itself.
// Where "tstring" is a typedef for basic_string of TCHAR
void ParseCommandLine (const TCHAR* args, OUT vector<tstring>& list)
{
bool inQuotes = false;
for (const TCHAR* p = args; *p;) {
while (isspace(*p))
p++;
if (*p == '\0')
return;
// Scan to the end of the argument
PCTSTR start;
for (start = p; *p != '\0' && (inQuotes || !isspace(*p)); p++)
if (*p == '"')
inQuotes = !inQuotes;
// Strip quotes if they are along the outside (e.g. the quotes are
// preserved for --out:"some thing" but stripped from "--out:some thing")
if (*start == '"' && p[-1] == '"')
list.push_back(tstring(start + 1, p - start - 2));
else
list.push_back(tstring(start, p - start));
}
}
Usage:
vector<tstring> list; ParseCommandLine(GetCommandLine(), list);

