Boost logo

Boost Users :

Subject: [Boost-users] newbie problem with Program Option string value
From: Neil Sutton (neilmsutton_at_[hidden])
Date: 2013-03-22 15:05:36


I am having problems getting a program option to work. The option should
enable a user to enter a location when they select the '--location' option.
so, if you type --location canada, the code should store the supplied
string - canada and pass it on to the program. This is a typical program
option with nothing special. But I cannot seem to get it to work!

Please look at this:
----------------------------------------------------
std::string place_option; // a global variable

po::options_description general("General options");
general.add_options()
("location", po::value<std::string>(), "location of your home");
// there are other non string options

 po::variables_map vm;
    po::store(po::parse_command_line(ac,av, general),vm);
    po::notify(vm);
     if(vm.count("help"))
      {
std::cout << general << "\n";
return 0;
      }
    if(vm.count("version, v"))
      {
std::cout << "version aplha\n";
      }
 if(vm.count("location"))
       {
        place_option = vm["location"].as<std::string>();
       // should pass the string value entered at command line to
std::string place.
       }

If i run my program with eg 'myprogram --location disney'

I get:
terminate called after throwing an instance of 'std::logic_error'
  what(): basic_string::_S_construct NULL not valid
Aborted
------------------------------

Now I have tried a few variations of the code. The examples show where an
int value is supplied and retrieved. I cannot work out how to do - what
should be very trivial. I simply want to recover the value associated with
'location' i.e a std::string entered at the command line. I then want to
pass that value on to my program to do something with it. I do not
understand the error. Can someone kindly show me the correct syntax to
accomplish this please?

Kind regards.



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net