|
-
May 1st, 2008, 10:42 AM
#1
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?
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
May 1st, 2008, 11:51 AM
#2
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
}
-
May 1st, 2008, 12:47 PM
#3
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.
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
May 1st, 2008, 07:40 PM
#4
Re: CWinAPP::ParseCommandLine() and CCommandLineInfo
-
May 2nd, 2008, 12:22 AM
#5
Re: CWinAPP::ParseCommandLine() and CCommandLineInfo
Thanks Kirants. That's pretty much the same as Hoxsiew's suggestion so I'll try it out today.
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
May 2nd, 2008, 09:17 AM
#6
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))
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
May 2nd, 2008, 10:09 AM
#7
Re: CWinAPP::ParseCommandLine() and CCommandLineInfo
Thanks for the headsup, John E. I need to update the FAQ
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|