-
May 5th, 2009, 03:58 AM
#1
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.
Tags for this Thread
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
|