Files
Library/tests/standalone/Configuration/Argparse.cpp
T

378 lines
14 KiB
C++

#include <Void/Configuration/Argparse.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <optional>
#include <string>
#include <vector>
using namespace Void::Argparse;
// Helper: convert vector of strings to argc/argv style
struct Args {
std::vector<std::string> strs;
std::vector<const char*> ptrs;
int argc;
const char* const* argv;
explicit Args(std::vector<std::string> s) : strs{std::move(s)}
{
for (const auto& str : strs) ptrs.push_back(str.c_str());
argc = static_cast<int>(ptrs.size());
argv = ptrs.data();
}
};
// ── flags ─────────────────────────────────────────────────────────────────────
Void::Testing::Test argparse_flag_long()
{
bool verbose = false;
Parser p{"test"};
p.flag("--verbose", "-v", "enable verbose", verbose);
const Args a{{"test", "--verbose"}};
Void::Testing::expect(p.parse(a.argc, a.argv).has_value());
Void::Testing::expect(verbose);
}
Void::Testing::Test argparse_flag_short()
{
bool verbose = false;
Parser p{"test"};
p.flag("--verbose", "-v", "enable verbose", verbose);
const Args a{{"test", "-v"}};
Void::Testing::expect(p.parse(a.argc, a.argv).has_value());
Void::Testing::expect(verbose);
}
Void::Testing::Test argparse_flag_absent()
{
bool verbose = false;
Parser p{"test"};
p.flag("--verbose", "-v", "enable verbose", verbose);
const Args a{{"test"}};
Void::Testing::expect(p.parse(a.argc, a.argv).has_value());
Void::Testing::expect(!verbose);
}
Void::Testing::Test argparse_multiple_flags()
{
bool a = false, b = false;
Parser p{"test"};
p.flag("--alpha", "-a", "", a);
p.flag("--beta", "-b", "", b);
const Args args{{"test", "--alpha"}};
Void::Testing::expect(p.parse(args.argc, args.argv).has_value());
Void::Testing::expect(a);
Void::Testing::expect(!b);
}
// ── options ───────────────────────────────────────────────────────────────────
Void::Testing::Test argparse_option_equals()
{
std::optional<std::string> out;
Parser p{"test"};
p.option("--output", "-o", "output file", out);
const Args a{{"test", "--output=file.txt"}};
Void::Testing::expect(p.parse(a.argc, a.argv).has_value());
Void::Testing::expect(out.has_value());
Void::Testing::expect_eq(*out, std::string{"file.txt"});
}
Void::Testing::Test argparse_option_space()
{
std::optional<std::string> out;
Parser p{"test"};
p.option("--output", "-o", "output file", out);
const Args a{{"test", "--output", "result.txt"}};
Void::Testing::expect(p.parse(a.argc, a.argv).has_value());
Void::Testing::expect_eq(*out, std::string{"result.txt"});
}
Void::Testing::Test argparse_option_short()
{
std::optional<std::string> out;
Parser p{"test"};
p.option("--output", "-o", "output file", out);
const Args a{{"test", "-o", "out.txt"}};
Void::Testing::expect(p.parse(a.argc, a.argv).has_value());
Void::Testing::expect_eq(*out, std::string{"out.txt"});
}
Void::Testing::Test argparse_option_default()
{
std::string out;
Parser p{"test"};
p.option_default("--format", "-f", "output format", out, "json");
const Args a{{"test"}};
Void::Testing::expect(p.parse(a.argc, a.argv).has_value());
Void::Testing::expect_eq(out, std::string{"json"});
}
Void::Testing::Test argparse_option_default_overridden()
{
std::string out;
Parser p{"test"};
p.option_default("--format", "-f", "output format", out, "json");
const Args a{{"test", "--format=xml"}};
Void::Testing::expect(p.parse(a.argc, a.argv).has_value());
Void::Testing::expect_eq(out, std::string{"xml"});
}
Void::Testing::Test argparse_option_missing_value_is_error()
{
std::optional<std::string> out;
Parser p{"test"};
p.option("--output", "-o", "output", out);
const Args a{{"test", "--output"}}; // value missing
const auto r = p.parse(a.argc, a.argv);
Void::Testing::expect(!r.has_value());
}
// ── positionals ───────────────────────────────────────────────────────────────
Void::Testing::Test argparse_positional()
{
std::string filename;
Parser p{"test"};
p.positional("input", "input file", filename);
const Args a{{"test", "data.csv"}};
Void::Testing::expect(p.parse(a.argc, a.argv).has_value());
Void::Testing::expect_eq(filename, std::string{"data.csv"});
}
Void::Testing::Test argparse_positional_missing_is_error()
{
std::string filename;
Parser p{"test"};
p.positional("input", "input file", filename);
const Args a{{"test"}};
Void::Testing::expect(!p.parse(a.argc, a.argv).has_value());
}
Void::Testing::Test argparse_mix_flags_and_positionals()
{
bool flag = false;
std::string file;
Parser p{"test"};
p.flag("--dry-run", "-n", "dry run", flag);
p.positional("file", "file to process", file);
const Args a{{"test", "--dry-run", "input.txt"}};
Void::Testing::expect(p.parse(a.argc, a.argv).has_value());
Void::Testing::expect(flag);
Void::Testing::expect_eq(file, std::string{"input.txt"});
}
// ── errors ────────────────────────────────────────────────────────────────────
Void::Testing::Test argparse_unknown_option_is_error()
{
Parser p{"test"};
const Args a{{"test", "--unknown"}};
Void::Testing::expect(!p.parse(a.argc, a.argv).has_value());
}
// ── help ──────────────────────────────────────────────────────────────────────
Void::Testing::Test argparse_help_contains_names()
{
bool v = false;
std::optional<std::string> o;
Parser p{"mytool", "A description."};
p.flag("--verbose", "-v", "verbose mode", v);
p.option("--output", "-o", "output file", o);
const auto h = p.help();
Void::Testing::expect(h.find("--verbose") != std::string::npos);
Void::Testing::expect(h.find("--output") != std::string::npos);
Void::Testing::expect(h.find("A description.") != std::string::npos);
}
Void::Testing::Test argparse_subcommands_option_many_and_distinct_outcomes()
{
bool verbose = false;
std::vector<std::string> targets;
std::vector<std::string> profiles;
std::string compdb_mode;
bool build_selected = false;
bool use_selected = false;
Parser root{"vbs", "Void Build System"};
root.flag("--verbose", "-v", "verbose output", verbose);
auto& build_cmd =
root.subcommand("build", "Build project", [&] { build_selected = true; });
build_cmd.option_many("--target", "-t", "targets", targets);
build_cmd.option_many("--profile", "-p", "profiles", profiles);
auto& compdb_cmd = root.subcommand("compdb", "Compile database commands");
auto& use_cmd =
compdb_cmd.subcommand("use", "Select compdb", [&] { use_selected = true; });
use_cmd.positional("mode", "compdb mode", compdb_mode);
// 1. Subcommand + repeatable option_many
{
const Args a{{"vbs", "-v", "build", "--target", "app", "--target=lib",
"-p", "debug", "--profile", "release"}};
const auto res = root.parse_detailed(a.argc, a.argv);
Void::Testing::require(res.ok());
Void::Testing::expect(verbose);
Void::Testing::expect(build_selected);
Void::Testing::expect(!use_selected);
Void::Testing::expect_eq(res.matched_command_path.size(), std::size_t{1});
Void::Testing::expect_eq(res.matched_command_path[0],
std::string{"build"});
Void::Testing::expect_eq(targets.size(), std::size_t{2});
Void::Testing::expect_eq(targets[0], std::string{"app"});
Void::Testing::expect_eq(targets[1], std::string{"lib"});
Void::Testing::expect_eq(profiles.size(), std::size_t{2});
Void::Testing::expect_eq(profiles[0], std::string{"debug"});
Void::Testing::expect_eq(profiles[1], std::string{"release"});
}
// 2. Nested subcommands ("vbs compdb use active")
{
build_selected = false;
use_selected = false;
const Args a{{"vbs", "compdb", "use", "active"}};
const auto res = root.parse_detailed(a.argc, a.argv);
Void::Testing::require(res.ok());
Void::Testing::expect(!build_selected);
Void::Testing::expect(use_selected);
Void::Testing::expect_eq(res.matched_command_path.size(), std::size_t{2});
Void::Testing::expect_eq(res.matched_command_path[0],
std::string{"compdb"});
Void::Testing::expect_eq(res.matched_command_path[1], std::string{"use"});
Void::Testing::expect_eq(compdb_mode, std::string{"active"});
}
// 3. Help is ParseOutcome::Help (distinct from ParseOutcome::Error)
{
const Args a{{"vbs", "--help"}};
const auto res = root.parse_detailed(a.argc, a.argv);
Void::Testing::expect(res.is_help());
Void::Testing::expect(!res.is_error());
Void::Testing::expect(res.message.find("Commands:") != std::string::npos);
const Args sub_help{{"vbs", "build", "--help"}};
const auto sres = root.parse_detailed(sub_help.argc, sub_help.argv);
Void::Testing::expect(sres.is_help());
Void::Testing::expect(sres.message.find("--target") != std::string::npos);
}
// 4. Missing or unknown subcommand is ParseOutcome::Error without running callbacks
{
build_selected = false;
const Args missing_sub{{"vbs"}};
const auto mres = root.parse_detailed(missing_sub.argc, missing_sub.argv);
Void::Testing::expect(mres.is_error());
const Args bad_sub{{"vbs", "unknowncmd"}};
const auto bres = root.parse_detailed(bad_sub.argc, bad_sub.argv);
Void::Testing::expect(bres.is_error());
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);
}