A Simple Win32 Command-Line Parser | CodeGuru

A Simple Win32 Command-Line Parser

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[] […]

Written By
CodeGuru Staff
CodeGuru Staff
May 6, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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  = ‘’,
               QUOTE = ‘”‘ };
        bool bInQuotes = false;
        PSZ pargs = m_cmdline;
        while (*pargs)
        {
            while (isspace (*pargs))        // skip leading whitespace
                pargs++;
            bInQuotes = (*pargs == QUOTE);  // see if this token is quoted
            if (bInQuotes)                  // skip leading quote
                pargs++;
            push_back (pargs);              // store position of current token
            // Find next token.
            // NOTE: Args are normally terminated by whitespace, unless the
            // arg is quoted.  That’s why we handle the two cases separately,
            // even though they are very similar.
            if (bInQuotes)
            {
                // find next quote followed by a space or terminator
                while (*pargs &&
                      !(*pargs == QUOTE && (isspace (pargs[1]) || pargs[1] == TERM)))
                    pargs++;
                if (*pargs)
                {
                    *pargs = TERM;  // terminate token
                    if (pargs[1])   // if quoted token not followed by a terminator
                        pargs += 2; // advance to next token
                }
            }
            else
            {
                // skip to next non-whitespace character
                while (*pargs && !isspace (*pargs))
                    pargs++;
                if (*pargs && isspace (*pargs)) // end of token
                {
                   *pargs = TERM;    // terminate token
                    pargs++;         // advance to next token or terminator
                }
            }
        } // while (*pargs)
    } // ParseCmdLine()
}; // class CmdLineArgs

Download demo project – 5 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.