foobarfuzzbizz
foobarfuzzbizz

Reputation: 58735

Interfacing MFC and Command Line

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?

Upvotes: 3

Views: 6860

Answers (3)

Larrys S. Smith
Larrys S. Smith

Reputation: 171

The CCommandLineInfo stuff is really tedious to use. In increasing order of bloat, I recommend using TCALP (Templatized C++ Command Line Parser http://tclap.sourceforge.net/manual.html) or boost program_options (http://www.boost.org/doc/libs/1_48_0/doc/html/program_options.html) You can also use those libraries in other, non-MFC C++ apps, and even on other operating systems. TCLAP can be configured to support Windows-style parameters, i.e. starting with a "/" instead of POSIX ones starting with "-" (http://tclap.sourceforge.net/manual.html#CHANGING_STARTSTRINGS)

Upvotes: 0

BrianK
BrianK

Reputation: 2705

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

Upvotes: 2

RichieHindle
RichieHindle

Reputation: 281835

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

Upvotes: 8

Related Questions