Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VC++: read cmdline arguments

Posted on 2004-09-20
8
Medium Priority
?
918 Views
Last Modified: 2011-09-20
Hello experts,
I'm using the following code:
---
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow )
{
      bool bOK = false;
      //
      //Read CmdLine's argument(s):
      char *CmdLine;
      CmdLine = (char *) lpCmdLine;
      // Next line can be used for Debugging purposes:
      //MessageBox(0,CmdLine,"A r g u m e n t - Computername",MB_OK);  
      char *computername;
      computername = (char *) CmdLine;
      if (strcmp(computername ,"") == 0)
      {
            MessageBox(0,"No argument has been supplied!","I n f o r m a t i o n",MB_OK);
            return bOK;
      }
                // ...
}
---
Now I want to introduce a switch like "-debug".
This switch I want to use to display some debugging information.

Calling sample:
---
myprog.exe computername                            <== this is working

myprog.exe -debug computername                 <== that is my target
   or
myprog.exe computername -debug                 <== that is my target (which one is better)
---
If anyone knows a solution, please modify the above's code accordingly (I'm using VC++6.0).
Thank you very much for every answer.
  HStrix
 
0
Comment
Question by:HStrix
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12102199
Use the string manipulation routines or 'strtok()' for that:

bool bDebug = false;

nt APIENTRY WinMain(HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR     lpCmdLine,
                    int       nCmdShow )
{
    bool bOK = false;
    //
    //Read CmdLine's argument(s):
    char *CmdLine;
    CmdLine = (char *) lpCmdLine;
    // Next line can be used for Debugging purposes:
    //MessageBox(0,CmdLine,"A r g u m e n t - Computername",MB_OK);  
    char *computername, option;
    computername =  strdup ((char *) CmdLine);
    option = strtok ( computername, " ");

    if ( !strcmp ( option, "-debug")) {

        bDebug = true; // option present

    } else {

         MessageBox(0,"Invalud argument has been supplied!","I n f o r m a t i o n",MB_OK);
         return bOK
    }

    if (!strlen(computername))
    {
         MessageBox(0,"No argument has been supplied!","I n f o r m a t i o n",MB_OK);
         return bOK;
    }
                // ...
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 12102254
BTW, if you'd like a more "universal" solution, take a look at e.g. http://www.codeproject.com/cpp/simpleoptions.asp ("Simple command line processing") or a "full blown" one at http://www.codeproject.com/cpp/cmdlineparser.asp ("Command line parser")

There's more to find at http://www.codeproject.com/cpp/#Command+line+processing
0
 

Author Comment

by:HStrix
ID: 12102802
Thank jkr,
I modified your code to
---
      char *computername;
      char *option;
      computername =  strdup ((char *) CmdLine);
      MessageBox(0,computername,"cmdline",MB_OK);  // for testing only
      option = strtok(computername," ");  
      MessageBox(0,option,"switch",MB_OK);                 // for testing only
                if ( !strcmp ( option, "-debug")) {
        bDebug = true; // option present
            computername = "";  // ???? how to get the computername here ???
    } else {
            bDebug = false;
            computername = (char *) CmdLine;  // here: a switch is not given
    }
---
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 12102856
>> // ???? how to get the computername here ???

You already have it. I assumed the

myprog.exe computername -debug

format, since it is easier to parse.
0
 

Author Comment

by:HStrix
ID: 12103040
Ok, I checked it the way arround.
Now it is as follows: (calling is::= my.exe computername [-debug])
---
      char *computername;
      char *option;
      computername =  strdup ((char *) CmdLine);
      MessageBox(0,computername,"cmdline",MB_OK);  // computername -debug
      option = strtok(computername," ");                      // this is again the computername. not the switch
      MessageBox(0,option,"switch",MB_OK);
                if ( !strcmp ( option, "-debug")) {
                    bDebug = true;    // option present
                } else {
                    bDebug = false;
                }
---
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 12103183
Sorry, sometimes I am slow :o)
    char* lineToParse = strdup ((char *) CmdLine);
    char *computername;
    char *option;
    computername =  strtok(lineToParse ," ");
    MessageBox(0,computername,"cmdline",MB_OK);  
    option = strtok(NULL ," ");                      
    MessageBox(0,option,"switch",MB_OK);
               if ( !strcmp ( option, "-debug")) {
                   bDebug = true;    // option present
               } else {
                   bDebug = false;
               }
0
 
LVL 86

Expert Comment

by:jkr
ID: 12103189
BTW, I'd *really* recommend using one of the articles I mentione above.
0
 

Author Comment

by:HStrix
ID: 12104094
Thank you jkr,
my working code is now as follows:
---
      char *CmdLine;
      char *string;
      CmdLine = (char *) lpCmdLine;
      //
      // calling is::= my.exe computername [-debug]
      char *testarg;
      testarg = (char *) CmdLine;          
                if (!strlen(testarg))
      {
            MessageBox(0,"No argument has been supplied!","I n f o r m a t i o n",MB_OK);
            return bOK;
            //string = "mhpab5gc"; // for testing
      } else
      {
            string = (char *) lpCmdLine;
      }
      //
      bDebug = false;
      char *computername;
                char *option;
      //
      char *token;
      char seps[]   = " ,\t\n";
      token = strtok( string, seps );
      int itok=0;
      option = "-nodebug";
      while( token != NULL )
      {
            /* While there are tokens in "string" */
            //MessageBox(0,token,"token",MB_OK);
            if (itok==0)
            {
                  computername= token;
            } else
            {
                  option=token;
            }
            itok++;
            /* Get next token: */
            token = strtok( NULL, seps );
      }
      //
      if ( !strcmp ( option, "-debug"))
      {
            bDebug = true;    // option present            
      }
      //bDebug = true;  // for testing
      if (bDebug == true)
      {
            MessageBox(0,"true","bDebug",MB_OK);
            MessageBox(0,CmdLine,"A r g u m e n t - Cmdline",MB_OK);
            MessageBox(0,computername,"Computername",MB_OK);
            MessageBox(0,option,"switch",MB_OK);
      } else
      {
            //MessageBox(0,"false","bDebug",MB_OK);
      }      
---
The problem was, that the program crashed in case of looking for the second token (-debug)
if that token was not supplied.

I will have a look at your suggestions as well.

   Thank you again.

     HStrix
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question