Click here to Skip to main content
15,888,031 members
Articles / Desktop Programming / MFC
Article

A Command Line Class

Rate me:
Please Sign up or sign in to vote.
4.42/5 (9 votes)
1 Oct 20011 min read 124.9K   1.7K   39   19
A class that permit to get easily the command line parameters

Overview

I created this class with the idea of to get easily the parameters of command line.
This was inspired by the people of asked severeal times in the Question Forum the following:
- How Can I get the Command Line Parameters?

For this I created the CCommandLine class

The CCommandLine Class.

CCommandLine::CCommandLine

Constructs a CCommandLine object.

CCommandLine();

CCommandLine::GetFirstParameter

The GetFirstParameter returns the first flag and its parameter.

BOOL GetFirstParameter(CString& strFlag, CString& strParam);

Parameters

CString& strFlag Pointer to a buffer in which to return the Flag.

CString& strParam Pointer to a buffer in which to return the Parameter.

If the function has parameters to return, returns TRUE.

Remarks

If the command line has a parameter without flag, the strFlag variable will be empty.

See Sample


CCommandLine::GetNextParameter

The GetNextParameter returns the next flag and its parameter.

BOOL GetNextParameter(CString& strFlag, CString& strParam);

Parameters

CString& strFlag Pointer to a buffer in which to return the Flag.

CString& strParam Pointer to a buffer in which to return the Parameter.

If the function has parameters to return, returns TRUE.

Remarks

If the command line has a parameter without flag, the strFlag variable will be empty.

See Sample


CCommandLine::GetCommandLine

The GetCommandLine returns the command line string for the application that is running.

void GetCommandLine(CString& strCommand);

Parameters

CString& strCommand Pointer to a buffer in which to return the Command Line.


CCommandLine::GetAppName

The GetAppName returns the application name for the application that is running.

void GetAppName(CString& strAppName);

Parameters

CString& strAppName Pointer to a buffer in which to return the Application Name.


CCommandLine::GetAppPath

The GetAppPath returns the complete path from where the application was executed.

void GetAppPath(CString& strAppPath);

Parameters

CString& strAppPath Pointer to a buffer in which to return the Application Path.


Sample

BOOL CTestApp::InitInstance()
{
	CCommandLine pCmd;
	CString strFlag = _T("");
	CString strParam = _T("");

	BOOL bRet = pCmd.GetFirstParameter(strFlag, strParam);
	while(bRet)
	{
		DoSomethingWithTheParameter(strFlag, strParam);
		bRet = pCmd.GetNextParameter(strFlag, strParam);
	}
	.....
	.....
}

Carlos A. Antollini.

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


Written By
Architect Citigroup
Argentina Argentina
Carlos Antollini is a software engineer working on Object Oriented, Visual C++, MFC, COM, ATL, ADO, Internet technologies and Business Intelligence.
Carlos is originally from Argentina, he was living for several years in Fort Lauderdale, Florida, working for Citibank. Then he started his own business.
Carlos is the creator of <a href="http://www.piFive.com">piFive</a>[<a target="_blank" title="piFive" href="http://www.piFive.com">^</a>], a family of BI Analytic Platform software, that it deals next to, <a href="http://www.latinsys.com">latinsys</a>[<a target="_blank" title="latinsys" href="http://www.latinsys.com">^</a>], his partner in businesses...
Currently he is sharing his passion for project management and BI at Citigroup.

Comments and Discussions

 
GeneralSpace between arguments Pin
Prasadaknair27-Oct-08 19:09
Prasadaknair27-Oct-08 19:09 
GeneralRe: Space between arguments Pin
ChewsHumans22-Sep-09 16:49
ChewsHumans22-Sep-09 16:49 
Generalvery helpfull stuff !!! Pin
sani chabi yo2-Jun-05 6:36
sani chabi yo2-Jun-05 6:36 
Generalhelp!! Pin
monimicki13-Dec-04 13:35
monimicki13-Dec-04 13:35 
GeneralRe: help!! Pin
bebenuage23-Dec-04 2:56
bebenuage23-Dec-04 2:56 
GeneralRe: help!! Pin
monimicki23-Dec-04 20:31
monimicki23-Dec-04 20:31 
GeneralRe: help!! Pin
Mikael Segerstein1-Mar-05 7:41
Mikael Segerstein1-Mar-05 7:41 
GeneralRe: help!! [modified] Pin
GilC6-Aug-09 0:09
GilC6-Aug-09 0:09 
GeneralDoes not work as intended Pin
David Crow4-Dec-03 8:13
David Crow4-Dec-03 8:13 
GeneralRe: Does not work as intended Pin
Terry O'Nolley29-Jun-04 13:25
Terry O'Nolley29-Jun-04 13:25 
GeneralScroll Bar Pin
Hump227-Jan-03 20:47
Hump227-Jan-03 20:47 
GeneralQuestion on CCommanLine class Pin
16-May-02 18:03
suss16-May-02 18:03 
GeneralRe: Question on CCommanLine class Pin
Carlos Antollini16-May-02 18:07
Carlos Antollini16-May-02 18:07 
GeneralClass rewrite Pin
B.Mayrargue8-Oct-01 0:03
B.Mayrargue8-Oct-01 0:03 
Here is a leak free, smaller, supporting quoted parameters and spaces in the executable path name version of CCommandLine

I liked the ease of use of this class,
and it's MFC style. But I ran into bugs and missing
functions. So I modified it - completely in fact !

The source files can be downloaded at:
http://www.softlion.com/progs/cmdline.zip
I reproduced them below.

//Example of use:
BOOL CMyApp::InitInstance()
{
CCommandLine pCmd;
CString strFlag,strParam;

for( BOOL bRet = pCmd.GetFirstParameter(strFlag, strParam); bRet; bRet = pCmd.GetNextParameter(strFlag, strParam) )
{
if( strFlag.CompareNoCase("auto") == 0 )
g_bAutoMode = TRUE;

else if( strFlag.CompareNoCase("d") == 0 )
g_askedDate = strParam;

else if( strFlag.CompareNoCase("conf") == 0 )
g_confFile = strParam;

else if( strFlag.CompareNoCase("out") == 0 )
g_outFile = strParam;
}

CMyDlg dlg;
m_pMainWnd = &dlg;
dlg.DoModal();

return FALSE;
}







//
// MODULE: cmdline.h
/*
2001/10/05 - Version 2.00 - Benjamin Mayrargue (http://www.softLion.com http://activex.softLion.com)
Simplifications
leaks free (malloc...)
supports quoted parameters

2001/09/29 - Version 1.01 - Carlos Antollini (cantollini@hotmail.com)
Original version
*/

#pragma once


class CCommandLine: public CCommandLineInfo
{
public:
CCommandLine();
~CCommandLine();

public:
BOOL GetFirstParameter(OUT CString& strFlag, OUT CString& strParam);
BOOL GetNextParameter(OUT CString& strFlag, OUT CString& strParam);

inline void GetCommandLine(CString& strCommand) const {strCommand = m_strCommandLine;};
inline void GetAppName(CString& strAppName) const {strAppName = m_strAppName;};
inline void GetAppPath(CString& strAppPath) const {strAppPath = m_strAppPath;};

protected:
void UnquoteString( OUT CString& strOut ); //Manage the quotes (").

CString m_strCommandLine;
CString m_strAppName;
CString m_strAppPath;
char *m_token;
char *m_string;
};





//
// MODULE: cmdline.h
/*
2001/10/05 - Version 2.00 - Benjamin Mayrargue (http://www.softLion.com http://activex.softLion.com)
Simplifications
leaks free (malloc...)
supports quoted parameters

2001/09/29 - Version 1.01 - Carlos Antollini (cantollini@hotmail.com)
Original version
*/

#include "stdafx.h"
#include "cmdline.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


static char seps[] = " ";


CCommandLine::CCommandLine()
{
m_string = m_token = NULL;

m_strCommandLine = ::GetCommandLine();
m_strCommandLine.TrimLeft();
m_strCommandLine.TrimRight();
m_strAppName = AfxGetAppName();
}


CCommandLine::~CCommandLine()
{
if(m_string)
delete [] m_string;
}


BOOL CCommandLine::GetFirstParameter(CString& strFlag, CString& strParam)
{
strFlag.Empty();
strParam.Empty();

if( m_string ) delete [] m_string;
m_string = new char[m_strCommandLine.GetLength()+ 1];
memset(m_string, 0, (m_strCommandLine.GetLength()+1) * sizeof(char));
lstrcpy(m_string, m_strCommandLine);

m_token = strtok(m_string, seps );

//Forget the first param (it's the file name and it's quoted).
if( !m_token )
return FALSE;
GetNextParameter(strFlag,strParam);

return GetNextParameter(strFlag,strParam);
}


BOOL CCommandLine::GetNextParameter(CString& strFlag, CString& strParam)
{
strFlag.Empty();
strParam.Empty();

if( !m_token )
return FALSE;


//It's a flag
if( *m_token == '/' || *m_token == '-' )
{
m_token++;
UnquoteString(strFlag);
m_token = strtok(NULL, seps);
}

if( *m_token != '/' && *m_token != '-' )
{
UnquoteString(strParam); //It's a param
m_token = strtok( NULL, seps );
}

return TRUE;
}


//Manage the quotes (").
void CCommandLine::UnquoteString( OUT CString& strOut )
{
if( *m_token == '\"' )
{
LPSTR p = ++m_token;
while( m_token && *(m_token+lstrlen(m_token)-1) != '\"' )
{
*(m_token-1) = ' ';
m_token = strtok(NULL, seps);
}
*(m_token-1) = ' '; //For the last one

LPSTR last = m_token+lstrlen(m_token)-1; //Remove the last quote
if( *last == '\"' ) *last = 0;
strOut = p;
}
else
strOut = m_token;
}


________________________

http://www.sofTLion.com
________________________

GeneralRe: Class rewrite Pin
Carlos Antollini9-Oct-01 3:41
Carlos Antollini9-Oct-01 3:41 
GeneralRe: Class rewrite Pin
dazinith9-Oct-02 6:43
dazinith9-Oct-02 6:43 
GeneralSome fixes for your modification Pin
dazinith9-Oct-02 9:27
dazinith9-Oct-02 9:27 
GeneralRe: Some fixes for your modification Pin
Squirrel Twirler26-Apr-05 6:50
Squirrel Twirler26-Apr-05 6:50 
GeneralRe: Some fixes for your modification Pin
Dimok13-May-05 23:24
Dimok13-May-05 23:24 

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.