使用程序選項,我正在檢查參數的有效組合。但出於某種原因,gpu參數是一個布爾值,並且無論在命令行中將其設置爲false,它總是如此。如果我在命令行中指定了gpu選項,那麼有沒有辦法讓gpu選項失效?我希望能夠創建一個bool變量來表示是否使用了命令行上的選項。Boost程序選項bool always True
另外,我找不到有關variables_map的count()的任何文檔。它是一個std :: map函數嗎?
部分代碼:
namespace po = boost::program_options;
po::options_description desc("Allowed Options");
desc.add_options()
("help,h", "Produce help message")
("remove_database,r",po::value<std::vector<std::string>>
(&remove_database),
"Remove a pre-built database, provide a name(s) of the database")
("gpu,u", po::bool_switch()->default_value(false),
"Use GPU? Only for specific algorithms");
po::variables_map vm;
po::store(po::parse_command_line(argc,argv,desc),vm);
po::notify(vm);
//Processing Cmd Args
bool help = vm.count("help");
bool remove = vm.count("remove_database");
bool gpu = vm.count("gpu");
test(help,"help");
test(remove, "remove");
test(gpu, "gpu");
.....
void test(bool var1, std::string var2){
if(var1)
std::cout << var2 << " is active " << std::endl;
else
std::cout << var2 << " is not active " << std::endl;
輸出:
$./a.out -r xx -u off
remove is active
gpu is active
$./a.out -r xx -u false
remove is active
gpu is active
'bool_switch'文檔:「工作方式‘價值’的功能相同,但創建value_semantic將不接受任何明確的值,所以,如果選項出現在命令行上,值。將是'真實'。「 –
chris