Boost logo

Boost Users :

Subject: [Boost-users] program_options positional and required arguments
From: Yang Zhang (yanghatespam_at_[hidden])
Date: 2009-11-09 02:44:39


More usability issues with program_options: I couldn't really find
much mention of positional options in the docs. I'm writing a typical
command line program: one that takes some set of optional options and
a single required positional argument. Currently, I'm doing (where
namespace po = boost::program_options):

  po::options_description desc("Allowed options");
  desc.add_options()
    ("help,h", "show this help message")
    ...
    ("exe", po::wvalue<vector<wstring> >(), "the command line to execute");

  po::positional_options_description posopts;
  posopts.add("exe", -1);

  po::variables_map vm;
  po::store(po::wcommand_line_parser(argc, argv).
            options(desc).positional(posopts).run(), vm);
  po::notify(vm);
  if (vm.count("help") > 0) { cout << desc << endl; return 0; }
  ...
  if (vm.count("exe") == 0) { cerr << desc << endl; return 1; }
  wstring exe(vm["exe"].as<vector<wstring> >()[0]);

I had to add the exe option to the regular (non-positional)
options_description as a vector<wstring>, causing it to show up as an
option in the usage message. Omitting it resulted in errors about the
option being unregistered (even after trying to add
allow_unregistered). Why would I get this error? Shouldn't the
wcommand_line_parser::run method be doing this checking after it has
collected all the sets of option descriptions? Can I get rid of this?
What's the proper way to express what I want?

Is there a way to require the positional argument occur (1) at least
once and (2) at most once (besides manual checks after parsing)? The
configuration of positional options doesn't seem to be quite as rich
as those for normal options; is this accurate?

Thanks.

-- 
Yang Zhang
http://www.mit.edu/~y_z/

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