GithubHelp home page GithubHelp logo

rwpenney / bpomodes Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 34 KB

Command-line parsing for subcommands using boost::program_options

License: Boost Software License 1.0

CMake 5.58% C++ 94.42%
boost command-line-parser subcommand-parsing

bpomodes's Introduction

Boost program-options subcommand parser

This is a simple wrapper for boost::program_options which allows basic handling of "subcommands" where a single C++ application provides multiple operating modes which each have their own configuration options. It is analogous to the add_subparsers() functionality of Python's argparse module.

The supplied demo application illustrates basic usage of the library and handles command-line combinations such as:

./bpo-demo --help
./bpo-demo -h one
./bpo-demo --loglevel 17 two
./bpo-demo --logfile some_path.txt three --counter 31

The -h or --help options are automatically inserted into the common options, and generate mode-specific messages such as:

bpomodes demo:
  -L [ --logfile ] arg (=/dev/null) location of logfile
  --loglevel arg (=1)               logging level
  -h [ --help ]                     Show usage information
  [one|three|two]
  <subcommand_args> ...

mode three:
  -c [ --counter ] arg (=0) how many things to count

Count in integer steps

BpoModes was originally influenced by A.Rankine's GitHub gist.

Usage

The library consists of a single C++11 class, BpoModes, which manages a collection of boost::program_options::options_description objects, one for each subcommand, and another that represent options shared by all subcommands. In its simplest form, one simply populates each of these options_description objects and passes them to BpoModes::add(), and then calls the parse() method to generate a variables_map:

using namespace boost::program_options;
options_description generic_options, options_one, options_two;

generic_options.add_options()
    /* add generic and subcommand options */ ;

BpoModes parser(generic_options);

parser.add("subcommand_one", options_one);
parser.add("subcommand_two", options_two);

const auto varmap = parser.parse(argc, argv);

The field varmap["subcommand"].as<std::string>() will be populated with the name of the subcommand as passed to the BpoModes::add() method.

More sophisticated use-cases can use the third, optional, argument to BpoModes::add(), which should be a subclass of BpoModes::ModeHandler. That interface provides three customization points for setting up the underlying command_line_parser before it sees the vector of command-line arguments; or for providing an entry-point that can be called from main() to delegate processing of the subcommand.

The demo.cpp file shows more detail about how these components fit together. Running ./bpo-demo --help will show information about the available subcommands.

bpomodes's People

Contributors

rwpenney avatar

Watchers

 avatar

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.