Total members 11895 |It is currently Sun Dec 22, 2024 11:12 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Object Oriented Command Line Parser code implementation using C++.

java code
// CmdLineParser.cpp: implementation of the CCmdLineParser class.
//
// Copyright (c) Pavel Antonov, 2002
//
// This code is provided "as is", with absolutely no warranty expressed
// or implied. Any use is at your own risk.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "CmdLineParser.h"

const TCHAR CCmdLineParser::m_sDelimeters[] = _T("-/");
const TCHAR CCmdLineParser::m_sQuotes[] = _T("\""); // Can be _T("\"\'"), for instance
const TCHAR CCmdLineParser::m_sValueSep[] = _T(" :"); // Space MUST be in set

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CCmdLineParser::CCmdLineParser(LPCTSTR sCmdLine, bool bCaseSensitive)
: m_bCaseSensitive(bCaseSensitive)
{
if(sCmdLine) {
Parse(sCmdLine);
}
}

CCmdLineParser::~CCmdLineParser()
{
m_ValsMap.clear();
}

bool CCmdLineParser::Parse(LPCTSTR sCmdLine) {
if(!sCmdLine) return false;

m_sCmdLine = sCmdLine;
m_ValsMap.clear();
const CCmdLineParser_String sEmpty;

int nArgs = 0;
LPCTSTR sCurrent = sCmdLine;
while(true) {
// /Key:"arg"
if(_tcslen(sCurrent) == 0) { break; } // No data left
LPCTSTR sArg = _tcspbrk(sCurrent, m_sDelimeters);
if(!sArg) break; // No delimeters found
sArg = _tcsinc(sArg);
// Key:"arg"
if(_tcslen(sArg) == 0) break; // String ends with delimeter
LPCTSTR sVal = _tcspbrk(sArg, m_sValueSep);
if(sVal == NULL) { //Key ends command line
CCmdLineParser_String csKey(sArg);
if(!m_bCaseSensitive) {
csKey.MakeLower();
}
m_ValsMap.insert(CValsMap::value_type(csKey, sEmpty));
break;
} else if(sVal[0] == _T(' ') || _tcslen(sVal) == 1 ) { // Key with no value or cmdline ends with /Key:
CCmdLineParser_String csKey(sArg, sVal - sArg);
if(!csKey.IsEmpty()) { // Prevent /: case
if(!m_bCaseSensitive) {
csKey.MakeLower();
}
m_ValsMap.insert(CValsMap::value_type(csKey, sEmpty));
}
sCurrent = _tcsinc(sVal);
continue;
} else { // Key with value
CCmdLineParser_String csKey(sArg, sVal - sArg);
if(!m_bCaseSensitive) {
csKey.MakeLower();
}

sVal = _tcsinc(sVal);
// "arg"
LPCTSTR sQuote = _tcspbrk(sVal, m_sQuotes), sEndQuote(NULL);
if(sQuote == sVal) { // Quoted String
sQuote = _tcsinc(sVal);
sEndQuote = _tcspbrk(sQuote, m_sQuotes);
} else {
sQuote = sVal;
sEndQuote = _tcschr(sQuote, _T(' '));
}

if(sEndQuote == NULL) { // No end quotes or terminating space, take rest of string
CCmdLineParser_String csVal(sQuote);
if(!csKey.IsEmpty()) { // Prevent /:val case
m_ValsMap.insert(CValsMap::value_type(csKey, csVal));
}
break;
} else { // End quote or space present
if(!csKey.IsEmpty()) { // Prevent /:"val" case
CCmdLineParser_String csVal(sQuote, sEndQuote - sQuote);
m_ValsMap.insert(CValsMap::value_type(csKey, csVal));
}
sCurrent = _tcsinc(sEndQuote);
continue;
}
}

}

return (nArgs > 0);
}

CCmdLineParser::CValsMap::const_iterator CCmdLineParser::findKey(LPCTSTR sKey) const {
CCmdLineParser_String s(sKey);
if(!m_bCaseSensitive) {
s.MakeLower();
}
return m_ValsMap.find(s);
}
// TRUE if "Key" present in command line
bool CCmdLineParser::HasKey(LPCTSTR sKey) const {
CValsMap::const_iterator it = findKey(sKey);
if(it == m_ValsMap.end()) return false;
return true;
}

// Is "key" present in command line and have some value
bool CCmdLineParser::HasVal(LPCTSTR sKey) const {
CValsMap::const_iterator it = findKey(sKey);
if(it == m_ValsMap.end()) return false;
if(it->second.IsEmpty()) return false;
return true;
}
// Returns value if value was found or NULL otherwise
LPCTSTR CCmdLineParser::GetVal(LPCTSTR sKey) const {
CValsMap::const_iterator it = findKey(sKey);
if(it == m_ValsMap.end()) return false;
return LPCTSTR(it->second);
}
// Returns true if value was found
bool CCmdLineParser::GetVal(LPCTSTR sKey, CCmdLineParser_String& sValue) const {
CValsMap::const_iterator it = findKey(sKey);
if(it == m_ValsMap.end()) return false;
sValue = it->second;
return true;
}

CCmdLineParser::POSITION CCmdLineParser::getFirst() const {
return m_ValsMap.begin();
}
CCmdLineParser::POSITION CCmdLineParser::getNext(POSITION& pos, CCmdLineParser_String& sKey, CCmdLineParser_String& sValue) const {
if(isLast(pos)) {
sKey.Empty();
return pos;
} else {
sKey = pos->first;
sValue = pos->second;
pos ++;
return pos;
}
}
// just helper ;)
bool CCmdLineParser::isLast(POSITION& pos) const {
return (pos == m_ValsMap.end());
}





Attachments:
OOCmdLine_demo.zip [42.41 KiB]
Downloaded 871 times
CmdLineParser_src.zip [2.73 KiB]
Downloaded 850 times

_________________
Please recommend my post if you found it helpful. ,
java,j2ee,ccna ,ccnp certified .
Author:
Expert
User avatar Posts: 838
Have thanks: 2 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Command Line Parser Code Using C++
 How to implement Command Line Parser in C++ Code     -  
 read from command line     -  
 c program - command line prompt - need help     -  
 Command line chat (client and server)     -  
 program to run shell command line functions     -  
 Reading a File Line by Line in php     -  
 php SAX parser in action     -  
 php simple XML parser     -  
 Line Translation 2D     -  
 Line Rotation 2D     -  



Topic Tags

C++ Inputs
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com