Sets of mutually exclusive options in boost program options

前端 未结 1 1314
Happy的楠姐
Happy的楠姐 2021-01-04 00:59

My program (prog.exe) supports the following four flags: -P, -p , -b and -s. However:

  • -b
1条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-04 01:41

    You should start with a few tutorials [1][2] to understand how boost::program_options works.

    Then, you can define two mutually exclusive options simply defining a small function as explained in real.cpp. For example, you can specify two conflicting (or depending) options defining a conflicting_options() function:

    void conflicting_options(const boost::program_options::variables_map & vm,
                             const std::string & opt1, const std::string & opt2)
    {
        if (vm.count(opt1) && !vm[opt1].defaulted() &&
            vm.count(opt2) && !vm[opt2].defaulted())
        {
            throw std::logic_error(std::string("Conflicting options '") +
                                   opt1 + "' and '" + opt2 + "'.");
        }
    }
    

    and then calling

    conflicting_options (vm, "quiet", "verbose");
    

    right after boost::program_options::store()

    I still don't understand if it's possible to define two mutually exclusive positional options, but that should be another question.

    0 讨论(0)
提交回复
热议问题