muopt is a header-only C++17 micro-library for argument parsing. Its design is inspired by
Lexopt.
The following command line options are supported.
- Short arguments (
-h) - Long arguments (
--help) - Short arguments with values (
-ovalue,-o=value,-o value) - Chained short arguments (
-abcresults three short flags:a,b, andc) - Long arguments with values (
--name=Vito,--name Vito) - Plain arguments, i.e. options without hyphen prefixes (
run,build)
#include "muopt/muopt.hpp"
#include <iostream>
int main(int argc, char **argv) {
muopt::Parser parser(argc, argv);
while (std::optional<muopt::Arg> arg = parser.next()) {
if (arg->is_long("help"))
std::cout << "usage: example [--input FILE]\n";
if (arg->is_long("input"))
std::cout << parser.arg_value().value_or("") << '\n';
}
}See the examples directory for more usage examples.
In muopt, options are values are both just arguments. Whether it is treated as an option or a value depends on how it interacts with
the parser, the - and -- prefix determines the kind and not some coupling implementation.
- The parser is created by instantiating
muopt::Parserwithargcandargv. next()returns the next argument as a Short, Long, or Plain.next()returnsstd::nulloptwhen args are exhausted.arg_value()returns the string of the next Plain, if a Plain is next in line, consuming it and making it unavailable to the nextnext()call.arg_value()returnsstd::nulloptotherwise. The non-Plain argument is not consumed; the nextnext()call will still see it.
CMake (FetchContent)
include(FetchContent)
FetchContent_Declare(
muopt
GIT_REPOSITORY https://github.com/secona/muopt.git
GIT_TAG main
)
FetchContent_MakeAvailable(muopt)
target_link_libraries(program PRIVATE muopt::muopt)CMake (vendored)
add_subdirectory(vendor/muopt)
target_link_libraries(program PRIVATE muopt::muopt)Bazel (Bzlmod)
# in MODULE.bazel
bazel_dep(name = "muopt")
git_override(
module_name = "muopt",
commit = "bedd3f76720c9790b0fd3175f8e28eaab3c3c13e", # change as needed
remote = "https://github.com/secona/muopt.git",
)Bazel
new_git_repository(
name = "muopt",
commit = "bedd3f76720c9790b0fd3175f8e28eaab3c3c13e", # change as needed
init_submodules = False,
remote = "https://github.com/secona/muopt.git",
)In one of my Rust projects, I used Lexopt and loved it. When working on a C++ project, I tried looking for
similar argument parsers with Lexopt's simplicity and couldn't find one, so I decided to make my own. muopt
offers more control and is intended to be minimal, bare-bones, and without bloat.
muopt is licensed under the MIT License. See LICENSE for more information.
- lexopt for the inspiration.