A Command Line Class






4.42/5 (9 votes)
Sep 28, 2001
1 min read

126552

1746
A class that permit to get easily the command line parameters
- Overview
- The CCommandLine Class
- CCommandLine
- GetFirstParameter
- GetNextParameter
- GetCommandLine
- GetAppName
- GetAppPath
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
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.
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.
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.
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.
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.