Giter Club home page Giter Club logo

optparse's Introduction

optparse

Build status

This is yet another option parser for C++. It is modelled after the excellent Python optparse API. Although incomplete, anyone familiar to optparse should feel at home: https://docs.python.org/library/optparse.html

Copyright (C) 2010 Johannes Weißl <[email protected]>

License: your favourite BSD-style license

Design decisions

  • elegant and easy usage more important than speed / flexibility
  • shortness more important than feature completeness
    • no unicode
    • no checking for user programming errors

Why not use getopt/getopt_long?

  • not C++ / not completely POSIX
  • too cumbersome to use, would need lot of additional code

Why not use Boost.Program_options?

  • boost not installed on all target platforms (esp. cluster, HPC, ...)
  • too big to include just for option handling: 322 *.h (44750 lines) + 7 *.cpp (2078 lines)

Why not use tclap/Opag/Options/CmdLine/Anyoption/Argument_helper/...?

  • no reason, writing one is faster than code inspection :-)
  • similarity to Python desired for faster learning curve

Future work

  • nargs > 1?
  • comments?

Example

#include <optparse.h>

#include <iostream>
#include <string>
#include <vector>


int main(int argc, char **argv)
{
    optparse::OptionParser parser =
        optparse::OptionParser().description("just an example");

    parser.add_option("-f", "--file").dest("filename")
          .help("write report to FILE").metavar("FILE");
    parser.add_option("-q", "--quiet")
          .action("store_false")
          .dest("verbose")
          .set_default("1")
          .help("don't print status messages to stdout");

    const optparse::Values options = parser.parse_args(argc, argv);
    const std::vector<std::string> args = parser.args();

    if (options.get("verbose"))
    {
        std::cout << options["filename"] << "\n";
    }
}

optparse's People

Contributors

gonoph avatar myint avatar nova80 avatar toebbel avatar weisslj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

optparse's Issues

Don't std::exit() on error

In the current implementation the optparse just stops the whole program on any parsing error (as well as after processing --help and --version options). This may be unacceptable, when some cleanup code should be executed before exiting, in parallel MPI environment, etc.

I propose to implement some more mild way to handle parser errors. I think about 3 possibilities:

  • throw an exception instead of exiting
  • set some internal flag e.g. bool optparse::is_valid that can be checked explicitly
  • replace any calls of std::exit() by optparse::exit() method and make it virtual to allow overriding.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.