Boost logo

Boost Users :

Subject: [Boost-users] [Program_options] Positional options are not parsed, "unrecognised option". A bug?
From: Dmitry Shintyakov (shintyakov_at_[hidden])
Date: 2012-11-04 09:41:21


Hello, everybody.
I am having problems using Boost::Program_options library.
I have a program, that have several optional keyword arguments, and 3
positional arguments.
Keyword arguments are parsed well, but positional arguments are always
causing an exception? saying: "unrecognised option"

After spending 2 hours trying to find a problem, I decided to ask for help.

I am using Boost 1.50.0-2, GCC 4.7.2 under Arch linux (32 bit).

My example is almost exact copy from the tutorial, so I suspect that
there is some bug. Example code (with all non-relevant code removed)
follows. To test it, run it as following:

./a.out 123 123 test

Expected behaviour is to see a line:
Arguments: output=output.png width=123 height=123

Instead, I see this:
terminate called after throwing an instance of
'boost::exception_detail::clone_impl<boost::exception_detail::
error_info_injector<boost::program_options::unknown_option> >'
  what(): unrecognised option '1'

==Test code==
#include <iostream>
#include <boost/program_options.hpp>
using namespace std;

int main( int argc, char * argv[] )
{
  namespace po = boost::program_options;
  string output_file;

  po::options_description desc("converter [options] width height source");
  desc.add_options()
    ("help", "Show help message")
    ("output,o", po::value<string>( &output_file
)->default_value("output.png"),
     "Output file to put converted image data to. Default is output.png")
    ;

  po::positional_options_description pos_desc;
  pos_desc.add("width",1).add("height",1).add("input-file",1);

  po::variables_map vm;

  po::store( po::command_line_parser(argc, argv)
             .options(desc)
             .positional(pos_desc)
             .run()
             , vm );
  po::notify( vm );

  if( vm.count("help") ){
    cout << desc <<endl;
    return 0;
  }

  if (!vm.count("width") || !vm.count("height") || !vm.count("input-file") ){
    cerr<<"Error: width, weight and input file must be specified"<<endl;
    return 1;
  }

  cout << "Arguments: output="<<output_file
       << " width="<<vm["width"].as<string>()
       << " height="<<vm["height"].as<string>()<<endl;
  return 0;
}


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