[program_options] unknown positional option

I'm having some trouble using the program_options library. Here is some code slightly modified from an example in the documentation for boost::program_options. <code> #include <iostream> #include <vector> #include <string> #include <boost/program_options/positional_options.hpp> #include <boost/program_options/variables_map.hpp> #include <boost/program_options/parsers.hpp> namespace po = boost::program_options; int main(int argc, char ** argv) { po::positional_options_description desc; desc.add("files", -1); std::cout << "Declared positional options." << std::endl; po::variables_map vm; po::store(po::command_line_parser(argc, argv).positional(desc).run(), vm); std::cout << "Stored command line arguments into vm." << std::endl; po::notify(vm); std::cout << "Notified vm that all args have been parsed" << std::endl; std::cout << " num files specified: " << vm.count("files") << std::endl; std::vector<std::string> files = vm["files"].as<std::vector<std::string>
(); for (unsigned int i = 0; i < files.size(); ++i) { std::cout << "file: " << files[i] << std::endl; }
return 0; } </code> Linking with version 1.40, the above code, when run with the command line arguments "hello world", produces the following output: <shell> Declared positional options. terminate called after throwing an instance of 'boost::exception_detail::clone_\ impl<boost::exception_detail::error_info_injector<boost::program_options::unkno\ wn_option> >' what(): unknown option files Aborted </shell> Linking with version 1.43, the above code, when run with the command line arguments "hello world", produces the following output: <shell> Declared positional options. Segmentation fault </shell> Am I missing something about how to use positional arguments with this library? I am using Ubuntu 10.04 on x86. The 1.40 version of boost::program_options was installed through the repositories, I compiled the 1.43 version manually. Jeffrey

Jeffrey Finkelstein wrote:
I'm having some trouble using the program_options library.
Here is some code slightly modified from an example in the documentation for boost::program_options.
<code> #include <iostream> #include <vector> #include <string> #include <boost/program_options/positional_options.hpp> #include <boost/program_options/variables_map.hpp> #include <boost/program_options/parsers.hpp>
namespace po = boost::program_options;
int main(int argc, char ** argv) {
po::positional_options_description desc; desc.add("files", -1); std::cout << "Declared positional options." << std::endl;
po::variables_map vm; po::store(po::command_line_parser(argc, argv).positional(desc).run(), vm);
positional_options_description merely specifies translation from position to option names. For example, the above say that every positional element of the command line is a value of an option named 'files'. However, such option still have to be declared in a regular way. - Volodya

On Thu, Aug 5, 2010 at 12:21 AM, Vladimir Prus <vladimir@codesourcery.com>wrote:
positional_options_description merely specifies translation from position to option names. For example, the above say that every positional element of the command line is a value of an option named 'files'. However, such option still have to be declared in a regular way.
I see. That is not clear at all in the documentation, including the tutorial. For reference, here's the code working with boost::program_options version 1.40.0: <code> #include <iostream> #include <vector> #include <string> #include <boost/program_options/options_description.hpp> #include <boost/program_options/parsers.hpp> #include <boost/program_options/positional_options.hpp> #include <boost/program_options/variables_map.hpp> namespace po = boost::program_options; int main(int argc, char ** argv) { po::positional_options_description positional; positional.add("files", -1); std::cout << "Declared positional options." << std::endl; po::options_description options; options.add_options() ("files", po::value<std::vector<std::string> >()->composing(), "input files") ; po::variables_map vm; po::store(po::command_line_parser(argc, argv).options(options) .positional(positional).run(), vm); std::cout << "Stored command line arguments into vm." << std::endl; po::notify(vm); std::cout << "Notified vm that all args have been parsed" << std::endl; if (vm.count("files") == 0) { std:: cout << "No input files have been specified" << std::endl; return 1; } std::vector<std::string> files = vm["files"].as<std::vector<std::string>
(); for (unsigned int i = 0; i < files.size(); ++i) { std::cout << "file: " << files[i] << std::endl; }
return 0; } </code> Then compiling and running this program will result in: <shell> jeff@turing:~/test$ g++ -o args args.cpp -lboost_program_options jeff@turing:~/test$ ./args file1 file2 Declared positional options. Stored command line arguments into vm. Notified vm that all args have been parsed file: file1 file: file2 </shell>

Jeffrey Finkelstein wrote:
On Thu, Aug 5, 2010 at 12:21 AM, Vladimir Prus <vladimir@codesourcery.com>wrote:
positional_options_description merely specifies translation from position to option names. For example, the above say that every positional element of the command line is a value of an option named 'files'. However, such option still have to be declared in a regular way.
I see. That is not clear at all in the documentation, including the tutorial.
Documentation patches will be welcome. - Volodya
participants (2)
-
Jeffrey Finkelstein
-
Vladimir Prus