boost-program-options

Custom validator for boost program_options doesn't work with GCC, works with MSVC

可紊 提交于 2020-01-04 06:45:45
问题 Let's say I would like special processing for integer options. According to the documentation I have to write my own validate function. Consider the following short program. #include <iostream> #include <vector> #include <string> #include <boost/program_options.hpp> namespace po = boost::program_options; namespace boost { namespace program_options { template <class charT> void validate(boost::any& v, const std::vector<std::basic_string<charT> >& xs, unsigned int*, int) { std::cout <<

boost::program_options - how to handle multiple sections with the same name in INI file

蓝咒 提交于 2020-01-02 02:38:06
问题 In a config like below; is there a way to handle individual sections. I am looking for a way to validate individual "server" sections below, in a reliable manner. [basic] number_of_servers=3 [server] ip=10.20.30.40 password=sdfslkhf [server] ip=10.20.30.41 password=sdfslkhf [server] ip=10.20.30.42 password=sdfslkhf [server] password=sdfslkhf [server] ip=10.20.30.42 回答1: When using boost::program_options to parse a INI file, the option names must be prefixed by their enclosing section names.

boost-program-options: notifier for options with no value

别说谁变了你拦得住时间么 提交于 2019-12-28 06:27:34
问题 One can use notifier for parsed options only if they have value_semantic. What is the best way for no-value options to be automatically handled by the given notifier? The simple approach is to make a dummy value_semantic with implicit assignment, so a user can pass the option without a value. This leads to a possibility of explicitly provided values. One can add a run-time check if the value was provided and throw an error. Update: BUT, this doesn't work in presence of positional options,

Specifying levels (e.g. --verbose) using Boost program_options

末鹿安然 提交于 2019-12-22 09:34:04
问题 Some of my options have multiple levels, e.g. of "verbosity". I would like my users to choose between the following two equivalent styles: // no argument: verbosity of 1 my_program -v // count the 'v's: verbosity of 4 my_program -vv --something_else XYZ -vv // specify the value: verbosity of 4 my_program --verbose 3 What is the easiest way of doing this with the Boost program_options library? 回答1: This is how I envisage the code being used. We use level_value in place of the normal program

How to accept empty value in boost::program_options

余生长醉 提交于 2019-12-22 03:19:11
问题 I'm using boost::program_options library to process command line params. I need to accept a file name via -r option, in case if it is empty (-r given without params) I need to use stdin. desc.add_options() ("replay,r", boost::program_options::value<std::string>(), "bla bla bla") In this case boost wouldn't accept -r without params and throw an exception. default_value () option does not work as well as it would make library return value even if user didn't give -r option. Any ideas how to

boost::program_options config file option with multiple tokens

醉酒当歌 提交于 2019-12-20 17:56:17
问题 I can not seem to be able to read from config file multitoken options like I can from command line. What is the syntax for the config file? This is how the option description is added: //parser.cpp - - - po::options_description* generic; generic=new po::options_description("Generic options"); generic->add_options() ("coordinate",po::value<std::vector<double> >()->multitoken(),"Coordinates (x,y)"); After which I parse command and config-files. On command line '--coordinate 1 2' works. However,

Building boost::options from a string/boost::any map

妖精的绣舞 提交于 2019-12-17 18:10:02
问题 I have a map which represents a configuration. It's a map of std::string and boost::any . This map is initialized at the start and I'd like the user to be able to override these options on the command line. What I'd love to do is build the program options from this map using the options_description::add_option() method. However, it takes a template argument po::value<> whereas all I have is boost::any . So far, I just have the shell of the code. m_Config represents my configuration class, and

Undefined reference when linking with Boost using g++-4.9 on a g++-5-ish distribution

北城余情 提交于 2019-12-13 02:15:21
问题 I've written the following groundbreaking application: #include <boost/program_options.hpp> int main(int argc, char** argv) { boost::program_options::options_description generic_options("foo"); return 0; } I am trying to build this on Debian Stretch, on which the default compiler is GCC 5.3.1-5, and the installed version of Boost is 1.58.0. However, for reasons which I will not go into here (which would be apparent had this not been a MCVE), I need to compile and link the binary using g++-4.9

boost::program_options - does it do an exact string matching for command line options?

*爱你&永不变心* 提交于 2019-12-12 15:44:36
问题 There seems to be a problem the way boost::program_options's options_description matching is done. int main(int argc, char* argv[]) { boost::program_options::options_description desc("CmdLine utility"); desc.add_options() ("hel", "hel message") ("help", "produce help message") ("helps","helps message") ; boost::program_options::variables_map vm; boost::program_options::store(boost::program_options::parse_command_line(argc, argv,desc), vm); boost::program_options::notify(vm); if(vm.count("help

Short options only in boost::program_options

久未见 提交于 2019-12-12 07:25:08
问题 How would one go about specifying short options without their long counterparts in boost? (",w", po::value<int>(), "Perfrom write with N frames") generates this -w [ -- ] arg : Perfrom write with N frames Any way to specify short options only? 回答1: If you are using command line parser, there is a way to set different styles. So the solution would be to use only long options and enable allow_long_disguise style which allows long options to be specified with one dash (i.e. "-long_option"). Here