Total members 9950 | Gratitudes |It is currently Sat Feb 11, 2012 2:32 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Question
 Question subject: Command line parser
PostPosted: Sat Nov 08, 2008 11:36 pm 
Offline
Expert
User avatar

Joined: Tue Nov 06, 2007 2:17 pm
Posts: 847
Has thanked: 0 time
Have thanks: 1 time

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

 Article by:   Pavel Antonov
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! ;)


Attachments:
CmdLineParser_demo.zip [53.25 KiB]
Downloaded 110 times
CmdLineParser_src.zip [2.73 KiB]
Downloaded 111 times

_________________
Any help needed just reply to my topic ,
ccna ,ccnp certified .
TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 start a new line in html  HTML examples  msi_333  0
 Command line chat (client and server)  Java  tetosoft  2
 Help needed to verify a new on-line C test  Job Opportunities  JanHruska  0
 JVM does not recognize MQ command.  Java  Mallesh  0
 the text file i saved all in one line. How can i solve it?  Java  Bluerose  0

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team