|
|
|
|
Sometimes, it is necessary to have a program alter its behavior in response to command line arguments. The problem is, the existing mechanism is hard coded to a limited set of functions, the only one that makes much sense is the one that supplies a document name. Here's what actually happens: During the application's InitInstance method, CCommandLineInfo is the default command line information object. ParseCommandLine calls into the CCommandLineInfo's ParseParam method and it figures out what the command line arguments mean. Here's the odd part - ParseCommandLine iterates through the command line one unit at a time. That includes the argument flags so the argument flag is presented to CComandLineInfo as a separate operation from the actual command line value. As an example, assume that a program's command line arguments are -r 75.50. ParseCommandLine will call ParseParam with 'r' and then it calls a second time with "75.50". It is the responsibility of the ParseParam function to remember the '-r' so that it knows how to deal with the real parameter when it finally shows. In order to get ParseParam to handle the non-standard commands is easy. Just derive your own class from CCommandLineInfo and define your own ParseParam. After the ParseCommandLine has run, it is up to the app to get the information out of the CCommandLineInfo derived class. Derive Class
class CHatcCmdInfo : public CCommandLineInfo
{ public: bool m_bRate; double m_dRate; public: CHatcCmdInfo(); virtual ~CHatcCmdInfo(); virtual void ParseParam(const TCHAR* pszParam, BOOL bFlag, BOOL bLast); }; Implement Class
CHatcCmdInfo::CHatcCmdInfo()
{ m_bRate = false; } CHatcCmdInfo::~CHatcCmdInfo() { } void CHatcCmdInfo::ParseParam(const TCHAR* lpszParam,BOOL bFlag,BOOL bLast) { static char cFlag; if ( bFlag ) { cFlag = lpszParam[0]; } else { switch (cFlag) { // No flag at all. Must be the first argument and must be the data file name. case 0: { m_strFileName = lpszParam; break; } case 'r': case 'R': { m_bRate = true; m_dRate = atof( lpszParam ); break; } } } } Use the class
CHatcCmdInfo cmdInfo;
ParseCommandLine(cmdInfo); m_bRate = cmdInfo.m_bRate;
|
|
|