Git Product home page Git Product logo

cpp-argparse's Introduction

cpp-argparse

A semi-faithful port of Python's argparse to C++ (still in progress)

Documentation

(This builds as low as C++11, I think)

First, you create a parser object with ArgumentParser parser("program name").

Then you create an Argument object arg (for example) with either the optional argument string flag or positional argument name, then change one of these class/instance members:

  • .alt - (string) Allows for adding a short flag option string
  • .nargs - (string) Defaults to "1", but can be set to an integer or one of:
    • ? - Accepts 0 or 1 arguments
    • * - Accepts 0 or more arguments
    • + - Accepts 1 or more arguments

Then you add the Argument object to the parser with parser.add_argument(arg)

Once your arguments are added to the parser, you can then use parser.parse_args(argc, argv) to parse your CLI's arguments. The first argument will be skipped, of course.

A setup for a multi-argument SOURCE, single-argument DEST program (examples/examples1.cpp) might look like:

#include <argparse.hpp>

int main(int argc, char* argv[]) {
  ArgumentParser parser("mycli");

  Argument arg("source");
  arg.nargs = "+";
  parser.add_argument(arg);

  arg = {"dest"};
  parser.add_argument(arg);

  ParsedResults results = parser.parse_args(argc, argv);

  std::cout << "source" << std::endl;
  for (int i = 0; i < results["source"].size(); ++i) {
    std::cout << "  " << results["source"].at(i) << std::endl;
  }

  std::cout << "dest" << std::endl;
  for (int i = 0; i < results["dest"].size(); ++i) {
    std::cout << "  " << results["dest"].at(i) << std::endl;
  }
}

Producing this output:

source
  one
  two
dest
  three

As a result of: ./a.out one two three

cpp-argparse's People

Contributors

h4cky54ck 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.