Before you say OVERKILL, I don't care.
How can I make Boost.program_options handle the required cat
option -
?
I have
// visible
po::options_description options("Options");
options.add_options()("-u", po::value<bool>(), "Write bytes from the input file to the standard output without delay as each is read.");
po::positional_options_description file_options;
file_options.add("file", -1);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(options).positional(file_options).run(), vm);
po::notify(vm);
bool immediate = false;
if(vm.count("-u"))
immediate = true;
if(vm.count("file"))
support::print(vm["file"].as<vector<string>>());
which throws an exception when I run cat - - -
:
unrecognised option '-'
I want it to see -
as a positional argument, and I need it in the correct order in the full file list. How could I achieve this?
UPDATE
I have a half fix. I needed
po::options_description options("Options");
options.add_options()("-u", po::value<bool>(), "Write bytes from the input file to the standard output without delay as each is read.")
("file", po::value< vector<string> >(), "input file");
po::positional_options_description file_options;
file_options.add("file", -1);
Problem is, I seem to only get 2 of the three -
when I output the arguments:
if(vm.count("file"))
support::print(vm["file"].as<vector<string>>());
where support::print nicely handles the vector and stuff.
You need to define the named program option that is positional, in your case it is file
#include <boost/foreach.hpp>
#include <boost/program_options.hpp>
#include <iostream>
#include <string>
#include <vector>
namespace po = boost::program_options;
int
main( int argc, char* argv[] )
{
std::vector<std::string> input;
po::options_description options("Options");
options.add_options()
("-u", po::value<bool>(), "Write bytes from the input file to the standard output without delay as each is read.")
("file", po::value(&input), "input")
;
po::positional_options_description file_options;
file_options.add("file", -1);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(options).positional(file_options).run(), vm);
po::notify(vm);
bool immediate = false;
if(vm.count("-u"))
immediate = true;
BOOST_FOREACH( const auto& i, input ) {
std::cout << "file: " << i << std::endl;
}
}
Here's a coliru demo and the output if you don't want to click through
$ g++ -std=c++11 -O2 -pthread main.cpp -lboost_program_options && ./a.out - - -
file: -
file: -
file: -
If you only see 2 of 3 positional arguments, it's likely because argv[0]
is by convention the program name, and is therefore not considered for argument parsing. This can be seen in the source code for the basic_command_line_parser
template
37 template<class charT>
38 basic_command_line_parser<charT>::
39 basic_command_line_parser(int argc, const charT* const argv[])
40 : detail::cmdline(
41 // Explicit template arguments are required by gcc 3.3.1
42 // (at least mingw version), and do no harm on other compilers.
43 to_internal(detail::make_vector<charT, const charT* const*>(argv+1, argv+argc+!argc)))
44 {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With