2009-01-14 53 views
3

例如,使用的語法如下:簡單的選項處理使用Boost庫(C++)

-I [file] -A 1 2 3 

問:

如何檢查是否指定了文件,另外如果(整數)值被指定。

我瞭解以下內容:

po::options_descriptions desc("Allowed options"); 
desc.add_options() 

如何再使用指定的參數,例如:

if (argv[3] == 1) { 
     ... 
    } 

問候

回答

4

您使用variables_map檢查是否有指定的選項。如果你增加了一個選項叫做"file"和你variables_map被稱爲vm

if(vm.count("myoption")) { ... } // Returns 0 if myoption not specified. 1 or more if it was. 

一旦使用add_options添加一些選項,你可以像這樣訪問他們,假設你已經安裝一個名爲variables_mapvm

vm["myoption"].as<int>() // Will return an int, assuming your option is an int 
vm["myoption"].as<std::string>() // Will return an std::string, assuming your option is an int 

在你的情況,你想要將其中一個指定的選項轉換爲一個整數序列。你能做到這一點,像這樣:

vm["myoption"].as< std::vector<int> >() 

這將返回一個包含3個整數,你可以索引一個向量,並使用就像任何普通的載體。要查看是否有專門的3,只需使用size()矢量成員函數。

關於此的增強教程位於here