Click here to Skip to main content
13,768,344 members
Click here to Skip to main content
Add your own
alternative version


102 bookmarked
Posted 17 Aug 2002

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 -->


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 ;). 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.


First, you should construct the object and call the Parse function (from constructor or
CCmdLineParser parser(::GetCommandLine());
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


Formally, command line should be in following form:
CommandLine::=[<Key> [,<Key>...]]
<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! ;)


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


About the Author

Pavel Antonov
Web Developer
Russian Federation Russian Federation
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionThis wheel has been reinvented a number of times: TCLAP, boost program_options to name a few Pin
Larry S. Smith17-Dec-11 22:28
memberLarry S. Smith17-Dec-11 22:28 
GeneralMy vote of 4 Pin
hivhiv18-Dec-10 0:18
memberhivhiv18-Dec-10 0:18 
QuestionGood!But a question. Pin
suxiaojack26-Apr-08 2:28
membersuxiaojack26-Apr-08 2:28 
GeneralBug report and my implementation of CCmdLineParser::CCmdLineParser() [modified] Pin
doroboy27-Nov-07 22:11
memberdoroboy27-Nov-07 22:11 
GeneralRe: Bug report and my implementation of CCmdLineParser::CCmdLineParser() Pin
doroboy13-Dec-07 14:56
memberdoroboy13-Dec-07 14:56 
GeneralThank you, I'm just looking for this Pin
Peter Liu26-Aug-07 4:46
memberPeter Liu26-Aug-07 4:46 
GeneralExcellent Pin
kpinkert19-Aug-05 10:06
memberkpinkert19-Aug-05 10:06 
GeneralSupporting - - parameters and map. Pin
dB.29-Jun-05 7:52
memberdB.29-Jun-05 7:52 
GeneralRe: Supporting - - parameters and map. Pin
dB.19-Dec-05 12:05
memberdB.19-Dec-05 12:05 
GeneralToo much blah.... Pin
douglash128-Oct-04 7:29
memberdouglash128-Oct-04 7:29 
GeneralRe: Too much blah.... Pin
John M. Drescher28-Oct-04 7:53
memberJohn M. Drescher28-Oct-04 7:53 
GeneralRe: Too much blah.... Pin
peterchen1-Nov-04 2:28
memberpeterchen1-Nov-04 2:28 
GeneralRe: Too much blah.... Pin
Tim Stubbs28-Nov-08 6:11
memberTim Stubbs28-Nov-08 6:11 
GeneralAnother suggestion Pin
Jim Crafton3-Oct-04 4:46
memberJim Crafton3-Oct-04 4:46 
GeneralRe: Another suggestion Pin
TheGreatAndPowerfulOz14-Dec-05 3:33
memberTheGreatAndPowerfulOz14-Dec-05 3:33 
GeneralThanks Pin
Rutger Ellen23-Jul-04 5:50
memberRutger Ellen23-Jul-04 5:50 
GeneralLicense issue... Pin
R. Douglas Barbieri14-Apr-04 9:48
memberR. Douglas Barbieri14-Apr-04 9:48 
GeneralLicense: Freeware Pin
Pavel Antonov14-Apr-04 21:25
memberPavel Antonov14-Apr-04 21:25 
GeneralRe: License: Freeware Pin
Member 163325028-Jul-10 14:21
memberMember 163325028-Jul-10 14:21 
GeneralNot a standard Pin
codep@michaelleesimons.com13-Jan-04 10:35
susscodep@michaelleesimons.com13-Jan-04 10:35 
GeneralRe: Not a standard Pin
Stewart Heitmann19-Jan-04 19:41
memberStewart Heitmann19-Jan-04 19:41 
GeneralAt last !!! Pin
Serge Wautier11-Dec-03 3:34
memberSerge Wautier11-Dec-03 3:34 
Generalspaces in command line parameters Pin
Jogi0117-Nov-03 2:17
memberJogi0117-Nov-03 2:17 
GeneralRe: spaces in command line parameters Pin
Pavel Antonov17-Nov-03 2:55
memberPavel Antonov17-Nov-03 2:55 
GeneralRe: spaces in command line parameters Pin
Jogi0119-Nov-03 1:59
memberJogi0119-Nov-03 1:59 

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

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

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.181116.1 | Last Updated 18 Aug 2002
Article Copyright 2002 by Pavel Antonov
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid