I'm trying to use Boost to be able to pass multiple arguments or have multiple occurrences of a flag with ->multitoken
and ->composing
. However, I want this to be an optional flag using boost::optional<>
.
Below is the basic boost example from their site, modified for my purpose. Without the boost::optional
wrapper, everything works as expected.
run //path/to/example:main --letter a b c
results in a b c
being printed.
If I change options_description to be of boost::optional
type:
("letter", po::value<boost::optional<vector<string>>>()->multitoken()->composing(), "multiple values");
Then --letter a b c'
Errors with option '--letter' only takes a single argument
And --letter a --letter b
Errors with option '--letter' cannot be specified more than once
Anyone know more about boost to be able to use both boost::optional
and ->multitoken()
/->composing()
? I would like to avoid removing boost::optional and adding a default value.
/* The simplest usage of the library.
*/
#include <boost/optional.hpp>
#include <boost/program_options.hpp>
namespace po = boost::program_options;
#include <iostream>
#include <iterator>
using namespace std;
int main(int ac, char* av[]) {
try {
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("letter", po::value<vector<string>>()->multitoken()->composing(), "multiple values");
po::variables_map vm;
po::store(po::parse_command_line(ac, av, desc), vm);
po::notify(vm);
if (vm.count("help")) {
cout << desc << "\n";
return 0;
}
if (vm.count("letter")) {
vector<string> list = vm["letter"].as<vector<string>>();
for (size_t i = 0; i < list.size(); ++i)
cout << list[i] << " ";
cout << '\n';
}
} catch (exception& e) {
cerr << "error: " << e.what() << "\n";
return 1;
} catch (...) {
cerr << "Exception of unknown type!\n";
}
return 0;
}