help.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2018, 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 
29 #include <ndn-cxx/util/logger.hpp>
30 
31 #include <cerrno>
32 #include <cstring>
33 #include <unistd.h>
34 
35 namespace nfd {
36 namespace tools {
37 namespace nfdc {
38 
39 NDN_LOG_INIT(nfdc.Help);
40 
42 
43 void
44 helpList(std::ostream& os, const CommandParser& parser, ParseMode mode, const std::string& noun)
45 {
46  os << "nfdc [-h|--help] [-V|--version] <command> [<args>]\n\n";
47  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 
70 static void
71 helpCommand(const std::string& noun, const std::string& verb)
72 {
73  std::string manpage = "nfdc-" + noun;
74 
75  ::execlp("man", "man", manpage.data(), nullptr);
76  NDN_LOG_FATAL("Error opening man page for " << manpage << ": " << std::strerror(errno));
77 }
78 
79 int
80 help(std::ostream& os, const CommandParser& parser, std::vector<std::string> args)
81 {
82  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] : "";
90 
91  if (noun.empty()) {
92  helpList(os, parser);
93  return 0;
94  }
95  else {
96  helpCommand(noun, verb); // should not return
97  return 1;
98  }
99 }
100 
101 } // namespace nfdc
102 } // namespace tools
103 } // namespace nfd
NDN_LOG_INIT(nfdc.CommandDefinition)
int help(std::ostream &os, const CommandParser &parser, std::vector< std::string > args)
tries to help the user, if requested on the command line
Definition: help.cpp:80
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
const int LIST_COMMAND_NAME_COLUMN_WIDTH
Definition: help.cpp:41
std::vector< const CommandDefinition * > listCommands(const std::string &noun, ParseMode mode) const
list known commands for help
ParseMode
indicates which mode is the parser operated in
void helpList(std::ostream &os, const CommandParser &parser, ParseMode mode, const std::string &noun)
writes the list of available commands to a stream
Definition: help.cpp:44
print a number of whitespaces
static void helpCommand(const std::string &noun, const std::string &verb)
Definition: help.cpp:71