357 lines
12 KiB
C++
357 lines
12 KiB
C++
#include <Void/Configuration/Argparse.hpp>
|
|
|
|
#include <sstream>
|
|
|
|
namespace Void::Argparse
|
|
{
|
|
|
|
Parser::Parser(std::string program, std::string description)
|
|
: program_{std::move(program)}, description_{std::move(description)}
|
|
{
|
|
}
|
|
|
|
Parser::~Parser() = default;
|
|
Parser::Parser(Parser&&) noexcept = default;
|
|
Parser& Parser::operator=(Parser&&) noexcept = default;
|
|
|
|
Parser& Parser::flag(std::string_view lng,
|
|
std::string_view sht,
|
|
std::string_view desc,
|
|
bool& out)
|
|
{
|
|
flags_.push_back(
|
|
{std::string{lng}, std::string{sht}, std::string{desc}, &out});
|
|
return *this;
|
|
}
|
|
|
|
Parser& Parser::option(std::string_view lng,
|
|
std::string_view sht,
|
|
std::string_view desc,
|
|
std::optional<std::string>& out)
|
|
{
|
|
options_.push_back(
|
|
{std::string{lng}, std::string{sht}, std::string{desc}, {}, &out, nullptr, nullptr});
|
|
return *this;
|
|
}
|
|
|
|
Parser& Parser::option_many(std::string_view lng,
|
|
std::string_view sht,
|
|
std::string_view desc,
|
|
std::vector<std::string>& out)
|
|
{
|
|
options_.push_back(
|
|
{std::string{lng}, std::string{sht}, std::string{desc}, {}, nullptr, nullptr, &out});
|
|
return *this;
|
|
}
|
|
|
|
Parser& Parser::option_default(std::string_view lng,
|
|
std::string_view sht,
|
|
std::string_view desc,
|
|
std::string& out,
|
|
std::string_view default_value)
|
|
{
|
|
out = std::string{default_value};
|
|
options_.push_back({std::string{lng},
|
|
std::string{sht},
|
|
std::string{desc},
|
|
std::string{default_value},
|
|
nullptr,
|
|
&out,
|
|
nullptr});
|
|
return *this;
|
|
}
|
|
|
|
Parser& Parser::positional(std::string_view name,
|
|
std::string_view desc,
|
|
std::string& out)
|
|
{
|
|
positionals_.push_back({std::string{name}, std::string{desc}, &out});
|
|
return *this;
|
|
}
|
|
|
|
Parser& Parser::positional_many(std::string_view name,
|
|
std::string_view desc,
|
|
std::vector<std::string>& out,
|
|
bool required)
|
|
{
|
|
positional_many_ = MultiPosEntry{
|
|
std::string{name}, std::string{desc}, &out, required};
|
|
return *this;
|
|
}
|
|
|
|
Parser& Parser::subcommand(std::string name,
|
|
std::string description,
|
|
std::function<void()> on_selected)
|
|
{
|
|
const std::string sub_prog =
|
|
program_.empty() ? name : (program_ + " " + name);
|
|
auto sub_parser = std::make_unique<Parser>(sub_prog, std::move(description));
|
|
Parser* raw = sub_parser.get();
|
|
subcommands_.push_back(SubcommandEntry{
|
|
.name = std::move(name),
|
|
.parser = std::move(sub_parser),
|
|
.on_selected = std::move(on_selected),
|
|
});
|
|
return *raw;
|
|
}
|
|
|
|
Parser& Parser::require_subcommand(bool required) noexcept
|
|
{
|
|
has_explicit_require_sub_ = true;
|
|
require_subcommand_ = required;
|
|
return *this;
|
|
}
|
|
|
|
ParseResult Parser::parse_detailed(int argc, const char* const* argv) const
|
|
{
|
|
const Parser* active = this;
|
|
std::size_t pos_idx = 0;
|
|
bool after_double_dash = false;
|
|
std::vector<std::string> matched_path;
|
|
std::vector<const std::function<void()>*> callbacks;
|
|
|
|
auto match_flag_or_opt = [&](const Parser* p,
|
|
std::string_view arg,
|
|
int& i,
|
|
bool& matched,
|
|
std::string& err) -> bool {
|
|
for (const auto& f : p->flags_) {
|
|
if (arg == f.lng || (!f.sht.empty() && arg == f.sht)) {
|
|
*f.out = true;
|
|
matched = true;
|
|
return true;
|
|
}
|
|
}
|
|
for (const auto& opt : p->options_) {
|
|
std::string_view value;
|
|
bool found = false;
|
|
if (arg.size() > opt.lng.size() + 1 &&
|
|
arg.substr(0, opt.lng.size()) == opt.lng &&
|
|
arg[opt.lng.size()] == '=') {
|
|
value = arg.substr(opt.lng.size() + 1);
|
|
found = true;
|
|
} else if (arg == opt.lng ||
|
|
(!opt.sht.empty() && arg == opt.sht)) {
|
|
if (i + 1 >= argc) {
|
|
err = "option '" + std::string{arg} + "' requires a value";
|
|
return false;
|
|
}
|
|
value = argv[++i];
|
|
found = true;
|
|
}
|
|
if (found) {
|
|
const std::string val_str{value};
|
|
if (opt.out_opt) *opt.out_opt = val_str;
|
|
if (opt.out_str) *opt.out_str = val_str;
|
|
if (opt.out_many) opt.out_many->push_back(val_str);
|
|
matched = true;
|
|
return true;
|
|
}
|
|
}
|
|
matched = false;
|
|
return true;
|
|
};
|
|
|
|
for (int i = 1; i < argc; ++i) {
|
|
const std::string_view arg{argv[i]};
|
|
|
|
if (!after_double_dash) {
|
|
if (arg == "--") {
|
|
after_double_dash = true;
|
|
continue;
|
|
}
|
|
|
|
if (arg == "--help" || arg == "-h") {
|
|
return ParseResult{
|
|
.outcome = ParseOutcome::Help,
|
|
.message = active->help(),
|
|
.matched_command_path = std::move(matched_path),
|
|
};
|
|
}
|
|
|
|
bool matched = false;
|
|
std::string err;
|
|
if (!match_flag_or_opt(active, arg, i, matched, err)) {
|
|
return ParseResult{
|
|
.outcome = ParseOutcome::Error,
|
|
.message = std::move(err),
|
|
.matched_command_path = std::move(matched_path),
|
|
};
|
|
}
|
|
if (matched) {
|
|
continue;
|
|
}
|
|
|
|
if (arg.size() > 1 && arg[0] == '-') {
|
|
return ParseResult{
|
|
.outcome = ParseOutcome::Error,
|
|
.message = "unknown option: " + std::string{arg},
|
|
.matched_command_path = std::move(matched_path),
|
|
};
|
|
}
|
|
|
|
if (pos_idx == 0 && !active->subcommands_.empty()) {
|
|
const SubcommandEntry* chosen = nullptr;
|
|
for (const auto& sub : active->subcommands_) {
|
|
if (sub.name == arg) {
|
|
chosen = ⊂
|
|
break;
|
|
}
|
|
}
|
|
if (chosen != nullptr) {
|
|
matched_path.push_back(chosen->name);
|
|
if (chosen->on_selected) {
|
|
callbacks.push_back(&chosen->on_selected);
|
|
}
|
|
active = chosen->parser.get();
|
|
pos_idx = 0;
|
|
continue;
|
|
}
|
|
if (active->positionals_.empty() &&
|
|
!active->positional_many_.has_value()) {
|
|
return ParseResult{
|
|
.outcome = ParseOutcome::Error,
|
|
.message = "unknown subcommand: " + std::string{arg},
|
|
.matched_command_path = std::move(matched_path),
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
if (pos_idx < active->positionals_.size()) {
|
|
*active->positionals_[pos_idx++].out = std::string{arg};
|
|
} else if (active->positional_many_.has_value() &&
|
|
active->positional_many_->out != nullptr) {
|
|
active->positional_many_->out->push_back(std::string{arg});
|
|
} else {
|
|
return ParseResult{
|
|
.outcome = ParseOutcome::Error,
|
|
.message =
|
|
"unexpected positional argument: " + std::string{arg},
|
|
.matched_command_path = std::move(matched_path),
|
|
};
|
|
}
|
|
}
|
|
|
|
if (!active->subcommands_.empty()) {
|
|
const bool req = active->has_explicit_require_sub_
|
|
? active->require_subcommand_
|
|
: true;
|
|
if (req) {
|
|
return ParseResult{
|
|
.outcome = ParseOutcome::Error,
|
|
.message = "missing subcommand for '" + active->program_ + "'",
|
|
.matched_command_path = std::move(matched_path),
|
|
};
|
|
}
|
|
}
|
|
|
|
for (std::size_t j = pos_idx; j < active->positionals_.size(); ++j) {
|
|
return ParseResult{
|
|
.outcome = ParseOutcome::Error,
|
|
.message = "missing required argument: <" +
|
|
active->positionals_[j].name + ">",
|
|
.matched_command_path = std::move(matched_path),
|
|
};
|
|
}
|
|
|
|
if (active->positional_many_.has_value() &&
|
|
active->positional_many_->required &&
|
|
active->positional_many_->out != nullptr &&
|
|
active->positional_many_->out->empty()) {
|
|
return ParseResult{
|
|
.outcome = ParseOutcome::Error,
|
|
.message = "missing required arguments: <" +
|
|
active->positional_many_->name + ">",
|
|
.matched_command_path = std::move(matched_path),
|
|
};
|
|
}
|
|
|
|
for (const auto* cb : callbacks) {
|
|
(*cb)();
|
|
}
|
|
|
|
return ParseResult{
|
|
.outcome = ParseOutcome::Ok,
|
|
.message = {},
|
|
.matched_command_path = std::move(matched_path),
|
|
};
|
|
}
|
|
|
|
std::expected<void, std::string> Parser::parse(int argc,
|
|
const char* const* argv) const
|
|
{
|
|
auto res = parse_detailed(argc, argv);
|
|
if (!res.ok()) {
|
|
return std::unexpected{std::move(res.message)};
|
|
}
|
|
return {};
|
|
}
|
|
|
|
std::string Parser::help() const
|
|
{
|
|
std::ostringstream os;
|
|
os << "Usage: " << program_;
|
|
for (const auto& f : flags_) os << " [" << f.lng << "]";
|
|
for (const auto& o : options_) {
|
|
const std::string_view dn =
|
|
std::string_view{o.lng}.substr(o.lng.size() > 2 ? 2 : 0);
|
|
os << " [" << o.lng << " <" << dn << ">]";
|
|
}
|
|
if (!subcommands_.empty()) {
|
|
os << " <command>";
|
|
}
|
|
for (const auto& p : positionals_) os << " <" << p.name << ">";
|
|
if (positional_many_.has_value()) {
|
|
os << (positional_many_->required ? " <" : " [")
|
|
<< positional_many_->name
|
|
<< (positional_many_->required ? "...>" : "...]");
|
|
}
|
|
os << "\n";
|
|
if (!description_.empty()) os << "\n" << description_ << "\n";
|
|
|
|
if (!subcommands_.empty()) {
|
|
os << "\nCommands:\n";
|
|
for (const auto& s : subcommands_) {
|
|
os << " " << s.name;
|
|
if (!s.parser->description_.empty()) {
|
|
os << " " << s.parser->description_;
|
|
}
|
|
os << "\n";
|
|
}
|
|
}
|
|
if (!positionals_.empty() || positional_many_.has_value()) {
|
|
os << "\nArguments:\n";
|
|
for (const auto& p : positionals_)
|
|
os << " <" << p.name << "> " << p.desc << "\n";
|
|
if (positional_many_.has_value()) {
|
|
os << " <" << positional_many_->name << "...> "
|
|
<< positional_many_->desc << "\n";
|
|
}
|
|
}
|
|
if (!flags_.empty()) {
|
|
os << "\nFlags:\n";
|
|
for (const auto& f : flags_) {
|
|
os << " " << f.lng;
|
|
if (!f.sht.empty()) os << ", " << f.sht;
|
|
os << " " << f.desc << "\n";
|
|
}
|
|
}
|
|
if (!options_.empty()) {
|
|
os << "\nOptions:\n";
|
|
for (const auto& o : options_) {
|
|
const std::string_view dname =
|
|
std::string_view{o.lng}.substr(o.lng.size() > 2 ? 2 : 0);
|
|
os << " " << o.lng;
|
|
if (!o.sht.empty()) os << ", " << o.sht;
|
|
os << " <" << dname << ">";
|
|
if (!o.def.empty()) os << " (default: " << o.def << ")";
|
|
if (o.out_many != nullptr) os << " (repeatable)";
|
|
os << " " << o.desc << "\n";
|
|
}
|
|
}
|
|
os << " --help, -h show this help\n";
|
|
return os.str();
|
|
}
|
|
|
|
} // namespace Void::Argparse
|