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 {}