/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * Copyright 2022, Danny Robson */ #pragma once #include #include #include #include #include #include namespace cruft::cmdopt2 { struct argument { std::string name; std::optional description; bool required = false; using acceptor1_t = std::function; std::optional acceptor1; }; template struct ops : argument { template BaseT bind (ValueT&&) = delete; template BaseT bind (ValueT &ref) { CHECK (!acceptor1); if constexpr (std::is_same_v) { acceptor1 = [&ref] (std::string_view str) { ref = str; }; } else { acceptor1 = [&ref] (std::string_view str) { ref = parse::from_string (str); }; } return get (); } template BaseT bind (std::optional &ref) { CHECK (!acceptor1); if constexpr (std::is_same_v) { acceptor1 = [&ref] (std::string_view str) { ref = str; }; } else { acceptor1 = [&ref] (std::string_view str) { ref = parse::from_string (str); }; } return get (); } BaseT get (void) const { return reinterpret_cast (*this); } }; struct positional : public ops { static positional create (char const *name); static positional create (std::string_view name); static positional create (std::string const &name); static positional create (std::string &&name); int count = 1; }; struct keyword : public ops { using acceptor0_t = std::function; std::optional acceptor0; static keyword create (char const *name); static keyword create (std::string_view name); static keyword create (std::string const &name); static keyword create (std::string &&name); keyword flag (void) const; keyword flag (std::string_view long_) const; keyword flag (char short_) const; keyword count (int &) const; keyword present (bool &) const; std::optional short_; std::optional long_; }; }