命令行类






4.42/5 (9投票s)
2001年9月28日
1分钟阅读

127652

1746
一个允许轻松获取命令行参数的类。
概述
我创建这个类的目的是为了方便地获取命令行参数。
这受到问题论坛中多次提出的以下问题启发:
- 如何获取命令行参数?
为此,我创建了CCommandLine
类
构造一个CCommandLine对象。
CCommandLine();
CCommandLine::GetFirstParameter
GetFirstParameter返回第一个标志及其参数。
BOOL GetFirstParameter(CString& strFlag, CString& strParam);
参数
CString& strFlag
指向一个用于返回标志的缓冲区的指针。
CString& strParam
指向一个用于返回参数的缓冲区的指针。
如果函数有参数要返回,则返回TRUE
。
备注
如果命令行有一个没有标志的参数,则strFlag
变量将为空。
CCommandLine::GetNextParameter
GetNextParameter返回下一个标志及其参数。
BOOL GetNextParameter(CString& strFlag, CString& strParam);
参数
CString& strFlag
指向一个用于返回标志的缓冲区的指针。
CString& strParam
指向一个用于返回参数的缓冲区的指针。
如果函数有参数要返回,则返回TRUE
。
备注
如果命令行有一个没有标志的参数,则strFlag
变量将为空。
GetCommandLine返回正在运行的应用程序的命令行字符串。
void GetCommandLine(CString& strCommand);
参数
CString& strCommand
指向一个用于返回命令行的缓冲区的指针。
GetAppName返回正在运行的应用程序的应用程序名称。
void GetAppName(CString& strAppName);
参数
CString& strAppName
指向一个用于返回应用程序名称的缓冲区的指针。
GetAppPath返回应用程序被执行的完整路径。
void GetAppPath(CString& strAppPath);
参数
CString& strAppPath
指向一个用于返回应用程序路径的缓冲区的指针。
示例
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。