Line data Source code
1 : /*! 2 : * \file esys/repo/cli/cmd_t.h 3 : * \brief Cmd t for esysrepo 4 : * 5 : * \cond 6 : * __legal_b__ 7 : * 8 : * Copyright (c) 2020-2023 Michel Gillet 9 : * Distributed under the MIT License. 10 : * (See accompanying file LICENSE.txt or 11 : * copy at https://opensource.org/licenses/MIT) 12 : * 13 : * __legal_e__ 14 : * \endcond 15 : * 16 : */ 17 : 18 : #pragma once 19 : 20 : #include "esys/repo/esysrepo_defs.h" 21 : #include "esys/repo/cli/cmd.h" 22 : #include "esys/repo/result.h" 23 : 24 : namespace esys::repo::cli 25 : { 26 : 27 : /*! \tparam CMD the cmd type */ 28 : template<typename CMD> 29 : class Cmd_t : public Cmd 30 : { 31 : public: 32 : using CmdType = CMD; 33 : 34 : //! Constructor 35 : /*! 36 : * \param[in] app the app 37 : * \param[in] name the name 38 : * \param[in] description the description 39 : */ 40 : Cmd_t(AppBase *app, const std::string &name, const std::string &description); 41 : 42 : //! Destructor 43 : ~Cmd_t() override; 44 : 45 : CMD &get_cmd(); 46 : 47 : exe::Cmd &get_cmd_base() override; 48 : 49 : //! configure cmd 50 : /*! 51 : * \param[out] cmd the cmd 52 : */ 53 : virtual int configure_cmd(CMD &cmd) = 0; 54 : 55 : //! parse and configure 56 : int parse_and_configure() override; 57 : 58 : //! run 59 : int run() override; 60 : 61 : private: 62 : //!< \cond DOXY_IMPL 63 : CMD m_cmd; 64 : //!< \endcond 65 : }; 66 : 67 : template<typename CMD> 68 171 : Cmd_t<CMD>::Cmd_t(AppBase *app, const std::string &name, const std::string &description) 69 171 : : Cmd(app, name, description) 70 : { 71 171 : m_cmd.set_cli_cmd(this); 72 171 : m_cmd.set_app_base(app); 73 171 : } 74 : 75 : template<typename CMD> 76 171 : Cmd_t<CMD>::~Cmd_t() 77 : { 78 171 : } 79 : 80 : template<typename CMD> 81 66 : CMD &Cmd_t<CMD>::get_cmd() 82 : { 83 68 : return m_cmd; 84 : } 85 : 86 : template<typename CMD> 87 48 : exe::Cmd &Cmd_t<CMD>::get_cmd_base() 88 : { 89 48 : return m_cmd; 90 : } 91 : 92 : template<typename CMD> 93 50 : int Cmd_t<CMD>::parse_and_configure() 94 : { 95 50 : int result = parse(); 96 50 : if (result < 0) return result; 97 : 98 96 : if (get_vm().count("command") == 0) return -1; 99 96 : std::string cmd_txt = get_vm()["command"].template as<std::string>(); 100 48 : if (cmd_txt != get_name()) 101 : { 102 : //! \TODO add log 103 : return -1; 104 : } 105 : 106 48 : result = Cmd::configure_cmd(get_cmd_base()); 107 48 : if (result < 0) return result; 108 : 109 48 : return configure_cmd(get_cmd()); 110 50 : } 111 : 112 : template<typename CMD> 113 16 : int Cmd_t<CMD>::run() 114 : { 115 16 : int result = parse_and_configure(); 116 16 : if (result < 0) return result; 117 : 118 16 : get_cmd().set_logger_if(this->get_logger_if()); 119 16 : get_cmd().set_console_os(get_console_os()); 120 16 : return get_cmd().run().get_result_code_int(); 121 : } 122 : 123 : } // namespace esys::repo::cli