Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "help.hpp" |
| 27 | #include "format-helpers.hpp" |
| 28 | #include <ndn-cxx/util/logger.hpp> |
| 29 | #include <unistd.h> |
| 30 | |
| 31 | namespace nfd { |
| 32 | namespace tools { |
| 33 | namespace nfdc { |
| 34 | |
| 35 | NDN_LOG_INIT(nfdc.Help); |
| 36 | |
| 37 | const int LIST_COMMAND_NAME_COLUMN_WIDTH = 16; |
| 38 | |
| 39 | void |
| 40 | helpList(std::ostream& os, const CommandParser& parser, ParseMode mode, const std::string& noun) |
| 41 | { |
| 42 | os << "nfdc [-h] [-V] <command> [<args>]\n\n"; |
| 43 | if (noun.empty()) { |
| 44 | os << "All subcommands:\n"; |
| 45 | } |
| 46 | else { |
| 47 | os << "Subcommands starting with " << noun << ":\n"; |
| 48 | } |
| 49 | |
| 50 | std::vector<const CommandDefinition*> commands = parser.listCommands(noun, mode); |
| 51 | if (commands.empty()) { |
| 52 | os << " (none)\n"; |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | for (auto def : commands) { |
| 57 | os << " " << def->getNoun() << ' ' << def->getVerb() << ' ' |
| 58 | << text::Spaces{static_cast<int>(LIST_COMMAND_NAME_COLUMN_WIDTH - |
| 59 | def->getNoun().size() - def->getVerb().size() - 2)} |
| 60 | << def->getTitle() << '\n'; |
| 61 | } |
| 62 | |
| 63 | os << "\nSee 'nfdc help <command>' to read about a specific subcommand.\n"; |
| 64 | } |
| 65 | |
| 66 | static int |
| 67 | helpSingle(const std::string& noun, const std::string& verb) |
| 68 | { |
| 69 | std::string manpage = "nfdc-" + noun; |
| 70 | |
| 71 | execlp("man", "man", manpage.data(), nullptr); |
| 72 | NDN_LOG_FATAL("Error opening man page for " << manpage); |
| 73 | return 1; |
| 74 | } |
| 75 | |
| 76 | int |
| 77 | help(ExecuteContext& ctx, const CommandParser& parser, std::ostream& os) |
| 78 | { |
| 79 | std::string noun = ctx.args.get<std::string>("noun", ""); |
| 80 | std::string verb = ctx.args.get<std::string>("verb", ""); |
| 81 | |
| 82 | if (noun.empty()) { |
| 83 | helpList(os, parser, ParseMode::ONE_SHOT, noun); |
| 84 | return 0; |
| 85 | } |
| 86 | else { |
| 87 | return helpSingle(noun, verb); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | registerHelpCommand(CommandParser& parser) |
| 93 | { |
| 94 | CommandDefinition defHelp("help", ""); |
| 95 | defHelp |
| 96 | .setTitle("display help information") |
| 97 | .addArg("noun", ArgValueType::STRING, Required::NO, Positional::YES) |
| 98 | .addArg("verb", ArgValueType::STRING, Required::NO, Positional::YES); |
| 99 | parser.addCommand(defHelp, bind(&help, _1, cref(parser), ref(std::cout))); |
| 100 | } |
| 101 | |
| 102 | } // namespace nfdc |
| 103 | } // namespace tools |
| 104 | } // namespace nfd |