Single-Header C++20 argument parsing library.
Arg3P is a small & header-only argument parser designed for C++20. It provides a simple, type-safe interface for defining command-line options with short and/or long names. Boolean arguments act as flags, while all other argument types consume a following value.
- Header-only & dependency free
- Supports both short (-a) and long (--alpha) options.
- Short options may be grouped:
-abc==-a -b -c.- Only one non-boolean (value-taking) option may appear in a group & must be the last to appear.
- Positional arguments may be specified to appear before and after flagged
arguments through the
PositionalArgumentclass. - Automatic type converesion for fundamental and string-constructible types.
- Clean, type-safe design using modern C++20 concepts and ranges.
See the examples/ directory for more comprehensive examples.
// see examples/basic.cpp
#include <iostream>
#include <Arg3P/Arg3P.hpp>
int
main(int argc, const char *argv[])
{
using namespace Arg3P;
// Define command-line arguments
auto verbose =
Arg<bool>::makeRequired('v', "verbose", "enable verbose output");
auto input =
Arg<std::string>::makeRequired('i', "input", "set the input file path");
auto threads =
Arg<int>::makeRequired("threads", "set the number of worker threads");
// Register arguments with the parser
Parser<const char *> parser{{input, threads, verbose}};
// Parse command line (skip program name)
const std::optional<Error> error = parser(std::span(argv + 1, argc - 1));
if(error.has_value()) {
std::cerr << "Error parsing argument: "
<< errorName(error.value().error) << ": "
<< error.value().message << "\n";
return 1;
}
// Access results
std::cout << "Verbose: " << verbose->get().value_or(false) << "\n";
std::cout << "Input: " << input->get().value_or("<none>") << "\n";
std::cout << "Threads: " << threads->get().value_or(1) << "\n";
}- Boolean arguments (
Arg<bool>) act as flags and take no value. - Non-boolean arguments consume the next command-line token as a value.
- Grouped short options (
-abc) expand left to right; the last may take a value. - When there are front and back positional arguments expected and there are more non-flagged arguments provided before any flagged arguments appear, these are interpreted as the back positional arguments.
Arg3P is licensed under the BSD 2-Clause license, see the LICENSE file or https://opensource.org/license/BSD-2-Clause for a copy of the License.