blob: 902bf67a40fddc56df98c4e55970742c7a708850 [file] [log] [blame]
Junxiao Shi6c135622016-11-21 14:30:33 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi88a062d2017-01-26 03:10:05 +00003 * Copyright (c) 2014-2017, 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"
28#include <ndn-cxx/util/logger.hpp>
29#include <unistd.h>
30
31namespace nfd {
32namespace tools {
33namespace nfdc {
34
35NDN_LOG_INIT(nfdc.Help);
36
37const int LIST_COMMAND_NAME_COLUMN_WIDTH = 16;
38
39void
40helpList(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
Junxiao Shi88a062d2017-01-26 03:10:05 +000066static void
Junxiao Shi6c135622016-11-21 14:30:33 +000067helpSingle(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);
Junxiao Shi6c135622016-11-21 14:30:33 +000073}
74
Junxiao Shi88a062d2017-01-26 03:10:05 +000075void
76help(ExecuteContext& ctx, const CommandParser& parser)
Junxiao Shi6c135622016-11-21 14:30:33 +000077{
78 std::string noun = ctx.args.get<std::string>("noun", "");
79 std::string verb = ctx.args.get<std::string>("verb", "");
80
81 if (noun.empty()) {
Junxiao Shi88a062d2017-01-26 03:10:05 +000082 helpList(ctx.out, parser, ParseMode::ONE_SHOT, noun);
Junxiao Shi6c135622016-11-21 14:30:33 +000083 }
84 else {
Junxiao Shi88a062d2017-01-26 03:10:05 +000085 helpSingle(noun, verb); // should not return
86 ctx.exitCode = 1;
Junxiao Shi6c135622016-11-21 14:30:33 +000087 }
88}
89
90void
91registerHelpCommand(CommandParser& parser)
92{
93 CommandDefinition defHelp("help", "");
94 defHelp
95 .setTitle("display help information")
96 .addArg("noun", ArgValueType::STRING, Required::NO, Positional::YES)
97 .addArg("verb", ArgValueType::STRING, Required::NO, Positional::YES);
Junxiao Shi88a062d2017-01-26 03:10:05 +000098 parser.addCommand(defHelp, bind(&help, _1, cref(parser)));
Junxiao Shi6c135622016-11-21 14:30:33 +000099}
100
101} // namespace nfdc
102} // namespace tools
103} // namespace nfd