Hello! I wanted to share a project that I've been working on for the last few days, a small argument parsing library called muopt. It started from a quite popular Rust crate called lexopt that I use in one of my CLI Rust projects. I wanted something similar to lexopt and couldn't find it for C++, so I decided to write my own.
muopt is more of a tokenizer/lexer than it is a parser. It doesn't "connect" a value to its option and just views them as an argument. muopt is intended to be minimal and bare-bones. Below is a really simple example of muopt in action.
#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->islong("help"))
std::cout << "usage: example [--input FILE]\n";
if (arg->islong("input"))
std::cout << parser.argvalue().valueor("") << '\n';
}
}
The code above will match the arguments
--help and -h. Both will print out the usage message. It will also match --input=Hello or --input Hello and print out the value. The README has a deeper explanation on how muopt works and the repo has more examples.muopt is still very early; it's not even released yet. I'm still debating on how to take on certain things, for example the
arg_value() return type and how errors should be handled. Any suggestions are welcomed!GitHub: https://github.com/secona/muopt
https://redd.it/1v6342h
@r_cpp