Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Short argument in boost::program_options without long

Is it possible to specify an argument with boost::program_options with short option only? The answer given here is to use allow_long_disguise, which will result in long options being acceptable with a single dash. Is there any way to make some options short-only (single dash and single character), without using allow_long_disguise?

like image 447
SU3 Avatar asked Sep 13 '26 15:09

SU3


1 Answers

You could write your own parser

namespace po = boost::program_options;

std::pair<std::string, std::string> short_flag_parser(const std::string& s)
{
    // one letter arguments work with single dash
    if (s[0] == '-' && s.size() == 2) {
        return make_pair(s.substr(1), std::string());
    } else {
        return make_pair(std::string(), std::string());
    }
};

int main() {
    desc.add_options()
        ("help, h", "produce help message");
    

    po::variables_map vm;
    po::store(po::command_line_parser(ac, av).options(desc).extra_parser(short_flag_parser).run(), vm);
    po::notify(vm);    

    if (vm.count("help")) {
        std::cout << desc;
        return;
    }
}
like image 107
Alexander Popov Avatar answered Sep 16 '26 05:09

Alexander Popov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!