views:

819

answers:

2

I'd like to add a command line interface to my MFC application so that I could provide command line parameters. These parameters would configure how the application started.

However, I can't figure out how to interface these two. How could I go about doing this, if it's even possible?

+6  A: 

MFC has a CCommandLineInfo class for doing just that - see the CCommandLineInfo documentation.

RichieHindle
Thanks! Just what i was looking for!
samoz
+1  A: 

Here's how I do it in MFC apps:

int option1_value;
BOOL option2_value;

if (m_lpCmdLine[0] != '\0')
{
     // parse each command line token
     char seps[] = " "; // spaces
     char *token;
     char *p;
     token = strtok(m_lpCmdLine, seps); // establish first token   
     while (token != NULL)
     {
          // check the option
          do    // block to break out of      
          {
               if ((p = strstr(strupr(token),"/OPTION1:")) != NULL)
               {
                    sscanf(p + 9,"%d", &option1_value);
                    break;
               }

               if ((p = strstr(strupr(token),"/OPTION2")) != NULL)
               {
                    option2_value = TRUE;
                    break;
               }
          }
          while(0); 

          token = strtok(NULL, seps);    // get next token   
     }
}   // end command line not empty
BrianK
Very nice solution!Also, welcome to Stack Overflow!
samoz