/* * Copyright 2026 Joel Klinghed * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to * deal in the Software without restriction, including without limitation the * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * IN THE SOFTWARE. */ #include "args.hh" #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { std::string kEmpty; class OptionImpl : public Args::OptionArgument { public: enum Type : uint8_t { NoArgument, RequiredArgument, OptionalArgument, }; OptionImpl(Type type, char shortname, std::string longname, std::string arg, std::string help) : type(type), shortname(shortname), longname(std::move(longname)), arg(std::move(arg)), help(std::move(help)) {} const Type type; const char shortname; const std::string longname; const std::string arg; const std::string help; [[nodiscard]] bool is_set() const override { return is_set_; } [[nodiscard]] bool has_argument() const override { return value_.has_value(); } [[nodiscard]] const std::string& argument() const override { if (value_.has_value()) return value_.value(); assert(false); return kEmpty; } void clear() { is_set_ = false; value_.reset(); } void set_argument(std::string value) { assert(type == Type::RequiredArgument || type == Type::OptionalArgument); is_set_ = true; value_ = std::move(value); } void set_no_argument() { assert(type == Type::NoArgument || type == Type::OptionalArgument); is_set_ = true; value_.reset(); } private: bool is_set_{false}; std::optional value_; }; class ArgsImpl : public Args { public: explicit ArgsImpl(std::string prgname) : prgname_(std::move(prgname)) {} std::shared_ptr