Argparse: richer Clap-like CLI (version, counts, checked values, required/conflicts/env, combined shorts, typo hints, aligned help)
This commit is contained in:
1 parent
d73a34d622
commit
af8757080f
3 files changed
+520
-61
No files matched your search
@@ -17,6 +17,7 @@ namespace Void::Argparse
|
||||
enum class ParseOutcome : std::uint8_t {
|
||||
Ok,
|
||||
Help,
|
||||
Version,
|
||||
Error,
|
||||
};
|
||||
|
||||
@@ -33,6 +34,10 @@ namespace Void::Argparse
|
||||
{
|
||||
return outcome == ParseOutcome::Help;
|
||||
}
|
||||
[[nodiscard]] bool is_version() const noexcept
|
||||
{
|
||||
return outcome == ParseOutcome::Version;
|
||||
}
|
||||
[[nodiscard]] bool is_error() const noexcept
|
||||
{
|
||||
return outcome == ParseOutcome::Error;
|
||||
@@ -72,6 +77,14 @@ namespace Void::Argparse
|
||||
std::string_view short_name,
|
||||
std::string_view description, bool& out);
|
||||
|
||||
// Add a repeatable flag counting occurrences (-vvv -> 3).
|
||||
Parser& count_flag(std::string_view long_name,
|
||||
std::string_view short_name,
|
||||
std::string_view description, int& out);
|
||||
|
||||
// Set the program version exposed as --version / -V.
|
||||
Parser& version(std::string version);
|
||||
|
||||
// Add an option that takes a value. If not provided, out stays
|
||||
// nullopt.
|
||||
Parser& option(std::string_view long_name,
|
||||
@@ -93,10 +106,39 @@ namespace Void::Argparse
|
||||
std::string& out,
|
||||
std::string_view default_value = "");
|
||||
|
||||
// Option restricted to a fixed set of values.
|
||||
Parser& option_checked(std::string_view long_name,
|
||||
std::string_view short_name,
|
||||
std::string_view description,
|
||||
std::optional<std::string>& out,
|
||||
std::vector<std::string> allowed);
|
||||
|
||||
Parser& option_default_checked(
|
||||
std::string_view long_name, std::string_view short_name,
|
||||
std::string_view description, std::string& out,
|
||||
std::string_view default_value,
|
||||
std::vector<std::string> allowed);
|
||||
|
||||
// Mark a previously added option as required.
|
||||
Parser& require_option(std::string_view long_name);
|
||||
|
||||
// Fallback to an environment variable when the option is absent.
|
||||
Parser& env(std::string_view long_name,
|
||||
std::string_view env_var);
|
||||
|
||||
// Two options may not be used together.
|
||||
Parser& conflicts(std::string_view first, std::string_view second);
|
||||
|
||||
// Add a required positional argument.
|
||||
Parser& positional(std::string_view name,
|
||||
std::string_view description, std::string& out);
|
||||
|
||||
// Positional restricted to a fixed set of values.
|
||||
Parser& positional_checked(std::string_view name,
|
||||
std::string_view description,
|
||||
std::string& out,
|
||||
std::vector<std::string> allowed);
|
||||
|
||||
// Add a variadic trailing positional argument list.
|
||||
Parser& positional_many(std::string_view name,
|
||||
std::string_view description,
|
||||
@@ -129,17 +171,22 @@ namespace Void::Argparse
|
||||
|
||||
struct FlagEntry {
|
||||
std::string lng, sht, desc;
|
||||
bool* out;
|
||||
bool* out{nullptr};
|
||||
int* count_out{nullptr};
|
||||
};
|
||||
struct OptionEntry {
|
||||
std::string lng, sht, desc, def;
|
||||
std::optional<std::string>* out_opt{nullptr};
|
||||
std::string* out_str{nullptr};
|
||||
std::vector<std::string>* out_many{nullptr};
|
||||
std::vector<std::string> allowed{};
|
||||
bool required{false};
|
||||
std::string env_var{};
|
||||
};
|
||||
struct PosEntry {
|
||||
std::string name, desc;
|
||||
std::string* out;
|
||||
std::string* out{nullptr};
|
||||
std::vector<std::string> allowed{};
|
||||
};
|
||||
struct MultiPosEntry {
|
||||
std::string name, desc;
|
||||
@@ -152,7 +199,8 @@ namespace Void::Argparse
|
||||
std::function<void()> on_selected;
|
||||
};
|
||||
|
||||
std::string program_, description_;
|
||||
std::string program_, description_, version_;
|
||||
std::vector<std::pair<std::string, std::string>> conflicts_;
|
||||
std::vector<FlagEntry> flags_;
|
||||
std::vector<OptionEntry> options_;
|
||||
std::vector<PosEntry> positionals_;
|
||||
|
||||
Reference in new issue
Block a user