blob: e67992c29b7e2e658ed01e3c923d1a9221779fa3 [file] [log] [blame]
Junxiao Shi6c135622016-11-21 14:30:33 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -05002/*
3 * Copyright (c) 2014-2018, Regents of the University of California,
Junxiao Shi6c135622016-11-21 14:30:33 +00004 * 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 Pesaventoe0bae0f2018-02-17 22:07:52 -050028
Junxiao Shi6c135622016-11-21 14:30:33 +000029#include <ndn-cxx/util/logger.hpp>
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050030
Junxiao Shi6c135622016-11-21 14:30:33 +000031#include <unistd.h>
32
33namespace nfd {
34namespace tools {
35namespace nfdc {
36
37NDN_LOG_INIT(nfdc.Help);
38
39const int LIST_COMMAND_NAME_COLUMN_WIDTH = 16;
40
41void
42helpList(std::ostream& os, const CommandParser& parser, ParseMode mode, const std::string& noun)
43{
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050044 os << "nfdc [-h|--help] [-V|--version] <command> [<args>]\n\n";
Junxiao Shi6c135622016-11-21 14:30:33 +000045 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 Shi88a062d2017-01-26 03:10:05 +000068static void
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050069helpCommand(const std::string& noun, const std::string& verb)
Junxiao Shi6c135622016-11-21 14:30:33 +000070{
71 std::string manpage = "nfdc-" + noun;
72
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050073 ::execlp("man", "man", manpage.data(), nullptr);
Junxiao Shi6c135622016-11-21 14:30:33 +000074 NDN_LOG_FATAL("Error opening man page for " << manpage);
Junxiao Shi6c135622016-11-21 14:30:33 +000075}
76
Junxiao Shi88a062d2017-01-26 03:10:05 +000077void
78help(ExecuteContext& ctx, const CommandParser& parser)
Junxiao Shi6c135622016-11-21 14:30:33 +000079{
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050080 auto noun = ctx.args.get<std::string>("noun", "");
81 auto verb = ctx.args.get<std::string>("verb", "");
Junxiao Shi6c135622016-11-21 14:30:33 +000082
83 if (noun.empty()) {
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050084 helpList(ctx.out, parser);
Junxiao Shi6c135622016-11-21 14:30:33 +000085 }
86 else {
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050087 helpCommand(noun, verb); // should not return
Junxiao Shi88a062d2017-01-26 03:10:05 +000088 ctx.exitCode = 1;
Junxiao Shi6c135622016-11-21 14:30:33 +000089 }
90}
91
92void
93registerHelpCommand(CommandParser& parser)
94{
95 CommandDefinition defHelp("help", "");
96 defHelp
97 .setTitle("display help information")
98 .addArg("noun", ArgValueType::STRING, Required::NO, Positional::YES)
99 .addArg("verb", ArgValueType::STRING, Required::NO, Positional::YES);
Junxiao Shi88a062d2017-01-26 03:10:05 +0000100 parser.addCommand(defHelp, bind(&help, _1, cref(parser)));
Junxiao Shi6c135622016-11-21 14:30:33 +0000101}
102
103} // namespace nfdc
104} // namespace tools
105} // namespace nfd