3

How to effectively parse this command line in C++?

program -parameter1=value1 -parameter2=value2 -parameter3=value3

How to effectively drop a combination of the parameter and value

-parameter=value

I am trying to use this code but it does not work properly:

parameter[256], value[256],
while ( --argc > 0 )
{
   if ( *argv[argc] == '-' )
   {
       for ( char * text = argv[argc]; ; )
       {    
            switch ( * ( ++ text ) )
            {
                case '=' :
                {
                   *value = *(  text );
                    break;
                }

                default:
                {
                   *parameter = *text;
                }
            }
         }

       //Testing parameters and values
    }
}

Thanks for your comments and improvements.

4
  • What do you mean by it does not work properly? Does it fail to tell the parts of each command apart from each other? For example i don't see, how value should be filled because you always access its first byte, the same with parameter. Commented Jul 11, 2011 at 13:32
  • 1
    As Mark B answers, I heavily suggest the use of library code to parse arguments. It might look easy, but there are actually gazillions of corner cases to handle. Commented Jul 11, 2011 at 13:34
  • 2
    Your code looks like C. Are you really willing to use C++ constructs ? String processing is really different in C and in C++. Commented Jul 11, 2011 at 13:38
  • 2
    value[256] cries for buffer overflow. Commented Jul 11, 2011 at 15:23

6 Answers 6

16

Did you consider boost::program_options or if you can't use boost, getopt_long?

Sign up to request clarification or add additional context in comments.

1 Comment

This is probably a good answer as long as you don't have to include a thousand other boost files as well.
3

I've leveraged TCLAP ( Templatized C++ Command Line Parser Library ) in many C++ based command line apps and been very happy with it, but it might not give you all the flexibility to read the parameters in the format you are looking at, but still worth a look ,boost::program_options is a good suggestion also.

Comments

3

You can do this a lot more transparently with std::string and functions like find_first_of, splitting off the different parts and have the nice bonus that each time a find function returns std::string::npos, you know you've got an invalid argument.

Comments

1

I'd advise you to use the standard C library for argument parsing : there is a premade function called getopt for that.

1 Comment

getopt is not a part of the standard C library. It's a part of the POSIX standard though.
0

*value = *( text ); This line only writes one character.

Comments

0

Just use the flag library in coost.

// xx.cc
#include "co/flag.h"
#include "co/cout.h"

DEF_bool(x, false, "x");
DEF_uint32(u, 0, "xxx");
DEF_string(s, "", "xx");

int main(int argc, char** argv) {
    flag::parse(argc, argv);
    cout << "x: " << FLG_x << '\n';
    cout << "u: " << FLG_u << '\n';
    cout << FLG_s << "|" << FLG_s.size() << '\n';
    return 0;
}

Define flags with DEF_xxx macros, and call flag::parse(argc,argv) at the beginning of the main function.

Build the above code as xx, run it as follow:

./xx
./xx -x=true -u=77 -s="hello world"
./xx -x -u 88 -s kkk

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.