Environment: VC6, WIN 9x
A Simple Win32 Command-Line Parser
I was disapointed when I first discovered that (non-console)
Win32 apps did not have argc and argv. This class is my solution.
CmdLineArgs is a subclass of STL vector. After instantiation, it
contains the command line args for your app.
argc – size()
argv – operator[]
Here’s an example of it’s use…
int main ()
{
CmdLineArgs args;for (int i = 0; i < args.size(); i++) puts (args[i]); return 0; }
Here’s a test run of the example code above…
C:>test.exe This “is” a “”test”” “of the parsing” alg”o”rithm.
C:Test.exe
This
is
a
“test”
of the parsing
alg”o”rithm.
The class…
class CmdLineArgs : public std::vector
{
public:
CmdLineArgs ()
{
// Save local copy of the command line string, because
// ParseCmdLine() modifies this string while parsing it.
PSZ cmdline = GetCommandLine();
m_cmdline = new char [strlen (cmdline) + 1];
if (m_cmdline)
{
strcpy (m_cmdline, cmdline);
ParseCmdLine();
}
}
~CmdLineArgs()
{
delete m_cmdline;
}private:
PSZ m_cmdline; // the command line string////////////////////////////////////////////////////////////////////////////////
// Parse m_cmdline into individual tokens, which are delimited by spaces. If a
// token begins with a quote, then that token is terminated by the next quote
// followed immediately by a space or terminator. This allows tokens to contain
// spaces.
// This input string: This “is” a “”test”” “of the parsing” alg”o”rithm.
// Produces these tokens: This, is, a, “test”, of the parsing, alg”o”rithm
////////////////////////////////////////////////////////////////////////////////
void ParseCmdLine ()
{
enum { TERM = ‘