https://github.com/rsore/clargs
A modern, type-safe, header-only C++20 command-line argument parser with minimal runtime overhead and full compile-time validation
https://github.com/rsore/clargs
command-line-arg-processing command-line-arguments command-line-arguments-parser command-line-parser parser
Last synced: 4 months ago
JSON representation
A modern, type-safe, header-only C++20 command-line argument parser with minimal runtime overhead and full compile-time validation
- Host: GitHub
- URL: https://github.com/rsore/clargs
- Owner: rsore
- License: mit
- Created: 2024-07-23T05:58:12.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-11-17T23:19:00.000Z (over 1 year ago)
- Last Synced: 2024-11-17T23:26:53.679Z (over 1 year ago)
- Topics: command-line-arg-processing, command-line-arguments, command-line-arguments-parser, command-line-parser, parser
- Language: C++
- Homepage: https://rsore.github.io/clargs/
- Size: 229 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
[](https://github.com/rubensorensen/clargs/actions/workflows/tests.yml)
[](https://github.com/rubensorensen/clargs/actions/workflows/docs.yml)
[](https://github.com/rsore/clargs/actions/workflows/clang-format.yml)
[](https://github.com/rsore/clargs/actions/workflows/clang-tidy-analysis.yml)

> **Note**: This project is under active development, and its APIs are subject to change without notice.
# CLArgs
This library is a modern C++ (C++20) command-line argument parser designed with a focus on type safety, compile-time validation, and runtime performance. Unlike many other libraries, it avoids macros, dynamic allocation, type-erasure, and expensive runtime operations.
At its core, this parser ensures that all argument types are validated at compile-time, minimizing the chances of runtime errors. Once parsing is complete, retrieving values is as simple as a constant-time array lookup, meaning that all command-line options are available as strongly-typed values with zero overhead.
## Example usage
```cpp
using VerboseFlag = CLArgs::Flag<"--verbose,-v", "Enable verbose output">;
using ConfigOption = CLArgs::Option<"--config,--configuration,-c", "", "Specify config file", std::filesystem::path>;
int
main(int argc, char **argv)
{
auto parser = CLArgs::ParserBuilder{}
.add_program_description<"Example program.">()
.add_flag()
.add_option()
.build();
try
{
parser.parse(argc, argv);
}
catch (std::exception &e)
{
std::cerr << "Error: " << e.what() << '\n';
std::cerr << parser.help() << std::endl;
return EXIT_FAILURE;
}
const bool has_verbose = parser.has_flag();
std::cout << "Has verbose option: " << std::boolalpha << has_verbose << "\n";
if (const auto config = parser.get_option(); config.has_value())
{
std::cout << "Config file: " << config.value() << std::endl;
}
}
```
For full examples, see the examples/ directory
## Error handling
Static type-checking ensures proper usage and prevents building with invalid or unsupported value types.
All arguments are parsed, validated and stored during the `parse` method. This is the only potential point of failure, meaning that if the application makes it past that function call with no exceptions, values can safely be retrieved at any time with no risk.