|
Boost Users : |
Subject: Re: [Boost-users] [program_options] unknown positional option
From: Jeffrey Finkelstein (jeffrey.finkelstein_at_[hidden])
Date: 2010-08-05 12:11:55
On Thu, Aug 5, 2010 at 12:21 AM, Vladimir Prus <vladimir_at_[hidden]>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_at_turing:~/test$ g++ -o args args.cpp -lboost_program_options
jeff_at_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>
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net