Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 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" |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 28 | |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 29 | #include <ndn-cxx/util/logger.hpp> |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 30 | |
Davide Pesavento | 2a58815 | 2018-02-19 18:10:03 -0500 | [diff] [blame] | 31 | #include <cerrno> |
| 32 | #include <cstring> |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 33 | #include <unistd.h> |
| 34 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 35 | namespace nfd::tools::nfdc { |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 36 | |
| 37 | NDN_LOG_INIT(nfdc.Help); |
| 38 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 39 | constexpr int LIST_COMMAND_NAME_COLUMN_WIDTH = 16; |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 40 | |
| 41 | void |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 42 | helpList(std::ostream& os, const CommandParser& parser, ParseMode mode, std::string_view noun) |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 43 | { |
Alexander Afanasyev | c414ca5 | 2021-06-09 12:15:19 -0400 | [diff] [blame] | 44 | os << "nfdc [-h|--help] [-V|--version] [-f|--batch <batch-file>] [<command> [<args>]]\n\n"; |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 45 | if (noun.empty()) { |
| 46 | os << "All subcommands:\n"; |
| 47 | } |
| 48 | else { |
| 49 | os << "Subcommands starting with " << noun << ":\n"; |
| 50 | } |
| 51 | |
| 52 | std::vector<const CommandDefinition*> commands = parser.listCommands(noun, mode); |
| 53 | if (commands.empty()) { |
| 54 | os << " (none)\n"; |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | for (auto def : commands) { |
| 59 | os << " " << def->getNoun() << ' ' << def->getVerb() << ' ' |
| 60 | << text::Spaces{static_cast<int>(LIST_COMMAND_NAME_COLUMN_WIDTH - |
| 61 | def->getNoun().size() - def->getVerb().size() - 2)} |
| 62 | << def->getTitle() << '\n'; |
| 63 | } |
| 64 | |
| 65 | os << "\nSee 'nfdc help <command>' to read about a specific subcommand.\n"; |
| 66 | } |
| 67 | |
Junxiao Shi | 88a062d | 2017-01-26 03:10:05 +0000 | [diff] [blame] | 68 | static void |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 69 | helpCommand(const std::string& noun, const std::string& verb) |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 70 | { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 71 | const std::string manpage = "nfdc-" + noun; |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 72 | |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 73 | ::execlp("man", "man", manpage.data(), nullptr); |
Davide Pesavento | 2a58815 | 2018-02-19 18:10:03 -0500 | [diff] [blame] | 74 | NDN_LOG_FATAL("Error opening man page for " << manpage << ": " << std::strerror(errno)); |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Davide Pesavento | 2a58815 | 2018-02-19 18:10:03 -0500 | [diff] [blame] | 77 | int |
| 78 | help(std::ostream& os, const CommandParser& parser, std::vector<std::string> args) |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 79 | { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 80 | const auto helpOpts = {"help"sv, "--help"sv, "-h"sv}; |
Davide Pesavento | 2a58815 | 2018-02-19 18:10:03 -0500 | [diff] [blame] | 81 | auto it = std::find_first_of(args.begin(), args.end(), helpOpts.begin(), helpOpts.end()); |
| 82 | if (it == args.end()) |
| 83 | return 2; |
| 84 | |
| 85 | args.erase(it); |
| 86 | auto noun = args.size() > 0 ? args[0] : ""; |
| 87 | auto verb = args.size() > 1 ? args[1] : ""; |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 88 | |
| 89 | if (noun.empty()) { |
Davide Pesavento | 2a58815 | 2018-02-19 18:10:03 -0500 | [diff] [blame] | 90 | helpList(os, parser); |
| 91 | return 0; |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 92 | } |
| 93 | else { |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 94 | helpCommand(noun, verb); // should not return |
Davide Pesavento | 2a58815 | 2018-02-19 18:10:03 -0500 | [diff] [blame] | 95 | return 1; |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 99 | } // namespace nfd::tools::nfdc |