Solved

VC++: read cmdline arguments

Posted on 2004-09-20
8
914 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
Technology Partners: 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 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: 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.

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…
  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 …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

733 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