Boost logo

Boost Users :

From: Marc VIALA (mviala_at_[hidden])
Date: 2005-09-13 09:17:32


I'm just trying to use Boost.Program_options in an application and
during my implementation, it appears a couple of questions:
- Is it possible to introduce more than one "multitoken"
options in command line?
I didn't see any limitation in the documentation but my
sample hereafter seems to prove that yes.
- If the answer to the first question is yes, then how I can
handle this feature?
I seemed that with the older version, 1.32, this feature was
supported?
Thanks in advance.
Best regards,
Marc VIALA
--------------------------------------------------------------
Test Configuration:
--------------------------------------------------------------
        PC / WinXP SP2
        Visual C++ 7.1
        Boost 1.33
--------------------------------------------------------------
Code Example:
--------------------------------------------------------------
// How to use multitoken options in command line?
#include <vector>
#include <iostream>
#include <iterator>
#include <boost/program_options.hpp>
#include <boost/lambda/lambda.hpp>
using namespace boost ;
using namespace boost::lambda ;
namespace po = boost::program_options ;
using namespace std ;
int
main(int argc, char* argv[] )
{
  string rootDir ;
  vector<string> inputList, outputList ;
  // Options declaration
  po::options_description generic("Generic options") ;
  generic.add_options()
    ("help" , "produce a help message" )
    ("version" , "output the version number")
    ;
  po::options_description mandatory("Mandatory options");
  mandatory.add_options()
    ("root,r" , po::value<string>(&rootDir)
                , "set project root catalog" )
    ("acq,a" , po::value<vector<string> >(&inputList)->multitoken()
                , "set list of acquisition catalog#")
    ("measure,m", po::value<vector<string> >(&outputList)->multitoken()
                , "set list of measurement catalog#")
    ;
  // Group mandatory & generic options
  po::options_description cmdLineOpt("Allowed options") ;
  cmdLineOpt.add(generic).add(mandatory) ;
  // Simulate command line sample
  vector<string> args =
    po::split_winmain("-r Test --acq 11 tt 13 --measure 20 21 23");
  // Parse command line
  po::variables_map vm ;
  po::store(po::command_line_parser(args).options(cmdLineOpt).run(),
vm);
  po::notify(vm) ;
  // Display options & args in command lines
  cout << "root args : \n" ;
  if( vm.count("root") ) cout << '\t' << vm["root"].as<string>() <<
'\n' ;
  cout << "acq args : \n" ;
  if( vm.count("acq") ) {
    const vector<string>& vs = vm["acq"].as<vector<string> >() ;
    for_each(vs.begin(), vs.end(),
             cout << constant('\t') << _1 << constant('\n')) ;
  }
  cout << "measure args: \n" ;
  if( vm.count("measure") ) {
    const vector<string>& vs = vm["measure"].as<vector<string> >() ;
    for_each(vs.begin(), vs.end(),
             cout << constant('\t') << _1 << constant('\n')) ;
  }
  return 0 ;
}
--------------------------------------------------------------
--------------------------------------------------------------
Output results:
--------------------------------------------------------------
root args :
        Test
acq args :
        11
        tt
        13
        --measure (it seems to insert also option item w/ its args?)
        20
        21
        23
measure args:
 



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