Boost logo

Boost Users :

From: Jonas Boesch (ml_at_[hidden])
Date: 2007-06-10 10:38:55


Hi,

I'm having a problem with boost::program_options. I attached a
stripped-down version of the code that demonstrates the problem. I
have a number of positional arguments ( "ops" ), and one normal
argument ( "input-file,i" ).

Now, if i run the program

./op_test pos0 pos1 pos2 -i non_positional

it interpretes the "non_positional" as further positional arg, and
not as parameter belonging to -i.

Any hints on how to solve this are greatly appreciated.

My machine:
Mac OS X 10.4 ppc ( G5 )
XCode 2.4.1 ( gcc 4.0.1 )
boost 1.34

Greetings,

Jonas

The code:

// ------------------------ options.h -------------------

#ifndef __VMML__OPTIONS__H__
#define __VMML__OPTIONS__H__

#include <boost/program_options.hpp>
#include<map>

namespace vmml
{
namespace boost_po = ::boost::program_options;

typedef ::boost::program_options::variables_map var_map;
class options
{
public:
     options();

     void setup( int argc, char* argv[] );
     void get_operator_chain( std::vector< std::string >& op_chain );

protected:
     // generic options
     boost_po::options_description _generic_options;
     // op chain
     boost_po::options_description _all_options;
     std::map< std::string, boost_po::options_description* > _op_descs;

     // positional options
     boost_po::positional_options_description _positional_desc;

     boost_po::variables_map _options_map;
     std::vector< std::vector< std::string > > _op_cfgs_from_file;

};//class options

};//namespace vmml

#endif

// --------- options.cpp --------------------------------

#include "options.h"

#include <iostream>
#include <vector>
#include <string>

namespace vmml
{

options::options()
     : _generic_options( "Generic options" )
{
     _generic_options.add_options()
         ("input-file,i", "specify input file")
         ("ops,O", boost_po::value< std::vector< std::string > >()-
>composing(),
             "specify operators " )
         ;
     _positional_desc.add( "ops", -1 );
}

void
options::setup( int argc, char* argv[] )
{
     _all_options.add( _generic_options );

     boost_po::store(
         boost_po::command_line_parser( argc, argv )
             .options( _all_options )
             .positional( _positional_desc )
             //.allow_unregistered()
             .run(),
             _options_map
         );
     boost_po::notify( _options_map );
}

void
options::get_operator_chain( std::vector< std::string >& op_chain )
{
     if ( _options_map.count("ops") )
     {
         const std::vector< std::string >& ops
             = _options_map["ops"].as< std::vector< std::string > >();

         op_chain = ops;
     }
}

}//namespace vmml

int main( int argc, char** argv )
{
     vmml::options opt;
     opt.setup( argc, argv );

     std::vector< std::string > ops;
     opt.get_operator_chain( ops );

     std::vector< std::string >::iterator it = ops.begin();
     std::vector< std::string >::iterator itend = ops.end();
     for( ; it != itend; ++it )
     {
         std::cout << *it << std::endl;
     }

}


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