Click here to Skip to main content
Click here to Skip to main content
Go to top

Command line parser

, 17 Aug 2002
Rate this:
Please Sign up or sign in to vote.
An intuitive and extensible command line parser class that handles multiple command line formats
<!-- Download Links -->

Introduction

Getting list of arguments from command line is a common task which is required by a lot of applications. However, there is no standard solution (as far as I know Wink | ;) . So I wrote class CCmdLineParser, which can parse arguments from command line, if they are typed in folowing form: 

  • /Key 
  • /KeyWithValue:Value 
  • /KeyWithComplexValue:"Some really complex value of /KeyWithComplexValue"

Of course, multiple keys, Unicode and long (up to 32Kb) command lines are supported.

This implementation requires the MFC or ATL CString class, or some clone with similar interface as well as the STL class map.

Usage

First, you should construct the object and call the Parse function (from constructor or
CCmdLineParser parser(::GetCommandLine());
or
CCmdLineParser parser;
parser.Parse(_T("/Key /Key2:Val /Key3:\"Complex-Value\" -Key4"));

Then, there are two ways of working with results. You can check if some particular key was specified in the command line:

if(parser.HasKey(_T("Key")) {
	// Do some stuff
}
if(parser.HasKey(_T("Key2")) {
	LPCTSTR szKey2Value = parser.GetVal(_T("Key2"));
	// Do something with value of Key2
}

LPCTSTR szKey3Value = parser.GetVal(_T("Key3"));
if(szKey3Value) {
	// There was key "Key3" in input,  
} else {
	// No key "Key3" in input
}

LPCTSTR szKey4Value = parser.GetVal(_T("Key4"));
// Key4 was found in input, but since no value was specified, 
// szKey4Value points to empty string

Another way to use is to enumerate all keys in command line:

CString sKey, sValue;

CCmdLineParser::POSITION pos = parser.getFirst();
while(!realParser.isLast(pos)) {
	realParser.getNext(pos, sKey, sValue);
	// Do something with current key and value
}

Customization and "how it works"

Repeated keys

If several different values are specified with same key, only the first value is stored. So, if user passes command line /Add:One /Add:Two, /Add:Two will be ignored and will not be added to parsed list.

Case sensitive/insensitive

By default, keys are not case-sensitive. So, /KeyOne is equal to -keyONE. This is done by converting all keys to lowercase before storing them. If you want to change this behaviour, call setCaseSensitive(true) or call the constructor with the second argument set to true:
CCmdLineParser parser(::GetCommandLine(), true);
This will switch the parser to case-sensitive mode, and if the user passes -key, then GetKey(_T("Key")) will return false

Syntax

Formally, command line should be in following form:
CommandLine::=[<Key> [,<Key>...]]
<Key>::=<Delimeter>KeyName[<Separator><Value>]
<Value> ::= { KeyValue | <QuoteChar>Quoted Key Value<QuoteChar>} ][
<Delimeter>::= { - | / }
<Separator>::= { : }
<QuoteChar>::= { " }

Values for <Delimeter>, <Separator> and <QuoteChar> are stored in static variables m_sDelimeters, m_sValueSep and m_sQuotes respectively. If you want to change them (for instance, allow user to specify quoted values in apostrophes), you can do it in the beginning of CmdLineParser.cpp:

const TCHAR CCmdLineParser::m_sQuotes[] = _T("\"\'");
Note: If you want to change m_sDelimeters, space must be the first character of this string. Also, if you have your own CString class with other name than CString, you can change it in the beginning of CmdLineParser.h:
typedef MyOwnCString CCmdLineParser_String;
That's it! Wink | ;)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Pavel Antonov
Web Developer
Russian Federation Russian Federation
No Biography provided

Comments and Discussions

 
QuestionThis wheel has been reinvented a number of times: TCLAP, boost program_options to name a few PinmemberLarry S. Smith17-Dec-11 21:28 
GeneralMy vote of 4 Pinmemberhivhiv17-Dec-10 23:18 
QuestionGood!But a question. Pinmembersuxiaojack26-Apr-08 1:28 
GeneralBug report and my implementation of CCmdLineParser::CCmdLineParser() [modified] Pinmemberdoroboy27-Nov-07 21:11 
GeneralRe: Bug report and my implementation of CCmdLineParser::CCmdLineParser() Pinmemberdoroboy13-Dec-07 13:56 
GeneralThank you, I'm just looking for this PinmemberPeter Liu26-Aug-07 3:46 
GeneralExcellent Pinmemberkpinkert19-Aug-05 9:06 
GeneralSupporting - - parameters and map. PinmemberdB.29-Jun-05 6:52 
GeneralRe: Supporting - - parameters and map. PinmemberdB.19-Dec-05 11:05 
Someone asked me for full code.
 
CmdLine.h:
 
/*++
 
    Notes:
        Based on CCmdLine
        Copyright (c) Pavel Antonov, 2002
        http://www.codeproject.com/cpp/cmdlineparser.asp
        Changes by dB. (www.dblock.org)
 
    Formats:
        CommandLine::=[<Key> [,<Key>...]]
        <Key>::=<Delimeter>KeyName[<Separator><Value>]
        <Value> ::= { KeyValue | <QuoteChar>Quoted Key Value<QuoteChar>} ][
        <Delimeter>::= { - | / }
        <Separator>::= { : }
        <QuoteChar>::= { " }
--*/
 
#pragma once
 
class CCmdLine 
    : public CMapStringToString
{
    DECLARE_DYNAMIC(CCmdLine)
public:
    CCmdLine(LPCTSTR sCmdLine = NULL, BOOL bCaseSensitive = FALSE);
    virtual ~CCmdLine(void);
    void Parse(LPCTSTR pszCmdLine);
    inline const CString& GetCmdLine(void) const { return m_strCmdLine; }
    inline void SetCaseSensitive(BOOL bSensitive) { m_bCaseSensitive = bSensitive; }
    inline BOOL GetCaseSensitive(void) const { return m_bCaseSensitive; }
    inline BOOL HasKey(LPCTSTR pszKey) const { CString strValue; return CMapStringToString::Lookup(pszKey, strValue); }
private:
    CString m_strCmdLine;
    BOOL m_bCaseSensitive;
    static const TCHAR m_pszDelimeters[];
    static const TCHAR m_pszValueSep[];
    static const TCHAR m_pszQuotes[];
};
 
#ifdef CPPUNIT_API
class CCmdLineUnitTest : public CppUnit::TestCase 
{
public:
    void testParseCmdLine(void);
public:
    CPPUNIT_TEST_SUITE(CCmdLineUnitTest);
    CPPUNIT_TEST(testParseCmdLine);
    CPPUNIT_TEST_SUITE_END();
};
 
CPPUNIT_TEST_SUITE_REGISTRATION(CCmdLineUnitTest); 
#endif
 
CmdLine.cpp:
 
#include "stdafx.h"
 
IMPLEMENT_DYNAMIC(CCmdLine, CMapStringToString)
 
const TCHAR CCmdLine::m_pszDelimeters[] = _T("-/");
const TCHAR CCmdLine::m_pszQuotes[] = _T("\"\'`");
const TCHAR CCmdLine::m_pszValueSep[] = _T(" :="); // Space MUST be in set, but is not a name/value delimiter.

CCmdLine::CCmdLine(LPCTSTR pszCmdLine, BOOL bCaseSensitive)
    : m_bCaseSensitive(bCaseSensitive)
{
    if (pszCmdLine != NULL) 
    {
        Parse(pszCmdLine);
    }
}
 
CCmdLine::~CCmdLine()
{
 
}
 
void CCmdLine::Parse(LPCTSTR pszCmdLine) 
{
    if (! pszCmdLine)
        return;
    
    m_strCmdLine = pszCmdLine;
    RemoveAll();
 
    int nArgs = 0;
    LPCTSTR pszCurrent = pszCmdLine;
    while(TRUE) 
    {
        // /Key:"arg"
        if (_tcslen(pszCurrent) == 0) 
        { 
            // No data left
            break; 
        } 
 
        LPCTSTR pszArg = _tcspbrk(pszCurrent, m_pszDelimeters);
        if (! pszArg) 
        {
            // No delimeters found
            break; 
        }
 
        LPCTSTR pszInArg;
        do
        {
            pszArg = _tcsinc(pszArg);
            pszInArg = _tcspbrk(pszArg, m_pszDelimeters);
        } while (pszInArg == pszArg);
    
        // Key:"arg"
        if (_tcslen(pszArg) == 0) 
        {
            // String ends with delimeter
            break; 
        }
 
        LPCTSTR pszVal = _tcspbrk(pszArg, m_pszValueSep);
        if (pszVal == NULL) 
        { 
            // Key ends command line
            CString strKey(pszArg);
            if(! m_bCaseSensitive) 
            {
                strKey.MakeLower();
            }
            SetAt(strKey, _T(""));
            break;
        } 
        else if (pszVal[0] == _T(' ') || _tcslen(pszVal) == 1 ) 
        { 
            // Key with no value or cmdline ends with /Key:
            CString strKey(pszArg, (int) (pszVal - pszArg));
            if(! strKey.IsEmpty()) 
            { 
                // Prevent /: case
                if(!m_bCaseSensitive) 
                {
                    strKey.MakeLower();
                }
                SetAt(strKey, _T(""));
            }
 
            pszCurrent = _tcsinc(pszVal);
            continue;
        } 
        else 
        { 
            // Key with value
            CString strKey(pszArg, (int) (pszVal - pszArg));
            if (! m_bCaseSensitive) 
            {
                strKey.MakeLower();
            }
 
            pszVal = _tcsinc(pszVal);
            // "arg"
            LPCTSTR pszQuote = _tcspbrk(pszVal, m_pszQuotes), pszEndQuote(NULL);
            
            if (pszQuote == pszVal) 
            { 
                // Quoted String
                pszQuote = _tcsinc(pszVal);
                pszEndQuote = _tcspbrk(pszQuote, m_pszQuotes);
            } 
            else 
            {
                pszQuote = pszVal;
                pszEndQuote = _tcschr(pszQuote, _T(' '));
            }
 
            if (pszEndQuote == NULL) 
            { 
                // No end quotes or terminating space, take rest of string
                CString strValue(pszQuote);
                if(! strKey.IsEmpty()) 
                { 
                    // Prevent /:val case
                    SetAt(strKey, strValue);
                }
                break;
            } 
            else 
            { 
                // End quote or space present
                if(! strKey.IsEmpty()) 
                {    
                    // Prevent /:"val" case
                    CString strValue(pszQuote, (int) (pszEndQuote - pszQuote));
                    SetAt(strKey, strValue);
                }
                pszCurrent = _tcsinc(pszEndQuote);
                continue;
            }
        }
    }
}
 
#ifdef CPPUNIT_API
typedef struct
{
    LPCTSTR pszName;
    LPCTSTR pszValue;
} CCmdLineTestDataPair;
 
typedef struct 
{
    LPCTSTR pszCmdLine;
    BOOL fCaseSensitive;
    ULONG ulParameters;
    CCmdLineTestDataPair testDataPairs[3];
} CCmdLineTestData;
 
void CCmdLineUnitTest::testParseCmdLine(void)
{
    CCmdLineTestData testdata[] = 
    {
       { _T(""), FALSE, 0, { { NULL, NULL }, { NULL, NULL }, { NULL, NULL } } },
       { _T("/test"), FALSE, 1, { { _T("test"), NULL }, { NULL, NULL }, { NULL, NULL } } },
       { _T("--test"), FALSE, 1, { { _T("test"), NULL }, { NULL, NULL }, { NULL, NULL } } },
       { _T("/-/test"), FALSE, 1, { { _T("test"), NULL }, { NULL, NULL }, { NULL, NULL } } },
       { _T("----test"), FALSE, 1, { { _T("test"), NULL }, { NULL, NULL }, { NULL, NULL } } },
       { _T("/p1 /p2"), TRUE, 2, { { _T("p1"), NULL }, { _T("p2"), NULL }, { NULL, NULL } } },
       { _T("/P1 /p2"), FALSE, 2, { { _T("p1"), NULL }, { _T("p2"), NULL }, { NULL, NULL } } },
       { _T("/P1 /p2 /p2"), FALSE, 2, { { _T("p1"), NULL }, { _T("p2"), NULL }, { NULL, NULL } } },
       { _T("/p1=p1value /p2=p2value /p3=p3value"), FALSE, 3, { { _T("p1"), _T("p1value") }, { _T("p2"), _T("p2value") }, { _T("p3"), _T("p3value") } } },
       { _T("/p1=\"p1value\" /p2=\'p2value\' /p3:`p3value`"), FALSE, 3, { { _T("p1"), _T("p1value") }, { _T("p2"), _T("p2value") }, { _T("p3"), _T("p3value") } } }      
    };
 
    for (int i = 0; i < sizeof(testdata) / sizeof(CCmdLineTestData); i++)
    {
        // std::wcout << testdata[i].pszCmdLine << std::endl;
        CCmdLine c(testdata[i].pszCmdLine, testdata[i].fCaseSensitive);
        CPPUNIT_ASSERT_MESSAGE("Invalid command line parsed.", c.GetCmdLine().Compare(testdata[i].pszCmdLine) == 0);
        CPPUNIT_ASSERT_MESSAGE("Invalid case sensitive parameter.", c.GetCaseSensitive() == testdata[i].fCaseSensitive);
        CPPUNIT_ASSERT_MESSAGE("Invalid parameter count.", c.GetCount() == testdata[i].ulParameters);
 
        for (int j = 0; j < sizeof(testdata[i].testDataPairs) / sizeof(CCmdLineTestDataPair); j++)
        {
            if (testdata[i].testDataPairs[j].pszName == NULL && testdata[i].testDataPairs[j].pszValue == NULL)
                break;
 
            CString strValue;
            CPPUNIT_ASSERT_MESSAGE("Expected parameter not found.", c.Lookup(testdata[i].testDataPairs[j].pszName, strValue));
            CPPUNIT_ASSERT_MESSAGE("Parameter value does not match.", 
                ((strValue.GetLength() == 0) && (testdata[i].testDataPairs[j].pszValue == NULL)) 
                || strValue.Compare(testdata[i].testDataPairs[j].pszValue) == 0);
        }
    }
 
}
#endif

GeneralToo much blah.... Pinmemberdouglash128-Oct-04 6:29 
GeneralRe: Too much blah.... PinmemberJohn M. Drescher28-Oct-04 6:53 
GeneralRe: Too much blah.... Pinmemberpeterchen1-Nov-04 1:28 
GeneralRe: Too much blah.... PinmemberTim Stubbs28-Nov-08 5:11 
GeneralAnother suggestion PinmemberJim Crafton3-Oct-04 3:46 
GeneralRe: Another suggestion Pinmemberahz14-Dec-05 2:33 
GeneralThanks PinmemberRutger Ellen23-Jul-04 4:50 
GeneralLicense issue... PinmemberR. Douglas Barbieri14-Apr-04 8:48 
GeneralLicense: Freeware PinmemberPavel Antonov14-Apr-04 20:25 
GeneralRe: License: Freeware PinmemberMember 163325028-Jul-10 13:21 
GeneralNot a standard Pinsusscodep@michaelleesimons.com13-Jan-04 9:35 
GeneralRe: Not a standard PinmemberStewart Heitmann19-Jan-04 18:41 
GeneralAt last !!! PinmemberSerge Wautier11-Dec-03 2:34 
Generalspaces in command line parameters PinmemberJogi0117-Nov-03 1:17 
GeneralRe: spaces in command line parameters PinmemberPavel Antonov17-Nov-03 1:55 
GeneralRe: spaces in command line parameters PinmemberJogi0119-Nov-03 0:59 
GeneralCommand Line redirection PinmemberTom White5-Nov-03 6:02 
GeneralRe: Command Line redirection Pinsussdeiceone5-Nov-03 9:42 
GeneralRe: Command Line redirection PinmemberTom White5-Nov-03 9:53 
GeneralGreat, but a little question PinmemberAlan Wen15-Oct-03 0:30 
GeneralRe: Great, but a little question PinmemberPavel Antonov15-Oct-03 1:10 
GeneralRe: Great, but a little question PinmemberAlan Wen15-Oct-03 3:17 
GeneralSuggestions Pinmember[James Pullicino]23-Jun-03 4:01 
GeneralGreat! Thanks.. PinmemberAndreas Weller5-Jun-03 2:02 
GeneralExcellent stuff! PinmemberJörgen Sigvardsson19-Aug-02 2:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 18 Aug 2002
Article Copyright 2002 by Pavel Antonov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid