Issues with Command Line Parsing in MFC
Hi.
My very first post here at CodeGuru, so bear with me, if it doesn't comply with all the standards :)
I'm trying to add a command line interface to an existing MFC application. As of now I can open the application and load a file into the app. using the command line.
I would like the keep that functionality, and add three optional arguments to the command line interface. I've followed the example given by Kirants here: http://www.codeguru.com/forum/showthread.php?t=386406. The "ParseParam" function implemented in the exampel overrides the original ParseParam function, and therefore I'm unable to use the original ParseParam to open the file.
Code example given below:
CommandLineParser.h:
Code:
class CommandLineParser : public CCommandLineInfo
{
bool clp_reset;
bool clp_verify;
bool clp_load;
public:
CommandLineParser();
virtual ~CommandLineParser();
bool IsLoad();
bool IsVerify();
bool IsReset();
virtual void ParseParam(const char* pszParam, BOOL bFlag, BOOL bLast);
};
CommandLineParser::CommandLineParser()
{
clp_load = clp_verify = clp_reset = false;
}
CommandLineParser::~CommandLineParser()
{
}
bool CommandLineParser::IsLoad()
{
return clp_load;
}
bool CommandLineParser::IsVerify()
{
return clp_verify;
}
bool CommandLineParser::IsReset()
{
return clp_reset;
}
void CommandLineParser::ParseParam(const char* pszParam, BOOL bFlag, BOOL bLast)
{
//Arguments could be either -l or --l
if(0 == strcmp(pszParam, "l") || 0 == strcmp(pszParam, "-l"))
{
clp_load = true;
}
else if(0 == strcmp(pszParam, "v"))
{
clp_verify = true;
}
else if(0 == strcmp(pszParam, "r"))
{
clp_reset = true;
}
}
InitInstance:
Code:
BOOL CMyMlvApp::InitInstance()
{
LPTSTR TempPath;
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
...
TempPath=GetCommandLine();
if (TempPath[0]=='\"') TempPath++;
TempPath[strcspn(TempPath," ")]='\0';
_splitpath( TempPath, drive, dir, fname, ext );
strcpy(MainPathName,drive);
strcat(MainPathName,dir);
SetCurrentDirectory(MainPathName);
...
// Enable DDE Execute open
EnableShellOpen();
RegisterShellFileTypes(TRUE);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo1;
ParseCommandLine(cmdInfo1);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo1))
return FALSE;
CommandLineParser cmdInfo2;
ParseCommandLine(cmdInfo2);
if(cmdInfo2.IsLoad())
{
cout << "IsLoad";
}
else if(cmdInfo2.IsVerify())
{
cout << "IsVerify";
}
cout << "Was it loaded or verified?";
So, to sum it up, I think my issue is that CCommandLineInfo clears the commandline, leaving nothing to be parsed by my CommandLineParser.
How do I work around this?
btw - I'm on Win XP, and using VC++ 6
Best regards
Kasper D.
Re: Issues with Command Line Parsing in MFC
Just call the base class method in your CommandLineParser::ParseParam:
Code:
void CommandLineParser::ParseParam(const char* pszParam, BOOL bFlag, BOOL bLast)
{
//Arguments could be either -l or --l
if(0 == strcmp(pszParam, "l") || 0 == strcmp(pszParam, "-l"))
{
clp_load = true;
}
else if(0 == strcmp(pszParam, "v"))
{
clp_verify = true;
}
....
else
{
CCommandLineInfo::ParseParam( lpszParam, bFlag, bLast );
}
}
Re: Issues with Command Line Parsing in MFC
Wow... Simple and genius solution. This solved my primary issue, but gave me a bunch of new issues, so might return later :P
But for now: Recht schönen Dank Victor! :)
Re: Issues with Command Line Parsing in MFC
You are welcome!
PS: you might also want to look at Paul DiLascia's way to do it here: http://www.microsoft.com/msj/1099/c/c1099.aspx
Re: Issues with Command Line Parsing in MFC
Thank you - will look into that Q&A!
Re: Issues with Command Line Parsing in MFC
Quote:
Originally Posted by
KasperD
This solved my primary issue, but gave me a bunch of new issues, so might return later :P
Cool! I should update the FAQ. Thanks for bringing it up.
Also, curious as to what the new issues are. If related to the FAQ, I would like to fix them.
Re: Issues with Command Line Parsing in MFC
Hi kirants!
Okay, at least one of my new issues might be related to the FAQ :)
The order of arguments in the commandline matters. I would like it to be irrelevant.
e.g. "myAppName -opt1 file.txt" will execute option1, and open file.txt. But "myAppName file.txt -opt1" will execute option1, but will not open the file.
My final problem is a minor one - I would like the program to be able to handle 3 options. e.g. "myAppName -opt1 -opt2 -opt3 file.txt" which is suppose to open the file, and execute the three options. I 've solved this issue by not using an else-if-ladder in the initInstance() :)
//Kasper D.