Solved

VC++: read cmdline arguments

Posted on 2004-09-20
8
913 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
  • 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

860 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