Boost program_options kitaplığı örneklerinden birini çalıştırıyorum ve birden çok değer/vektör değerlerinden biri için varsayılan bir değer ayarlamayı denemek istedim, ancak görünmüyor çalışmak. Sanırım suggested here to work. Ben bu küçük değişiklik derleme yaparkenboost program_options çoklu değerler sorunu
po::options_description config("Configuration");
config.add_options()
("optimization", po::value<int>(&opt)->default_value(10),
"optimization level")
("include-path,I", o::value< vector<string> >()->default_value(vector<string>(),"SOMETHING")->composing(), "include path")
;
, hiçbir -I seçeneği geçirildiğinde "ŞEY" include- eklendiğini bekliyoruz:
Ne modifiye ettik hattı 40 yaklaşık üzerindedir yol argüman listesi.
Bunun neden böyle bir şey olduğu konusunda herhangi bir fikri olan var mı? İşte
tam kaynak kodu: "DEFAULT_VALUE" yöntemi için#include <boost/program_options.hpp>
namespace po = boost::program_options;
#include <iostream>
#include <fstream>
#include <iterator>
using namespace std;
// A helper function to simplify the main part.
template<class T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
copy(v.begin(), v.end(), ostream_iterator<T>(cout, " "));
return os;
}
int main(int ac, char* av[])
{
try {
int opt;
// Declare a group of options that will be
// allowed only on command line
po::options_description generic("Generic options");
generic.add_options()
("version,v", "print version string")
("help", "produce help message")
;
// Declare a group of options that will be
// allowed both on command line and in
// config file
po::options_description config("Configuration");
config.add_options()
("optimization", po::value<int>(&opt)->default_value(10),
"optimization level")
("include-path,I",
po::value< vector<string> >()->default_value(vector<string>(),"SOMETHING")->composing(),
"include path")
;
// Hidden options, will be allowed both on command line and
// in config file, but will not be shown to the user.
po::options_description hidden("Hidden options");
hidden.add_options()
("input-file", po::value< vector<string> >(), "input file")
;
po::options_description cmdline_options;
cmdline_options.add(generic).add(config).add(hidden);
po::options_description config_file_options;
config_file_options.add(config).add(hidden);
po::options_description visible("Allowed options");
visible.add(generic).add(config);
po::positional_options_description p;
p.add("input-file", -1);
po::variables_map vm;
store(po::command_line_parser(ac, av).
options(cmdline_options).positional(p).run(), vm);
ifstream ifs("multiple_sources.cfg");
store(parse_config_file(ifs, config_file_options), vm);
notify(vm);
if (vm.count("help")) {
cout << visible << "\n";
return 0;
}
if (vm.count("version")) {
cout << "Multiple sources example, version 1.0\n";
return 0;
}
if (vm.count("include-path"))
{
cout << "Include paths are: "
<< vm["include-path"].as< vector<string> >() << "\n";
}
if (vm.count("input-file"))
{
cout << "Input files are: "
<< vm["input-file"].as< vector<string> >() << "\n";
}
cout << "Optimization level is " << opt << "\n";
}
catch(exception& e)
{
cout << e.what() << "\n";
return 1;
}
return 0;
}
Mükemmel şekilde sağol, teşekkürler. – Petriborg
Ayrıca, yardımcı fonksiyonunuzu "şablon ostream & operatörü << (ostream & os, const vektörü & v)" destek isaretine koyarsanız, program_dizimleri bunu bulacak ve "-> default_value (myvector)" komutunu kullanabilirsiniz. ", böylece varsayılan seçenekleriniz otomatik olarak metinsel gösterime dönüştürülecektir. –