我需要实现一个可选的标志,比如说-f /-flag.由于这是一个标志,所以没有关联的值.在我的代码中,我只需要知道标志是否被设置.使用boost :: program_options做什么正确的方法?
解决方法
一个方便的方法是使用bool_switch功能:
bool flag = false; namespace po = boost::program_options; po::options_description desc("options"); desc.add_options() ("flag,f",po::bool_switch(&flag),"description"); po::variables_map vm; //store & notify if (flag) { // do stuff }
这比手动检查字符串更安全(字符串只在整个定义中使用一次).