Click here to Skip to main content
Licence 
First Posted 10 Mar 2006
Views 24,635
Bookmarked 15 times

Parse command lines using STL

By | 10 Mar 2006 | Article
A simple command line parser employing STL
Title:       Easy Parsing of Command Line switches using STL
Author:      Mohammed Lokhandwala 
Email:       <A href="mailto:lokhandwalamohammed@yahoo.com">lokhandwalamohammed@yahoo.com</A>
Environment: VC++ 5.0-6.0, NT 4.0, Win95/98
Keywords:    Command Line, Parser, STL
Level:       Intermediate"
Description: Describes how command line can be parsed using STL.
Section      C++
SubSection   General

Introduction

This article show code written using STL to parse the command line.  It provides for three forms of command line parameter syntax. a) Pairs, which are of the form "/Name=Value" on the command line, b) Switch, which are of the form "/Value" on the command line, and finally c) Uninterpreted, which are of the form "Value" on the command line.

This class is designed for simplicity in use, as well as Windows style command line parameters. It is not highly customizable, except that it can be derived from, and all functions are therefore virtual. With minor modifications, it can be used with WinMain, but my intention was to use it with standard main argc, argv type parameters.

Using the code

This code is very simple to use. The full name of the only class is Epoch::Foundation::CommandLineParser, for the rest of this article I will assume that the user is using the namespace. At a minimum, simply create an instance of the class CommandLineParser, and then invoke the function parse with the command line argument provided to main.

CommandLineParser parser; 
parser.parse(argc, argv);

Then read values from it as follows.
// Reads a value for a named pair. "/src=MyDir"
parser.getValue("src","Blank");

// Reads a switch, returns true if it exists. "/Pause"
parser.switchExists("Pause");

// Reads an uninterpreted data, returns true if it exists. "NoUse"
parser.nonInterpretedExists("NoUse");

In the above example, the parameter name is src. Blank is the default value, in case the user had not passed any value for that pair.

In addition, Programmers often expect a minimum set of parameters on the command line. If the minimum required parameters do not exist, the program typically terminates with a help screen. I have provided a function verifyCompulsory, which accepts a list of compulsary pairs, switches, and uninterpreted data. Obviously, its really only useful for pairs, but for completeness, I have provided the facility to verify all three formats.

 

 vectorOfString vec_RequiredPairs;

 vec_RequiredPairs.push_back("src");
 vec_RequiredPairs.push_back("dst");
 vec_RequiredPairs.push_back("versionize");

 if(!parser.verifyCompulsory(vec_RequiredPairs)) {
     cerr << endl << "Invalid Parameters." << endl;
     cerr << "Syntax: QUCL /src=\"source directory\" /dst=\"destination directory\" /versionize=true/false" << endl;
     throw Err::InvalidInput; 
} 

Points of Interest

As you will see in the implementation, I have used STL to implement the parser. It is really very simple use of STL, but nonetheless it contributes to making the code small and simple to implement.  

History

This is the first version.

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

About the Author

Mohammed Lokhandwala

Architect
Epoch Consulting Services
India India

Member

Follow on Twitter Follow on Twitter


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralBoost.Program_options PinmemberStephen Hewitt2:34 11 Mar '06  
QuestionEpochTypes.h?? PinmemberPJ Arends6:55 10 Mar '06  
AnswerRe: EpochTypes.h?? Pinmemberall_in_flames2:41 20 Mar '07  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 10 Mar 2006
Article Copyright 2006 by Mohammed Lokhandwala
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid