Code Monkey home page Code Monkey logo

argz's Introduction

argz

A light weight C++ in memory argument parser.

Highlights

  • Single header file
  • Requires C++20
  • Less than 200 lines of code
  • Apache 2.0 License

Quick Start

Simply include argz.hpp and you're good to go.

#include <argz/argz.hpp>

Start with an about: argz::about

constexpr std::string_view version = "1.2.3";
argz::about about{ "My program description", version };

To add a new argument, simply create argz::options. Provide a list of argument names that you want to group together, e.g. -i and --input.

std::string input{};
std::string study{};
int number = 123;
bool boolean = true;
std::optional<int> number_opt{};
argz::options opts{
   { { "input", 'i' }, input, "the input file"},
   { { "study", 's' }, study, "a study file"},
   { { "number" }, number, "input an int"},
   { { "boolean" }, boolean, "a boolean" },
   { { "number_opt" }, number_opt, "input an int"}
};

Now to parse command line arguments:

try {
    argz::parse(about, opts, argc, argv);
}
catch (const std::exception& e) {
    std::cerr << e.what() << '\n';
}

That is all!

Note that we don't have to cast out (.e.g. .as<int>) from our argument parser because it reads directly into the variables passed in as options!

Printing Help

-h prints a help message, including the program usage and information about the arguments registered with the Argz Parser. An example help message:

My program description
Version: 1.2.3

-h, --help              write help to console
-v, --version           write the version to console
-i, --input             the input file
-s, --study             a study file
--number                input an int, default: 123
--boolean               a boolean, default: 1

Supported Input Types

bool, int32_t, uint32_t, int64_t, uint64_t, std::string

And std::optional<T> where T is any of the above types except bool

Accept No Inputs Without Printing Help

By default the help is printed when no inputs are given. If this behavior is not desirable, set print_help_when_no_options to false inside of argz::about.

argz::about about{ "My program description", version,
                 .print_help_when_no_options = false };

argz's People

Contributors

stephenberry avatar erikrush avatar mtygesen 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.