Total members 9951 | Gratitudes |It is currently Sat Feb 11, 2012 10:04 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Question
 Question subject: Object Oriented Command Line Parser
PostPosted: Sat Nov 08, 2008 11:39 pm 
Offline
Expert
User avatar

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

Introduction

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:

* -b
* -h http://www.codeproject.com
* stdafx.cpp
* -temp:-7

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

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.
Code:


class CConfiguration : public ICmdLineParam
{
public:
    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.
Code:

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

Code:
// Value arguments

parser.PutValueCommand("out",&configuration);
parser.PutValueCommand("alg",&configuration);

// Empty arguments

parser.PutEmptyCommand("h",&errObj);
parser.PutEmptyCommand("help",&errObj);

// Default arguments, only one object can be asigned that will

// receive all the arguments.

parser.SetDefaultCommand(&fileList);

// Parsing errors.

parser.SetErrorCommand(&errObj);


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


if(parser.ParseArguments(argc,argv))
{
    // 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.

 Article by:   Daniel Vela


Attachments:
OOCmdLine_demo.zip [42.41 KiB]
Downloaded 132 times
CmdLineParser_src.zip [2.73 KiB]
Downloaded 134 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 
 need help about object detection using haarTraining  C-C++  Anonymous  0
 start a new line in html  HTML examples  msi_333  0
 object detection  Java  Anonymous  1
 How to use connection object in jsp  Servlets / JSP  ashaysumbha  0
 Connection object in jsp  Scripting Language  ashaysumbha  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