Loading repository data…
Loading repository data…
gershnik / repository
Fully-featured, powerful, command line argument parser in C++20
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
Fully-featured, powerful and simple-to-use C++ command-line argument parser.
git or clang using this library. See Syntax Description on the wiki for details on accepted syntax and configurability.- with /". You can specify what prefixes to use on short options and long options, which prefixes are equivalent, disable short or long option behaviors altogether, what separator to use for arguments instead of =, and more.
+ as a short option prefix in addition to -).boost::outcome or the proposed std::expected).
Two equivalent examples that demonstrate many of the most important features of the library can be found at:
A very minimalistic example to show "what does it look like" is below:
#include <argum.h>
#include <iostream>
using namespace Argum;
using namespace std;
int main(int argc, char * argv[]) {
const char * progname = (argc ? argv[0] : "prog");
int optionValue = 0;
std::string positionalArg;
Parser parser;
parser.add(
Option("--help", "-h").
help("show this help message and exit").
handler([&]() {
cout << parser.formatHelp(progname);
exit(EXIT_SUCCESS);
}));
parser.add(
Option("--option", "-o").
argName("VALUE").
help("an option with a value").
handler([&](const string_view & value) {
optionValue = parseIntegral<int>(value);
}));
parser.add(
Positional("positional").
help("positional argument").
handler([&](const string_view & value) {
positionalArg = value;
}));
try {
parser.parse(argc, argv);
} catch (ParsingException & ex) {
cerr << ex.message() << '\n';
cerr << parser.formatUsage(progname) << '\n';
return EXIT_FAILURE;
}
std::cout << "option value: " << optionValue << '\n';
std::cout << "positional argument: " << positionalArg << '\n';
}
More sample code can be found on the Wiki.
You can integrate Argum into your code in the following ways:
Download single-file/argum.h from this repo or the Releases page.
Drop it into your project and #include it. This is the simplest way.
If you have a compiler and build system that support modules, you can try to use an experimental module file. Download single-file/argum-module.ixx from this repo or the Releases page and integrate it into your project.
Note that, of the compilers I have access to, only MSVC 2026 currently supports modules to any usable extent and is capable of using that module file.
With modern CMake you can easily integrate Argum as follows:
include(FetchContent)
FetchContent_Declare(argum
GIT_REPOSITORY git@github.com:gershnik/argum.git
GIT_TAG <desired tag like v2.5>
GIT_SHALLOW TRUE
)
FetchContent_MakeAvailable(argum)
...
target_link_libraries(mytarget
PRIVATE
argum::argum
)
Alternatively, you can clone this repository somewhere and do this:
add_subdirectory(PATH_WHERE_YOU_DOWNLOADED_IT_TO, argum)
...
target_link_libraries(mytarget
PRIVATE
argum::argum
)
You can also build and install this library on your system using CMake.
cd SOME_PATH
cmake -S . -B build
cmake --build build
#Optional
#cmake --build build --target run-test
#install to /usr/local
sudo cmake --install build
#or for a different prefix
#cmake --install build --prefix /usr
Once the library has been installed, it can be used in the following ways:
Set the include directory to <prefix>/include where <prefix> is the install prefix from above.
find_package(argum)
target_link_libraries(mytarget
PRIVATE
argum::argum
)
pkg-configAdd the output of pkg-config --cflags argum to your compiler flags.
Note that the default installation prefix /usr/local might not be in the list of places your
pkg-config looks into. If so, you might need to do:
export PKG_CONFIG_PATH=/usr/local/share/pkgconfig
before running pkg-config.
Whichever method you use in order to use Argum, your compiler needs to be set to C++20 mode or above. Argum should compile cleanly even at the highest warning level.
If you don't use CMake, on MSVC you need to:
_CRT_SECURE_NO_WARNINGS macro defined to avoid its bogus "deprecation" warnings./Zc:preprocessor option to enable the standard-conformant preprocessor.Argum can operate in 3 modes selected at compile time:
Expected<T> return values. This class is similar to the proposed std::expected or boost::outcome. Trying to access a result.value() when it contains an error throws an equivalent exception. This mode is enabled via the ARGUM_USE_EXPECTED macro.result.value() when it contains an error calls std::terminate. This mode can be manually enabled via the ARGUM_NO_THROW macro. On Clang, GCC and MSVC, Argum automatically detects if exceptions are disabled during compilation and switches to this mode.Note that these modes only affect the handling of parsing errors. Logic errors such as passing incorrect parameters to configure the parser always assert in debug and std::terminate in non-debug builds.
By default, when being passed invalid arguments or when attempting to "throw" with exceptions disabled, Argum calls assert in debug mode and
std::terminate in release. You can customize this behavior. To do so, define ARGUM_CUSTOM_TERMINATE for the compilation. If you do this,
you will need to provide your own implementation of [[noreturn]] inline void terminateApplication(const char * message).
For reference, this is the default implementation:
[[noreturn]] inline void Argum::terminateApplication(const char * message) {
fprintf(stderr, "%s\n", message);
fflush(stderr);
#ifndef NDEBUG
assert(false);
#endif
std::terminate();
}
There are quite a few command-line parsing libraries for C++ out there, including Boost.Program_options, Lyra, TCLAP and many others. Unfortunately, beyond toy applications, I found none of them simultaneously easy to integrate, easy to use and easy to tweak. Specifically:
iostreams in any way.char and wchar_t.