Boost logo

Boost Users :

Subject: Re: [Boost-users] newbie problem with Program Option string value
From: Jeffrey Lee Hellrung, Jr. (jeffrey.hellrung_at_[hidden])
Date: 2013-03-24 12:51:21


On Mar 22, 2013 12:06 PM, "Neil Sutton" <neilmsutton_at_[hidden]> wrote:
>
> 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?

At which line in your program is the exception (indirectly) thrown?

- Jeff



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