#include #include namespace Void::Argparse { Parser::Parser(std::string program, std::string description) : program_{std::move(program)}, description_{std::move(description)} {} 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& out) { options_.push_back({std::string{lng}, std::string{sht}, std::string{desc}, {}, &out, nullptr}); 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}); 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; } std::expected Parser::parse(int argc, const char* const* argv) const { std::size_t pos_idx = 0; for (int i = 1; i < argc; ++i) { const std::string_view arg{argv[i]}; // Help if (arg == "--help" || arg == "-h") { return std::unexpected{help()}; } // Flag check bool matched_flag = false; for (const auto& f : flags_) { if (arg == f.lng || (!f.sht.empty() && arg == f.sht)) { *f.out = true; matched_flag = true; break; } } if (matched_flag) continue; // Option check: --key=value or --key value or -k value bool matched_opt = false; for (const auto& opt : options_) { std::string_view value; bool found = false; // --key=value 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; } // --key value or -k value else if (arg == opt.lng || (!opt.sht.empty() && arg == opt.sht)) { if (i + 1 >= argc) return std::unexpected{"option '" + std::string{arg} + "' requires a value"}; 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; matched_opt = true; break; } } if (matched_opt) continue; // Unknown --/- prefixed arg if (arg.size() > 1 && arg[0] == '-') return std::unexpected{"unknown option: " + std::string{arg}}; // Positional if (pos_idx >= positionals_.size()) return std::unexpected{"unexpected positional argument: " + std::string{arg}}; *positionals_[pos_idx++].out = std::string{arg}; } // Check required positionals for (std::size_t j = pos_idx; j < positionals_.size(); ++j) return std::unexpected{"missing required argument: <" + positionals_[j].name + ">"}; 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 << ">]"; } for (const auto& p : positionals_) os << " <" << p.name << ">"; os << "\n"; if (!description_.empty()) os << "\n" << description_ << "\n"; if (!positionals_.empty()) { os << "\nArguments:\n"; for (const auto& p : positionals_) os << " <" << p.name << "> " << p.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_) { // Derive a display name from the long option (strip leading --) 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 << ")"; os << " " << o.desc << "\n"; } } os << " --help, -h show this help\n"; return os.str(); } } // namespace Void::Argparse