/* * @project The CERN Tape Archive (CTA) * @copyright Copyright © 2021-2022 CERN * @license This program is free software, distributed under the terms of the GNU General Public * Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING". You can * redistribute it and/or modify it under the terms of the GPL Version 3, or (at your * option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * PARTICULAR PURPOSE. See the GNU General Public License for more details. * * In applying this licence, CERN does not waive the privileges and immunities * granted to it by virtue of its status as an Intergovernmental Organization or * submit itself to any jurisdiction. */ #pragma once #include #include #include #include "common/dataStructures/Tape.hpp" #include "CtaFrontendApi.hpp" namespace cta::admin { using namespace common::dataStructures; /*! * Command line option class */ class Option { public: enum option_t { OPT_CMD, OPT_FLAG, OPT_BOOL, OPT_UINT, OPT_STR, OPT_STR_LIST }; /*! * Constructor */ Option(option_t type, const std::string &long_opt, const std::string &short_opt, const std::string &help_txt, const std::string &alias = "") : m_type(type), m_lookup_key((alias.size() == 0) ? long_opt : alias), m_long_opt(long_opt), m_short_opt(short_opt), m_help_txt(help_txt), m_is_optional(false) { } /*! * Copy-construct an optional version of this option */ Option optional() const { Option option(*this); option.m_is_optional = true; return option; } /*! * Check if the supplied key matches the option */ bool operator==(const std::string &option) const { return option == m_short_opt || option == m_long_opt; } /*! * Check if the supplied option matches the option */ bool operator==(const Option &option) const { return option == m_short_opt || option == m_long_opt; } /*! * Return the type of this option */ option_t get_type() const { return m_type; } /*! * Return the number of parameters expected after this option */ int num_params() const { return m_type == OPT_CMD || m_type == OPT_FLAG ? 0 : 1; } /*! * Return the key for this option */ const std::string &get_key() const { return m_lookup_key; } /*! * Return whether the option is optional */ bool is_optional() const { return m_is_optional; } /*! * Validate the command protocol buffer against this option * * If the option is compulsory and is not present, throws an exception */ void validateCmd(const cta::admin::AdminCmd &admincmd) const; /*! * Return the help text for this option */ std::string get_help_text() const { return m_help_txt; } /*! * Return per-option help string */ std::string help() const { std::string help = m_is_optional ? " [" : " "; help += (m_type == OPT_CMD) ? "" : m_long_opt + '/' + m_short_opt; help += m_help_txt; help += m_is_optional ? "]" : ""; return help; } private: option_t m_type; //!< Option type std::string m_lookup_key; //!< Key to map option string to Protocol Buffer enum std::string m_long_opt; //!< Long command option std::string m_short_opt; //!< Short command option std::string m_help_txt; //!< Option help text bool m_is_optional; //!< Option is optional or compulsory }; /* * Type aliases */ using cmdLookup_t = std::map; using subcmdLookup_t = std::map; using cmd_key_t = std::pair; using cmd_val_t = std::vector