// File Name : CommandParser.h
// Last Modified By : Shiju P K
// Last Modified On : 03-07-2010
// Description : class to implement comand line parsing
//
// Copyright : (c) . All rights reserved.
//***********************************************************************
#pragma once
class CommandParser
{
char **argList; //point to hold argument list
int numArgs; // no of arguments in the list
public:
CommandParser(void);
CommandParser(int,char**);
~CommandParser(void);
char* GetParameter(const char *key);
};
// File Name : CommandParser.cpp
// Last Modified By : Shiju P K
// Last Modified On : 03-07-2010
// Description : class to implement comand line parsing
//
// Copyright : (c) . All rights reserved.
//***********************************************************************
#include "StdAfx.h"
#include "string.h"
#include "CommandParser.h"
// default constucter
CommandParser::CommandParser(void)
{
numArgs = 0;
argList = NULL;
}
CommandParser::~CommandParser(void)
{
}
//constructer to initialize with number of argument and argument list
CommandParser::CommandParser(int _numArgs,char** _argList)
{
numArgs = _numArgs;
argList = _argList;
}
//return argument that curresponds to the key
char* CommandParser::GetParameter(const char *key)
{
for( int currArg = 0; currArg < numArgs; currArg++ )
{
if( strcmp( key, argList[currArg] ) == 0 )
return argList[currArg+1];
}
return NULL;
}
调用示例:
int main(int argc, char** argv)
{
//CommandParser cp(argc,argv); // object to parse command line
// param = cp.GetParameter("-g");//get
// if(param != NULL )
// greediness = atof(param);
}