Solved

VC++: read cmdline arguments

Posted on 2004-09-20
8
912 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

803 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