Overview

ezOptionParser is another command-line parser class for C++ that has features not available in alternative solutions (getopt, boost, argtable, argstream, gflags) and doesn't require a steep learning curve.

Download

Source Code and Examples

ezOptionParser.hpp

Features

Examples

complete.cpp A complete example that could be used as a starting point for your own C++ program.

fileio.cpp Shows how to import and export options with files (that can contain comments!).

full.cpp A full test of all the features. Meant for testing, but can be a source of ideas for what's possible.

long.cpp Demo of using long flag names.

multi.cpp Shows how to handle multiple instances of a flag.

parseindex.cpp Demo of parsed indices for options, so you can create ordered contexts.

pretty.cpp Demo of pretty printing everything parsed, which can help in debugging.

short.cpp Short demo of a short flag name.

usage.cpp Demo of automatic usage message creation in three builtin layouts. Here are how the three layouts appear: aligned, interleaved, staggered

valid.cpp Demo of using validators defined by strings, which only check if values are within for their datatype limits.

validrange.cpp Demo of using validators with value ranges and lists defined by strings.

validfast.cpp Demo of using validators defined by constants for more efficient execution. These validators only check if values are within for their datatype limits..

validrangefast.cpp Demo of using validators with value ranges and lists defined by constants for more efficient execution.

Usage

Copy or include ezOptionParser.hpp to your project and use the "ez" namespace, as shown here:

// pretty.cpp
#include <stdio.h>
#include "ezOptionParser.hpp"

int main(int argc, const char * argv[]) {
  ez::ezOptionParser opt;

  opt.overview = "Demo of pretty printing everything parsed.";
  opt.syntax = "pretty [OPTIONS]";
  opt.example = "pretty foo bar --debug --dummy -list 1,2,16 in1 in2 out\n\n";
  opt.footer = "ezOptionParser (C) 2014\n";

  opt.add(
    "", // Default.
    0, // Required?
    0, // Number of args expected.
    0, // Delimiter if expecting multiple args.
    "Display usage instructions.", // Help description.
    "-h",     // Flag token. 
    "-help",  // Flag token.
    "--help", // Flag token.
    "--usage" // Flag token.
  );

  opt.add(
    "", // Default.
    0, // Required?
    0, // Number of args expected.
    0, // Delimiter if expecting multiple args.
    "Print all inputs and categories for debugging.", // Help description.
    "--debug"     // Flag token. 
  );

  opt.parse(argc, argv);

  if (opt.isSet("-h")) {
    std::string usage;
    opt.getUsage(usage);
    std::cout << usage;
    return 1;
  }

  if (opt.isSet("--debug")) {
    std::string pretty;
    opt.prettyPrint(pretty);
    std::cout << pretty;
  }

  return 0;
}

Here is the auto-generated usage message:

./pretty -h
Demo of pretty printing everything parsed.

USAGE: pretty [OPTIONS]

OPTIONS:

-h, -help, --help, --usage   Display usage instructions.

--debug                      Print all inputs and categories for debugging.

EXAMPLES:

pretty foo bar --debug --dummy -list 1,2,16 in1 in2 out

ezOptionParser (C) 2014

Testing

make
make memtest
make clean

Installation

sudo make install PREFIX=/usr/local

Compiling

Jose Santiago suggests these compiler options to get a warning-free compile with g++-4.5.2 and ezOptionParser-0.2.2:

g++ O6  -ggdb  -ansi   -Wall  -Wextra -Wwrite-strings -Wcast-align \
-Wpointer-arith -Winit-self -Wuninitialized -Wformat=2 \
-Woverloaded-virtual -Wstrict-null-sentinel -Wnon-virtual-dtor \
-fno-omit-frame-pointer -fno-eliminate-unused-debug-symbols \
-fno-tree-vectorize -Wstrict-aliasing -fstrict-aliasing

Distribution

make html
make clean
make dist VER=0.2.2

Publishing

ssh -t rsz,ezoptionparser@shell.sourceforge.net create 
scp html/* ezOptionParser.hpp rsz,ezoptionparser@shell.sourceforge.net:/home/project-web/ezoptionparser/htdocs
scp ../ezOptionParser-0.2.2.tar.gz rsz,ezoptionparser@shell.sourceforge.net:/home/frs/project/e/ez/ezoptionparser

Changelog

v0.2.2 (20140504)

v0.2.1 (20130506)

v0.2.0 (20121120)

v0.1.4 (20120629)

v0.1.3 (20120603)

v0.1.2 (20111126)

v0.1.1 (20111011)

v0.1.0 (20111011)

v0.0.0 (20110511)

License

Copyright 2011, 2012, 2014 Remik Ziemlinski (see MIT-LICENSE)