You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Inspired by morrisfranken/argparse, I develop the similar function using template struct,
template <typename T>
structarg
{
T value_;
std::string msg_;
arg(const T& value = T(), const std::string& msg = "") : value_(value), msg_(msg) {}
operator T&() { value_.msg_ = msg_; return value_; }
friend std::istringstream& operator >>(std::istringstream& ss, arg& obj) { ss >> obj.value_; return ss;}
};
structOptions {
structInit : publicstructopt::sub_command
{
Init() {}
friend std::istringstream& operator >> (std::istringstream& ss, Init& obj) {
ss >> obj.path.value();
return ss;
}
std::optional<arg<std::filesystem::path>> path = arg<std::filesystem::path>("init", "Path to the scene file");
std::optional<arg<float>> scale = arg<float>(1.0f, "Scale of the scene");
arg<float> density = arg<float>(float(), "Density of the scene");
};
Init init = arg(Init(), "Initialize a new scene");
};
STRUCTOPT(Options, init);
STRUCTOPT(Options::Init, path, scale, density);
the examples are shown as follow:
./main -h
USAGE: main [FLAGS] [SUBCOMMANDS]
FLAGS:
-h, --help Prints help information
-v, --version Prints version information
SUBCOMMANDS:
init : Initialize a new scene
and the subcommand mode
./main init -h
USAGE: init [FLAGS] [OPTIONS] density
FLAGS:
-h, --help Prints help information
-v, --version Prints version information
OPTIONS:
-p, --path Path to the scene file (default: "init")
-s, --scale Scale of the scene (default: 1)
ARGS:
density : Density of the scene
I find a similar library at https://github.com/morrisfranken/argparse?tab=readme-ov-file
It provides a feasible way to define default values and help messages.
The text was updated successfully, but these errors were encountered: