GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Categories
  • Newsletter
  • Submit
  • Login
  • Awesome C++
  • CLI

This view

  • Original page
  • New address

GeezerVison is showing cpp.libhunt.com โ€” jarro2783/cxxopts Alternatives - C++ CLI | LibHunt

  • Subscribe
  • Submit
  • Categories
  • About
  • Login

jarro2783/cxxopts

Lightweight C++ command line option parser

jarro2783 logo
Source Code    Changelog
Suggest Changes
Take the C++ Developer Survey - About 6 minutes on the tools, libraries and AI you use with C++. You get the results by email. Take the survey ยป
Popularity
7.9
Stable
Activity
6.6
Growing
Stars 4,830
Watchers 52
Forks 676
Last Commit about 20 hours ago

Programming language: C++
License: MIT License
Tags: CLI    
Latest version: v3.0

jarro2783/cxxopts alternatives and similar libraries

Based on the "CLI" category.
Alternatively, view jarro2783/cxxopts alternatives based on common mentions on social networks and blogs.

  • Vcpkg

    9.7 10.0 jarro2783/cxxopts VS Vcpkg
    C++ Library Manager for Windows, Linux, and MacOS
    microsoft logo
  • conan

    8.8 9.6 jarro2783/cxxopts VS conan
    Conan - The open-source C and C++ package manager
    conan-io logo
  • SaaSHub - Software Alternatives and Reviews

    SaaSHub helps you find the best software and product alternatives
    Promo www.saashub.com
    SaaSHub Logo
  • CLI11

    7.8 8.8 jarro2783/cxxopts VS CLI11
    CLI11 is a command line parser for C++11 and beyond that provides a rich feature set with a simple and intuitive interface.
    CLIUtils logo
  • gflags

    7.6 5.8 L2 jarro2783/cxxopts VS gflags
    The gflags package contains a C++ library that implements commandline flags processing. It includes built-in support for standard types such as string and the ability to define flags in the source file in which they are used. Online documentation available at:
    gflags logo
  • indicators

    7.3 5.2 jarro2783/cxxopts VS indicators
    Activity Indicators for Modern C++
    p-ranav logo
  • termbox

    6.4 0.0 L4 jarro2783/cxxopts VS termbox
    Library for writing text-based user interfaces
    nsf logo
  • args

    6.1 8.5 L1 jarro2783/cxxopts VS args
    A header-only C++ argument parser library. Supposed to be flexible and powerful, and attempts to be compatible with the functionality of the Python standard argparse library (though not necessarily the API).
    Taywee logo
  • Taywee/args

    6.1 8.5 L1 jarro2783/cxxopts VS Taywee/args
    A header-only C++ argument parser library. Supposed to be flexible and powerful, and attempts to be compatible with the functionality of the Python standard argparse library (though not necessarily the API).
    Taywee logo
  • rang

    6.0 6.2 L4 jarro2783/cxxopts VS rang
    A Minimal, Header only Modern c++ library for terminal goodies ๐Ÿ’„โœจ
    agauniyal logo
  • Argh!

    5.6 2.4 jarro2783/cxxopts VS Argh!
    Argh! A minimalist argument handler.
    adishavit logo
  • clipp

    5.6 0.0 jarro2783/cxxopts VS clipp
    easy to use, powerful & expressive command line argument parsing for modern C++ / single header / usage & doc generation
    muellan logo
  • docopt.cpp

    5.4 0.0 L3 jarro2783/cxxopts VS docopt.cpp
    C++11 port of docopt
    docopt logo
  • Ncurses

    4.5 1.2 jarro2783/cxxopts VS Ncurses
    ncurses Git mirror
    mirror logo
  • Boost.Program_options

    3.4 3.8 L3 jarro2783/cxxopts VS Boost.Program_options
    Boost.org program_options module
    boostorg logo
  • cifuzz

    3.3 6.8 jarro2783/cxxopts VS cifuzz
    DISCONTINUED. cifuzz makes fuzz tests as easy as unit tests
    CodeIntelligenceTesting logo
  • GuiarStr

    1.2 8.2 jarro2783/cxxopts VS GuiarStr
    A lightweight, dependency-free C library for UTF-8-safe and advanced string manipulation: trim, split, reverse, contains, count, and more.
    okba14 logo
  • TCLAP

    - jarro2783/cxxopts VS TCLAP
    A mature, stable and feature-rich library for defining and accessing command line arguments in ANSI C++. [MIT]

* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.

Do you think we are missing an alternative of jarro2783/cxxopts or a related project?

Add another 'CLI' Library

Popular Comparisons

  • jarro2783/cxxopts
    vs
    CLI11
  • jarro2783/cxxopts
    vs
    Boost.Program_options
  • jarro2783/cxxopts
    vs
    gflags
  • jarro2783/cxxopts
    vs
    clipp
  • jarro2783/cxxopts
    vs
    TCLAP

README

Build Status

Release versions

Note that master is generally a work in progress, and you probably want to use a tagged release version.

Version 3 breaking changes

If you have used version 2, there are a couple of breaking changes in version 3 that you should be aware of. If you are new to cxxopts you can skip this section.

The parser no longer modifies its arguments, so you can pass a const argc and argv and expect them not to be changed.

The ParseResult object no longer depends on the parser. So it can be returned from a scope outside the parser and still work. Now that the inputs are not modified, ParseResult stores a list of the unmatched arguments. These are retrieved like follows:

auto result = options.parse(argc, argv);
result.unmatched(); // get the unmatched arguments

Quick start

This is a lightweight C++ option parser library, supporting the standard GNU style syntax for options.

Options can be given as:

--long
--long=argument
--long argument
-a
-ab
-abc argument

where c takes an argument, but a and b do not.

Additionally, anything after -- will be parsed as a positional argument.

Basics

#include <cxxopts.hpp>

Create a cxxopts::Options instance.

cxxopts::Options options("MyProgram", "One line description of MyProgram");

Then use add_options.

options.add_options()
  ("d,debug", "Enable debugging") // a bool parameter
  ("i,integer", "Int param", cxxopts::value<int>())
  ("f,file", "File name", cxxopts::value<std::string>())
  ("v,verbose", "Verbose output", cxxopts::value<bool>()->default_value("false"))
  ;

Options are declared with a long and an optional short option. A description must be provided. The third argument is the value, if omitted it is boolean. Any type can be given as long as it can be parsed, with operator>>.

To parse the command line do:

auto result = options.parse(argc, argv);

To retrieve an option use result.count("option") to get the number of times it appeared, and

result["opt"].as<type>()

to get its value. If "opt" doesn't exist, or isn't of the right type, then an exception will be thrown.

Note that the result of options.parse should only be used as long as the options object that created it is in scope.

Unrecognised arguments

You can allow unrecognised arguments to be skipped. This applies to both positional arguments that are not parsed into another option, and -- arguments that do not match an argument that you specify. This is done by calling:

options.allow_unrecognised_options();

and in the result object they are retrieved with:

result.unmatched()

Exceptions

Exceptional situations throw C++ exceptions. There are two types of exceptions: errors defining the options, and errors when parsing a list of arguments. All exceptions derive from cxxopts::exceptions::exception. Errors defining options derive from cxxopts::exceptions::specification and errors parsing arguments derive from cxxopts::exceptions::parsing.

All exceptions define a what() function to get a printable string explaining the error.

Help groups

Options can be placed into groups for the purposes of displaying help messages. To place options in a group, pass the group as a string to add_options. Then, when displaying the help, pass the groups that you would like displayed as a vector to the help function.

Positional Arguments

Positional arguments are those given without a preceding flag and can be used alongside non-positional arguments. There may be multiple positional arguments, and the final positional argument may be a container type to hold a list of all remaining positionals.

To set up positional arguments, first declare the options, then configure a set of those arguments as positional like:

options.add_options()
  ("script", "The script file to execute", cxxopts::value<std::string>())
  ("server", "The server to execute on", cxxopts::value<std::string>())
  ("filenames", "The filename(s) to process", cxxopts::value<std::vector<std::string>>());

options.parse_positional({"script", "server", "filenames"})

Then parsing a set of arguments like:

my_script.py my_server.com file1.txt file2.txt file3.txt

will result in parsed arguments like the following table:

Field Value
"script" "my_script.py"
"server" "my_server.com"
"filenames" {"file1.txt", "file2.txt", "file3.txt"}

Default and implicit values

An option can be declared with a default or an implicit value, or both.

A default value is the value that an option takes when it is not specified on the command line. The following specifies a default value for an option:

cxxopts::value<std::string>()->default_value("value")

An implicit value is the value that an option takes when it is given on the command line without an argument. The following specifies an implicit value:

cxxopts::value<std::string>()->implicit_value("implicit")

If an option had both, then not specifying it would give the value "value", writing it on the command line as --option would give the value "implicit", and writing --option=another would give it the value "another".

Note that the default and implicit value is always stored as a string, regardless of the type that you want to store it in. It will be parsed as though it was given on the command line.

Default values are not counted by Options::count.

Boolean values

Boolean options have a default implicit value of "true", which can be overridden. The effect is that writing -o by itself will set option o to true. However, they can also be written with various strings using =value. There is no way to disambiguate positional arguments from the value following a boolean, so we have chosen that they will be positional arguments, and therefore, -o false does not work.

std::vector<T> values

Parsing of list of values in form of an std::vector<T> is also supported, as long as T can be parsed. To separate single values in a list the definition CXXOPTS_VECTOR_DELIMITER is used, which is ',' by default. Ensure that you use no whitespaces between values because those would be interpreted as the next command line option. Example for a command line option that can be parsed as a std::vector<double>:

--my_list=1,-2.1,3,4.5

Options specified multiple times

The same option can be specified several times, with different arguments, which will all be recorded in order of appearance. An example:

--use train --use bus --use ferry

this is supported through the use of a vector of value for the option:

options.add_options()
  ("use", "Usable means of transport", cxxopts::value<std::vector<std::string>>())

Custom help

The string after the program name on the first line of the help can be completely replaced by calling options.custom_help. Note that you might also want to override the positional help by calling options.positional_help.

Example

Putting all together:

int main(int argc, char** argv)
{
    cxxopts::Options options("test", "A brief description");

    options.add_options()
        ("b,bar", "Param bar", cxxopts::value<std::string>())
        ("d,debug", "Enable debugging", cxxopts::value<bool>()->default_value("false"))
        ("f,foo", "Param foo", cxxopts::value<int>()->default_value("10"))
        ("h,help", "Print usage")
    ;

    auto result = options.parse(argc, argv);

    if (result.count("help"))
    {
      std::cout << options.help() << std::endl;
      exit(0);
    }
    bool debug = result["debug"].as<bool>();
    std::string bar;
    if (result.count("bar"))
      bar = result["bar"].as<std::string>();
    int foo = result["foo"].as<int>();

    return 0;
}

Linking

This is a header only library.

Requirements

The only build requirement is a C++ compiler that supports C++11 features such as:

  • regex
  • constexpr
  • default constructors

GCC >= 4.9 or clang >= 3.1 with libc++ are known to work.

The following compilers are known not to work:

  • MSVC 2013

TODO list

  • Allow unrecognised options.

Do not miss the trending, libraries, news and articles with our weekly report.

Awesome C++ is part of the LibHunt network. Terms. Privacy Policy.

(CC)
BY-SA
Spin The Wheel Of Names for a cryptographically secure random name picker.
Schedule a when to meet poll.
You are visitor 01402792 โ€” GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800ร—600. No cookies required.