Boost logo

Boost Users :

From: The Grumpiest Man You Know (mrgrumpyguts_at_[hidden])
Date: 2005-02-20 17:34:56


As I saw elsewhere today, I'm new here.

On Sun, 20 Feb 2005 22:02:38 +0100, Julio M. Merino Vidal
<jmmv84_at_[hidden]> wrote:
> Hi everybody,
>
> I'm working on a program whose its actual syntax is:
>
> vcsme [-c conf_file] [-q] action [action_args]
>
> (very similar to the CVS one).
>
> I'm trying to convert it to use boost::program_options (to get rid of
> the getopt(3) call, and to have long options), but I don't know how to
> do it.

I have a call:
  desc.add_options()
    ("help,h", "Blab to the users")
That seems to allow --help and contractions, because of the "help,
and -h, because of the ,h". It seemed to me that it's "<long option
name>,<short option letter>"

> So far, I've added the code to handle -c and -q options. This was
> very easy to do, given the examples on the web. The only annoyance is
> that the options are accepted everywhere, but I'd prefer to only accept
> them if they come before the action name (because the action itself
> might allow options).

I was looking for something similar. I defined:

struct my_parser {
  my_parser() : found_subcmd(false) {};
  std::pair<std::string, std::string> operator() (const std::string & s) {
    found_subcmd = found_subcmd || (s[0] != '-');

    if (found_subcmd) {
      return make_pair(std::string("sub-cmd"), s);
    } else {
      return make_pair(std::string(), std::string());
    }
  };
  void reset() { found_subcmd = false; };

private:
  bool found_subcmd;
};

Then used it as the external parser.

  po::store(po::command_line_parser(argc, argv).options(desc)
            .extra_parser(cnos_cmd_parser()).positional(p).run(), vm);

This puts the pre magic word stuff in vm.

> Unfortunately, I'm stuck with the latest part, the one to handle
> actions. There must always be an action passed to the program, which
> may optionally accept arguments. Some example invocations:

I'd guess that you'd create another variable map and run a different
parser on the words in the vector sub-cmd.

But bear in mind the first line. ;)

----
Blue skies

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