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
@@ -273,3 +273,105 @@ Void::Testing::Test argparse_subcommands_option_many_and_distinct_outcomes()
|
||||
Void::Testing::expect(!build_selected);
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test argparse_rich_features()
|
||||
{
|
||||
using namespace Void::Argparse;
|
||||
|
||||
bool verbose = false;
|
||||
int level = 0;
|
||||
std::optional<std::string> mode;
|
||||
std::string format = "text";
|
||||
std::optional<std::string> required_opt;
|
||||
|
||||
Parser parser{"tool", "rich parser"};
|
||||
parser.version("1.2.3");
|
||||
parser.flag("--verbose", "-v", "verbose output", verbose)
|
||||
.count_flag("--level", "-l", "verbosity level", level)
|
||||
.option_checked("--mode", "-m", "mode", mode, {"fast", "slow"})
|
||||
.option_default_checked("--format", "-f", "format", format, "text",
|
||||
{"text", "json"})
|
||||
.option("--required", "-r", "required option", required_opt)
|
||||
.require_option("--required")
|
||||
.env("--required", "TOOL_REQUIRED")
|
||||
.conflicts("--verbose", "--mode");
|
||||
|
||||
// Checked/default values (avoid the --verbose/--mode conflict here).
|
||||
const char* argv[] = {"tool", "-ll", "--mode=fast", "--required", "x",
|
||||
"--format", "json"};
|
||||
auto result = parser.parse_detailed(7, argv);
|
||||
Void::Testing::expect(result.ok());
|
||||
Void::Testing::expect(!verbose);
|
||||
Void::Testing::expect_eq(level, 2);
|
||||
Void::Testing::require(mode.has_value());
|
||||
Void::Testing::expect_eq(*mode, std::string{"fast"});
|
||||
Void::Testing::expect_eq(format, std::string{"json"});
|
||||
Void::Testing::require(required_opt.has_value());
|
||||
Void::Testing::expect_eq(*required_opt, std::string{"x"});
|
||||
|
||||
// Combined short flags on an independent parser.
|
||||
{
|
||||
bool alpha = false;
|
||||
int beta = 0;
|
||||
Parser combined{"c"};
|
||||
combined.flag("--alpha", "-a", "alpha", alpha)
|
||||
.count_flag("--beta", "-b", "beta", beta);
|
||||
const char* combined_argv[] = {"c", "-abb"};
|
||||
auto combined_result = combined.parse_detailed(2, combined_argv);
|
||||
Void::Testing::expect(combined_result.ok());
|
||||
Void::Testing::expect(alpha);
|
||||
Void::Testing::expect_eq(beta, 2);
|
||||
}
|
||||
|
||||
// Version outcome.
|
||||
const char* version_argv[] = {"tool", "--version"};
|
||||
auto version = parser.parse_detailed(2, version_argv);
|
||||
Void::Testing::expect(version.is_version());
|
||||
Void::Testing::expect_eq(version.message, std::string{"tool 1.2.3"});
|
||||
|
||||
// Typo suggestion.
|
||||
const char* typo_argv[] = {"tool", "--verbos"};
|
||||
auto typo = parser.parse_detailed(2, typo_argv);
|
||||
Void::Testing::expect(typo.is_error());
|
||||
Void::Testing::expect(typo.message.find("did you mean '--verbose'") !=
|
||||
std::string::npos);
|
||||
|
||||
// Invalid checked value.
|
||||
const char* bad_value_argv[] = {"tool", "--mode", "medium", "--required",
|
||||
"x"};
|
||||
auto bad_value = parser.parse_detailed(4, bad_value_argv);
|
||||
Void::Testing::expect(bad_value.is_error());
|
||||
Void::Testing::expect(bad_value.message.find("possible values") !=
|
||||
std::string::npos);
|
||||
|
||||
// Missing required option.
|
||||
const char* missing_argv[] = {"tool"};
|
||||
auto missing = parser.parse_detailed(1, missing_argv);
|
||||
Void::Testing::expect(missing.is_error());
|
||||
Void::Testing::expect(missing.message.find("--required") !=
|
||||
std::string::npos);
|
||||
|
||||
// Environment fallback.
|
||||
::setenv("TOOL_REQUIRED", "from-env", 1);
|
||||
required_opt.reset();
|
||||
auto from_env = parser.parse_detailed(1, missing_argv);
|
||||
Void::Testing::expect(from_env.ok());
|
||||
Void::Testing::require(required_opt.has_value());
|
||||
Void::Testing::expect_eq(*required_opt, std::string{"from-env"});
|
||||
::unsetenv("TOOL_REQUIRED");
|
||||
|
||||
// Conflicting options.
|
||||
const char* conflict_argv[] = {"tool", "--verbose", "--mode", "slow",
|
||||
"--required", "x"};
|
||||
auto conflict = parser.parse_detailed(6, conflict_argv);
|
||||
Void::Testing::expect(conflict.is_error());
|
||||
Void::Testing::expect(conflict.message.find("cannot be used together") !=
|
||||
std::string::npos);
|
||||
|
||||
// Aligned help lists the new metadata.
|
||||
const std::string help = parser.help();
|
||||
Void::Testing::expect(help.find("Usage:") != std::string::npos);
|
||||
Void::Testing::expect(help.find("--mode, -m") != std::string::npos);
|
||||
Void::Testing::expect(help.find("possible values") != std::string::npos);
|
||||
Void::Testing::expect(help.find("--version, -V") != std::string::npos);
|
||||
}
|
||||
Reference in new issue
Block a user