Skip to content

dead-tech/argparse-cpp

Repository files navigation

Build Status Deploy Status pre-commit.ci status

argparse-cpp

Argument Parser in C++ inspired by python's argparse module.

Table of contents

Example

Simple example program that emulates an hypothetically compiler CLI.

#include <argparse/argparse.hpp>
#include <fstream>
#include <iostream>
#include <sstream>

int main(int argc, const char **argv)
{
    argparse::ArgumentParser parser(argc, argv);

   parser.add_argument("files")
         .set_type(argparse::ArgTypes::STRING)
         .set_default("test.txt")
         .set_help("Paths to the files to compile")
         .set_flags(argparse::ArgFlags::REQUIRED)
         .set_metavar("FILE_PATH")
         .set_nargs('+');

   parser.add_argument("--release", "-R")
         .set_type(argparse::ArgTypes::BOOL)
         .set_help("Build in release version");

   const auto args = parser.parse_args();

   const auto files = args.at("files").as<std::vector<std::string>>();
   const auto is_release = args.at("--release").as<bool>();

   const auto result = build_files(files, is_release);
}

Quickstart

Using conan package manager

Starting from release v0.1.0 it is possible to use conan to install the header only library from the artifactory remote.

  • Create a simple conanfile.txt in the root of your project

    [requires]
      argparse-cpp/0.1.1@dead/stable
    
    [generators]
      cmake
  • Move into directory where you want to build your project

    $ cd <build-dir>
  • Add the artifactory remote to your conan profile

    $ conan remote add argparse-cpp https://argparsecpp.jfrog.io/artifactory/api/conan/argparse-cpp-conan-local
  • Run conan install

    $ conan install .. -r argparse-cpp

Download source code from latest release

Click on the releases header on the right side of the GitHub page or navigate here.

Scroll down to the assets section and download the source code (zip or tar.gz).

Downloading the header file

  • Get the file through wget
    $ wget https://raw.githubusercontent.com/dead-tech/argparse-cpp/main/include/argparse/argparse.hpp

Cloning the repo

  • Clone the repo
    $ git clone https://github.com/dead-tech/argparse-cpp.git
    $ cd argparse-cpp
  • Move the header file
    $ mv include/argparse.hpp <your-project-include-path>

Documentation

Documentation can be found here.

Testing

This project uses catch2 as a testing framework.

All the available tests can be found here.

The following steps are what is required to run the test suite.

$ git clone https://github.com/dead-tech/argparse-cpp.git
$ cd argparse-cpp
$ mkdir build && cd build
$ conan install ..
$ cmake .. && make
$ ./argparse-cpp_tests

If these steps do not work properly for you see the official docs.

License

The project is licensed under MIT License (see LICENSE).