Click here to Skip to main content
15,867,834 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.6K   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 
when i entered in command prompt like :
C:\new folder1 d:\new folder2 arg3 arg4...

GetFirstParameter() returns;

1st argument = C:\new
2nd argument = folder1
3rd argument = d:\new
4th argument = folder 2 . This is not I want!!!!!!!! Frown | :(

but my requirement is ;

1st argument = C:\new folder1
2nd argument = d:\new folder2
3rd argument = arg3
4th argument = arg4

Do you have any idea on this ?Confused | :confused:
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 
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.