Line data Source code
1 : /*! 2 : * \file esys/repo/exe/cmdhelp.cpp 3 : * \brief 4 : * 5 : * \cond 6 : * __legal_b__ 7 : * 8 : * Copyright (c) 2020-2021 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 : #include "esys/repo/esysrepo_prec.h" 19 : #include "esys/repo/exe/cmdhelp.h" 20 : #include "esys/repo/cli/appbase.h" 21 : 22 : #include <termcolor/termcolor.hpp> 23 : 24 : #include <sstream> 25 : #include <iomanip> 26 : #include <cassert> 27 : #include <iostream> 28 : 29 : namespace esys::repo::exe 30 : { 31 : 32 17 : CmdHelp::CmdHelp() 33 17 : : Cmd("Help") 34 : { 35 17 : } 36 : 37 17 : CmdHelp::~CmdHelp() = default; 38 : 39 2 : Result CmdHelp::impl_run() 40 : { 41 2 : std::ostringstream oss; 42 2 : std::ostringstream oss_help; 43 2 : int result = 0; 44 : 45 2 : assert(get_app_base() != nullptr); 46 : 47 5 : auto print_one = [&](cli::Cmd *cmd, const std::string &cmd_txt) -> void { 48 6 : oss.str(""); 49 6 : oss_help.str(""); 50 3 : oss << "Command '" << cmd_txt << "':" << std::endl; 51 3 : result = cmd->print_doc(oss_help); 52 3 : if (result < 0) 53 : { 54 0 : oss << *cmd->get_desc_all(); 55 0 : info(oss.str()); 56 : } 57 : else 58 : { 59 3 : info(oss.str()); 60 3 : if (get_console_os() != nullptr) 61 0 : cmd->print_doc(*get_console_os()); 62 : else 63 3 : std::cout << oss_help.str(); 64 : } 65 3 : }; 66 : 67 2 : if (get_sub_args().empty()) 68 : { 69 1 : cli::Cmd *help_cmd = get_app_base()->find_cmd("help"); 70 1 : if (help_cmd == nullptr) 71 : { 72 0 : std::string err_str = "Internal error: help command not registered."; 73 0 : error(err_str); 74 0 : return ESYSREPO_RESULT(ResultCode::INTERNAL_ERROR, err_str); 75 0 : } 76 1 : print_one(help_cmd, "help"); 77 1 : return ESYSREPO_RESULT(ResultCode::OK); 78 : } 79 : 80 3 : for (auto &cmd_txt : get_sub_args()) 81 : { 82 2 : cli::Cmd *cmd = get_app_base()->find_cmd(cmd_txt); 83 2 : if (cmd == nullptr) 84 : { 85 0 : error("Unknown command '" + cmd_txt + "'."); 86 0 : continue; 87 : } 88 2 : print_one(cmd, cmd_txt); 89 : } 90 1 : return ESYSREPO_RESULT(ResultCode::OK); 91 2 : } 92 : 93 : } // namespace esys::repo::exe