CWinAPP::ParseCommandLine() and CCommandLineInfo
Does anyone know if the above can be used to extract flags from a list of command line arguments? For example, if someone started my app using this command line:-
MyApp -L -D hello goodbye
I could extract the parameters hello and goodbye - but how can I discover that 2 flags preceded them?
Re: CWinAPP::ParseCommandLine() and CCommandLineInfo
You can override the CCommandLineInfo class and assign it to your CWinApp, then deal with it in the override.
Code:
void CMyCommandLineInfo::ParseParam(const TCHAR* pszParam,BOOL bFlag,BOOL bLast)
{
if((_wcsicmp(pszParam,L"l")==0)&&(bFlag==1)){
m_bMyLFlag=1;
}
if((_wcsicmp(pszParam,L"d")==0)&&(bFlag==1)){
m_bMyDFlag=1;
}
CCommandLineInfo::ParseParam(pszParam,bFlag,bLast);
}
Then in your App InitInstance():
Code:
CMyCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
if(!ProcessShellCommand(cmdInfo)){
return FALSE;
}
//check for command line switches
if(cmdInfo.m_bMyLFlag){
//do something
}
Re: CWinAPP::ParseCommandLine() and CCommandLineInfo
Thanks, I'll experiment with that tomorrow. I wonder why CWinApp::ParseCommandLine() isn't virtual? That would have made life a lot easier.
Re: CWinAPP::ParseCommandLine() and CCommandLineInfo
Re: CWinAPP::ParseCommandLine() and CCommandLineInfo
Thanks Kirants. That's pretty much the same as Hoxsiew's suggestion so I'll try it out today. :thumb:
Re: CWinAPP::ParseCommandLine() and CCommandLineInfo
Kirants, your code works except for the 'flag' comparisons (/o, /e and /whatever). If the parameter was a flag, Windows strips off the leading '-' or '/'
So, for example, instead of :-
Code:
if (0 == strcmp(pszParam, "/o"))
hozsiew's suggestion would be better :-
Code:
if ((0 == strcmp(pszParam, "o")) && (bFlag))
Re: CWinAPP::ParseCommandLine() and CCommandLineInfo
Thanks for the headsup, John E. I need to update the FAQ :)