blob: 88ebff2998f02fd23dcc7b45ea870b90f18c223f [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
Davide Pesavento2a588152018-02-19 18:10:03 -050031#include <cerrno>
32#include <cstring>
Junxiao Shi6c135622016-11-21 14:30:33 +000033#include <unistd.h>
34
35namespace nfd {
36namespace tools {
37namespace nfdc {
38
39NDN_LOG_INIT(nfdc.Help);
40
41const int LIST_COMMAND_NAME_COLUMN_WIDTH = 16;
42
43void
44helpList(std::ostream& os, const CommandParser& parser, ParseMode mode, const std::string& noun)
45{
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050046 os << "nfdc [-h|--help] [-V|--version] <command> [<args>]\n\n";
Junxiao Shi6c135622016-11-21 14:30:33 +000047 if (noun.empty()) {
48 os << "All subcommands:\n";
49 }
50 else {
51 os << "Subcommands starting with " << noun << ":\n";
52 }
53
54 std::vector<const CommandDefinition*> commands = parser.listCommands(noun, mode);
55 if (commands.empty()) {
56 os << " (none)\n";
57 return;
58 }
59
60 for (auto def : commands) {
61 os << " " << def->getNoun() << ' ' << def->getVerb() << ' '
62 << text::Spaces{static_cast<int>(LIST_COMMAND_NAME_COLUMN_WIDTH -
63 def->getNoun().size() - def->getVerb().size() - 2)}
64 << def->getTitle() << '\n';
65 }
66
67 os << "\nSee 'nfdc help <command>' to read about a specific subcommand.\n";
68}
69
Junxiao Shi88a062d2017-01-26 03:10:05 +000070static void
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050071helpCommand(const std::string& noun, const std::string& verb)
Junxiao Shi6c135622016-11-21 14:30:33 +000072{
73 std::string manpage = "nfdc-" + noun;
74
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050075 ::execlp("man", "man", manpage.data(), nullptr);
Davide Pesavento2a588152018-02-19 18:10:03 -050076 NDN_LOG_FATAL("Error opening man page for " << manpage << ": " << std::strerror(errno));
Junxiao Shi6c135622016-11-21 14:30:33 +000077}
78
Davide Pesavento2a588152018-02-19 18:10:03 -050079int
80help(std::ostream& os, const CommandParser& parser, std::vector<std::string> args)
Junxiao Shi6c135622016-11-21 14:30:33 +000081{
Davide Pesavento2a588152018-02-19 18:10:03 -050082 const auto helpOpts = {"help", "--help", "-h"};
83 auto it = std::find_first_of(args.begin(), args.end(), helpOpts.begin(), helpOpts.end());
84 if (it == args.end())
85 return 2;
86
87 args.erase(it);
88 auto noun = args.size() > 0 ? args[0] : "";
89 auto verb = args.size() > 1 ? args[1] : "";
Junxiao Shi6c135622016-11-21 14:30:33 +000090
91 if (noun.empty()) {
Davide Pesavento2a588152018-02-19 18:10:03 -050092 helpList(os, parser);
93 return 0;
Junxiao Shi6c135622016-11-21 14:30:33 +000094 }
95 else {
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050096 helpCommand(noun, verb); // should not return
Davide Pesavento2a588152018-02-19 18:10:03 -050097 return 1;
Junxiao Shi6c135622016-11-21 14:30:33 +000098 }
99}
100
Junxiao Shi6c135622016-11-21 14:30:33 +0000101} // namespace nfdc
102} // namespace tools
103} // namespace nfd