2020-03-24 12:53:11 -05:00
2020-03-24 12:31:40 -05:00
2020-03-24 12:53:00 -05:00
2020-03-24 08:18:43 -05:00
2020-03-24 08:18:43 -05:00
2020-03-24 08:18:43 -05:00
2020-03-24 08:27:23 -05:00
2020-03-24 12:53:00 -05:00
2020-03-24 12:53:11 -05:00

argparse

Build Status

Simple single-file header-only CLI option parsing for C++. No subcommands or grouped commands. Only supports -s value/--long-option value style for options. Only supports --long-flag style for flags.

Getting Started

Download the latest argparse.hpp and include it in your project.

Features

  • allow (default) / disallow (Parser::no_unrecognize()) unrecognized options and flags
  • optional/required (PosnlBase::required()) positional arguments
  • flags with -s, --long-flag formats
  • options with --long-opt val format
  • positional arguments
  • -- to stop parsing options and flags
  • modify argc/argv (disable with Parser::no_consume())
  • Option/Positional Argument Types
    • int
    • size_t
    • float
    • double
    • std::string

Examples

#include "argparse/argparse.hpp"

int main(int argc, char **argv) {

  // A parser object
  Parser p;

  // Program data corresponding to flags, options, and positional arguments
  bool verbose = false;
  std::string toPrint;
  std::string maybePrint;
  int repeats = 1;

  // Inform the parser of the program data.
  // It will do type-specific conversion
  p.add_option(repeats, "--repeat");
  p.add_flag(verbose, "--verbose", "-v");
  p.add_positional(toPrint)->required();
  auto psnl = p.add_positional(maybePrint);

  // If there was an error during parsing, report it.
  if (!p.parse(argc, argv)) {
    std::cerr << p.help();
    exit(EXIT_FAILURE);
  }

  // Execute the program logic
  if (verbose) {
    std::cerr << "about to print '" << toPrint << "' " << repeats << " times";
    if (psnl->found()) {
      std::cerr << ", then '" << maybePrint << "'";      
    }
    std::cerr << std::endl;
  }

  for (int i = 0; i < repeats; ++i) {
    std::cout << toPrint;
  }
  if (psnl->found()) {
    std::cout << maybePrint;
  }
  std::cout << std::endl;

  return 0;
}

Roadmap

  • Reject duplicate flags / options
  • Help string output
  • support --long-option=value
  • have the last positional argument fill a vector with remaining
Description
Limited single-header-only CLI parsing for C++ without std::regex
Readme Apache-2.0 175 KiB
Languages
C++ 89.6%
CMake 7%
Shell 3.4%