Krzysiek Karbowiak
by Krzysiek Karbowiak

Categories

  • docs
  • Programming

Tags

  • cpp-argparse
  • c++17

I am pleased to announce a new release of cpp-argparse. The new version is 0.8.3 and this is the first public release.

Of course all the previous releases were public as well, as this project has been open source since its inception. However, this is the first release that I announce on this blog.

What is it?

In short, cpp-argparse is a reimplementation of the Python’s argparse module. It is a header-only, single-include library with no external dependencies. It just uses STL and requires C++17. It is released under MIT license.

It aims to deliver the same ease of use and familiarity as the Python version.

I say reimplementation and not a port, as I did not take Python source code and painstakingly rewrote it in C++. To the contrary, I started from scratch and made design and implementation based on documentation and a few experiments.

What can it do?

As of yet, cpp-argparse does not provide the full functionality of its Python counterpart. However, it covers almost every feature that I used more than once (except mutually exclusive groups) and then some.

You can find the details in the readme section. Here, I will just recap the major points.

The ArgumentParser class supports setting prog, usage, description, epilog, and add_help parameters.

The ArgumentParser::add_argument function supports setting action, nargs, const, default, type, choices, required, help, metavar, and dest parameters.

The parser automatically adds the -h/--help optional argument, generates help message, and handles parsing errors.

How do I use it?

You want to try it out? Great! Just take the argparse.h header file, store it somewhere and point your build system to it.

A simple application for copying files around might use cpp-argparse like this:

#include "argparse.h"


int main(int argc, char * argv[])
{
    auto parser = argparse::ArgumentParser()
        .prog("filecopy")
        .description("A simple utility for copying files");

    parser.add_argument("src").help("source file path");
    parser.add_argument("dst").help("destination file path");
    parser.add_argument("-o", "--overwrite")
        .action(argparse::store_true)
        .help("overwrite the destination if it exists");
    parser.add_argument("-v", "--verbose")
        .action(argparse::store_true)
        .help("print additional information");

    auto args = parser.parse_args(argc, argv);

    auto src = args.get_value("src"); // get_value returns a std::string
    auto dst = args.get_value("dst");
    auto overwrite = args.get_value<bool>("overwrite"); // get_value<T> returns a T
    auto verbose = args.get_value<bool>("verbose");

    ...
}

The above program generates the following help message automatically:

usage: filecopy [-h] [-o] [-v] src dst

A simple utility for copying files

positional arguments:
  src                   source file path
  dst                   destination file path

optional arguments:
  -h                    show this help message and exit
  -o                    overwrite the destination if it exists
  -v                    print additional information

To do

While this version is pretty usable and stable, it is far from being complete. In fact it might never get to that point.

Since this version does not cover the whole functionality, I could not call it 1.0.0. I admit that when I was starting, I did not plan on what to implement and what to skip, and therefore the current version number is pretty arbitrary.

However, I do plan on adding mutually exclusive groups and fixing any bugs that I encounter.