Click here to Skip to main content
13,794,050 members
Click here to Skip to main content
Add your own
alternative version


50 bookmarked
Posted 19 Nov 2003

Object Oriented Command Line Parser

, 26 Jan 2004
Rate this:
Please Sign up or sign in to vote.
Object oriented extensible command line parser


This class is useful in any object oriented command line applications. It provides an easy way to parse, check and distribute the command line arguments. This class parses the arguments in all these formats: <lu>

  • -b
  • -h
  • stdafx.cpp
  • -temp:-7 <lu>

Additionally this class is prepared to parse arguments starting with "--" or "/" at the same time.


The following convention is used to identify the three types of arguments:

  • The arguments that act like a flag ("ls -l") are called empty arguments.
  • Those that also have a value ("netstat -p tcp") are called value arguments.
  • And those who don't have any "-" ("copy a.cpp b.cpp") are called default arguments.

If it's necessary to specify negative numbers (like -temp -7) and to avoid that the engine parses two different empty arguments instead of a value argument, the user can type the command and its value separated by a ":" (like -temp:-7)

Using the code

First of all, the class interface ICmdLineParam must be implemented by all the classes that require to receive the command line arguments. We can implement only a class that receive all the arguments or any number of classes that receive any number of arguments. This interface has two virtual methods: Parse and GetError.

class CConfiguration : public ICmdLineParam
    bool Parse(string argument,string value)
        // The parser calls this method to indicate the name 
        // of the arguments and/or
        // the values, only when applicable. The '-' or '/' 
        // of the arguments are eliminated
        // before calling this method.
        // return false only if the parser must stop, on a serious error.
        // return true when the argument is OK or is malformed, 
        // in this second case 
        // the function GetError must return a description, see below.
    std::string GetError()
        // if a fail in command line occurs, this method must return 
        // a description of the error.
        // Ex: "Inaccesible input file", "invalid type of algorithm",..,etc.
        // return an empty string  to indicate that the arguments were OK.

Second, a command parser must be declared and also all the objects that must receive the arguments.

CCommandLineParser parser;

CConfiguration configuration;
CFileList fileList;
CErrorEntry errObj;

The most important in this process is to assign every argument to an object. This code register the valid arguments and its type (value, empty or default).

// Value arguments

// Empty arguments

// Default arguments, only one object can be asigned that will
// receive all the arguments.

// Parsing errors.

This code above parse a command line with this format:

[-h] [-help] [-alg md5|sha1] [-out hex] file_1 file_2 ... file_n

Finally the process must start calling the method ParseArguments

    // Application starts here

If the user types an invalid argument that is not registered or not type a value in a value argument, then an special argument is passed to the object registered with method SetErrorCommand. Also if one of the classes that implements ICmdLineParam returns a not empty string in GetError method, then another special argument is passed to the error registered class on its method Parse. This special arguments are:

  • UNKNOWN_PARAM argument not registered. Parameter 'value' is assigned with invalid argument.
  • NO_VALUE_PARAM value required. Parameter 'value' is assigned with the value argument.
  • GENERIC_ERROR a ICmdLineParam object returned an error description. Parameter 'value' is assigned with the error description.

This class must output to the user all the descriptions of the errors. See class CError on the demo project for an example.

Points of Interest

This code becomes handy when is necessary to add a new functionality to an already made application. It's easy to add new arguments or new values to previous defined arguments.


  • 24 Nov 03 - updated downloads


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

Daniel Vela
Spain Spain
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralSuggestion - automatically generate 'usage' text Pin
RichardC22-Dec-04 15:02
memberRichardC22-Dec-04 15:02 
GeneralBug Pin
grb12-Jan-04 13:31
membergrb12-Jan-04 13:31 
GeneralRe: Bug Pin
Daniel Vela18-Jan-04 8:56
memberDaniel Vela18-Jan-04 8:56 
GeneralRe: Bug Pin
Sef Tarbell23-Jan-04 7:11
memberSef Tarbell23-Jan-04 7:11 
GeneralRe: Bug Pin
Daniel Vela23-Jan-04 9:30
memberDaniel Vela23-Jan-04 9:30 
If you say that i can modify the parser to split the command where it finds a ":" and then use the second string as the value of the command,... i think is a good solution. I will study it.

Thank you very much.
GeneralRe: Bug Pin
Alexey Yakovlev29-Jan-04 10:56
memberAlexey Yakovlev29-Jan-04 10:56 
GeneralRe: Bug Pin
Daniel Vela29-Jan-04 11:31
memberDaniel Vela29-Jan-04 11:31 
Generalwhy not use '=' for numeric values Pin
s.piras30-Sep-05 4:32
members.piras30-Sep-05 4:32 

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 | 2.8.181207.3 | Last Updated 27 Jan 2004
Article Copyright 2003 by Daniel Vela
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid