Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Command Line Class

0.00/5 (No votes)
1 Oct 2001 1  
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