John F. Bergin
Bellevue, WA
(425) 242-1394
John.F.Bergin@Comcast.net Resume
Word Formatted Resume (Right Click and "Save Target As")
Cover Letter White Papers Code Examples
Senior
Software Engineer
Code Examples by John F. Bergin
Command Line Arguments
The following structure and program can be used to handle C++ command line arguments. The command line arguments are parsed and
stored in a data structure that can be used throughout the program.
// Structure for Command Line Parsing
typedef struct CmdLineOptions {
int Method;
int Hide;
int Help;
} CmdLineOptions;
// Prototype
CmdLineOptions *ParseCommandLine( CmdLineOptions*, LPWSTR );
//--------------------------------------------------------------------------------------
// Main Program
//--------------------------------------------------------------------------------------
int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow )
{
// Parse Command Line Arguments
CmdLineOptions *CmdLine = new CmdLineOptions;
ParseCommandLine(CmdLine, lpCmdLine);
// Print Command Line Arguments
printf("Method = %i \n" CmdLine->Method);
printf("Hide = %i \n" CmdLine->Hide);
printf("Help = %i \n" CmdLine->Help);
return 0;
}
//--------------------------------------------------------------------------------------
// Parse Command Line Arguments
//--------------------------------------------------------------------------------------
CmdLineOptions *ParseCommandLine( CmdLineOptions *CmdLine, LPWSTR lpCmdLine )
{
// Set Command Line Option Defaults
CmdLine->Method = 0;
CmdLine->Hide = 0;
CmdLine->Help = 0;
int numOptions = 3;
// Return if no Command Line Arguments
if ( lpCmdLine == NULL )return CmdLine;
wchar_t *tok;
wchar_t delimiters[] = L" ,\t\n";
wchar_t *next_tok = NULL;
int argc = 0;
wchar_t *argv[200];
// Parse Command Line Arguments and populate argc and argv[].
// // NOTE: wcscmp() errors if you compare the last token // from wcstok(), so count the number of arguments before // trying to do string comparisons. //tok = wcstok_s( lpCmdLine, delimiters, &next_tok);
argv[argc] = tok;
while ( tok != NULL )
{
argc++;
tok = wcstok_s( NULL, delimiters, &next_tok);
argv[argc] = tok;
}
int argNum=0;
int argument=0;
for ( argNum=2;argNum<=numOptions*2;argNum=argNum+2 )
{
if ( argc == argNum )
{
// If (argc == 2), do this once, if (argc == 4), do this twice, etc.
for ( argument=0;argument<argc/2;argument=argument++ ){
if ( _wcsicmp( argv[argument*2], L"Method") == 0 )
{
CmdLine->Method = atoi((const char *)argv[argument*2+1]);
}
else if ( _wcsicmp( argv[argument*2], L"Hide") == 0 )
{
CmdLine->Hide = 1
}
else if ( _wcsicmp( argv[argument*2], L"Help") == 0 )
{
CmdLine->Help = 1;
}
}
}
}
argv[0] = '\0';
// Make sure arguments are valid
if ( CmdLine->Method < 0 )CmdLine->Method = 0;
if ( CmdLine->Method >= 2 )
CmdLine->Method = 2;
if ( CmdLine->Hide < 0 )
CmdLine->Hide = 0;
if ( CmdLine->Hide > 1 )
CmdLine->Hide = 1;
if ( CmdLine->Help < 0 )
CmdLine->Help = 0;
if ( CmdLine->Help > 1 )
CmdLine->Help = 1;
return CmdLine;
}
____________________________
Copyright © 2010 by John F. Bergin.