Boost logo

Boost :

From: peter.nordlund_at_[hidden]
Date: 2001-11-18 18:28:18


--- In boost_at_y..., "Gennadiy E. Rozental" <rogeeff_at_m...> wrote:
> If it do not cost too much efforts I would implement it. Though I am
> personally never use this form of CLA.
>
> Gennadiy.

Hi Gennadiy,

I have a command line parser that maybe could be of interest,
or act as some kind of inspiration.
I will upload it on request.

This command line parser can be configured whether partial
matches is enough or if only complete matches should be allowed.

It can also be configured whether args must be supplied.
Args that must be supplied are marked with a * in the help text.

Default values can be given.

Option converters for the most common data-types are available,
and it is possible to add new converters for more complex data-types.

Output from running the program "testopts" for which also the
source-code is supplied, see below:
>> ./testopts -usage
  Usage: ./testopts [-enu3 enum] [-ov5 vector] [-b2 ] [-help ]
[-usage ] [-int1 arg] [-int2 arg] [-int4 arg] [-float1 arg]
[-string arg]
---------------------------------------------------------------------
>> ./testopts -help

  Usage: ./testopts [options]
        * -enu3 enum An enum option
                                    Alternatives:a, b, c
          -ov5 vector A vector option ("1 2 3 ")
          -b2 A bool option (0)
          -help A help option
          -usage A usage option
          -int1 arg An integer option, no default value
                                  given
          -int2 arg Another integer option with default
                                  value (42)
        * -int4 arg Another integer option, must be
                                  supplied
          -float1 arg A floating point option (3.7)
          -string arg A string option (Foo)
---------------------------------------------------------------------
>>./testopts2 -enu3 a -int4 9 -int1 7 -yxxxx qqqq
-b2: 0
-int1: 7
-float1: 3.7
-string: Foo
-enu3: 1

Not consumed arguments:
./testopts2
-yxxxx
qqqq
-------------
i2.wasSupplied():0

//----------------------------------------------
#include <Opts/OptsVector.h>
#include <Opts/OptsEnum.h>
#include <iostream>

using namespace Options;

enum testEnum {a=1,b=2,c=4};

int main (int argc, char **argv)
{
  std::map<std::string,int> inmap;
  inmap["a"] = testEnum(a);
  inmap["b"] = testEnum(b);
  inmap["c"] = testEnum(c);

  // Configure whether partial matches or nonmatching args
  // shall be allowed.
  Opts op(Opts::AllowNonMatchingArgs);

  Option<Enum<testEnum> > oe3(op, OptArg("-enu3"),
                              EnumConverter<Enum<testEnum> >
                              ("An enum option", inmap),
                              Opts::MustBeSupplied);
  Option<std::vector<int> > ov3(op, OptArg("-ov5"),
                                "A vector option", "1 2 3");
  Option<bool> b2(op, OptArg("-b2"), "A bool option");
  Option<Help> h(op, OptArg("-help"), "A help option");
  Option<Usage> u(op, OptArg("-usage"), "A usage option");
  Option<int> i1(op, OptArg("-int1"),
                 "An integer option, no default value given");
  Option<int> i2(op, OptArg("-int2"),
                 "Another integer option with default value", 42);
  Option<int> i4(op, OptArg("-int4"),
                 "Another integer option, must be supplied",
                 Opts::MustBeSupplied);
  Option<float> f1(op, OptArg("-float1"),
                   "A floating point option", 3.7);
  Option<std::string> s1(op, OptArg("-string"),
                         "A string option", "Foo");

  op.parse(argc, argv); // Parse command line

  std::cout << "-b2: " << b2 << std::endl;
  std::cout << "-int1: " << i1 << std::endl;
  std::cout << "-float1: " << f1 << std::endl;
  std::cout << "-string: " << std::string(s1) << std::endl;
  std::cout << "-enu3: " << oe3 << std::endl;
  op.printNotConsumed();
  std::cerr << "-------------" << std::endl;

  std::cerr << "i2.wasSupplied():" << i2.wasSupplied() << std::endl;
  return 0;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk