newbie problem with Program Option string value
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::valuestd::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"].asstd::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.
On Mar 22, 2013 12:06 PM, "Neil Sutton"
I am having problems getting a program option to work. The option should
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
enable a user to enter a location when they select the '--location' option. 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::valuestd::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"].asstd::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
I use something like the following:
parsed_options parsed = command_line_parser(argc,argv).style(style).options(desc).allow_unregistered().run();
unreg = collect_unrecognized(parsed.options,include_positional);
po::store(parsed, vm);
po::notify(vm);
desc is equivalent of your general. In your case you would ignore the style and allow_unregistered items.
Larry
From: Jeffrey Lee Hellrung, Jr.
Sent: Sunday, March 24, 2013 12:51 PM
To: boost-users@lists.boost.org
Subject: Re: [Boost-users] newbie problem with Program Option string value
On Mar 22, 2013 12:06 PM, "Neil Sutton"
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::valuestd::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"].asstd::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 mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Jeffrey Lee Hellrung, Jr.
-
Larry
-
Neil Sutton