help.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "help.hpp"
27 #include "format-helpers.hpp"
28 #include <ndn-cxx/util/logger.hpp>
29 #include <unistd.h>
30 
31 namespace nfd {
32 namespace tools {
33 namespace nfdc {
34 
35 NDN_LOG_INIT(nfdc.Help);
36 
38 
39 void
40 helpList(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 
66 static int
67 helpSingle(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);
73  return 1;
74 }
75 
76 int
77 help(ExecuteContext& ctx, const CommandParser& parser, std::ostream& os)
78 {
79  std::string noun = ctx.args.get<std::string>("noun", "");
80  std::string verb = ctx.args.get<std::string>("verb", "");
81 
82  if (noun.empty()) {
83  helpList(os, parser, ParseMode::ONE_SHOT, noun);
84  return 0;
85  }
86  else {
87  return helpSingle(noun, verb);
88  }
89 }
90 
91 void
93 {
94  CommandDefinition defHelp("help", "");
95  defHelp
96  .setTitle("display help information")
99  parser.addCommand(defHelp, bind(&help, _1, cref(parser), ref(std::cout)));
100 }
101 
102 } // namespace nfdc
103 } // namespace tools
104 } // namespace nfd
int help(ExecuteContext &ctx, const CommandParser &parser, std::ostream &os)
the 'help' command
Definition: help.cpp:77
declares semantics of a command
context for command execution
static int helpSingle(const std::string &noun, const std::string &verb)
Definition: help.cpp:67
NDN_LOG_INIT(nfdc.CommandDefinition)
T get(const std::string &key, const T &defaultValue=T()) const
CommandDefinition & setTitle(const std::string &title)
set one-line description
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
const int LIST_COMMAND_NAME_COLUMN_WIDTH
Definition: help.cpp:37
const CommandArguments & args
CommandDefinition & addArg(const std::string &name, ArgValueType valueType, Required isRequired=Required::NO, Positional allowPositional=Positional::NO, const std::string &metavar="")
declare an argument
std::vector< const CommandDefinition * > listCommands(const std::string &noun, ParseMode mode) const
list known commands for help
void registerHelpCommand(CommandParser &parser)
registers 'help' command
Definition: help.cpp:92
ParseMode
indicates which mode is the parser operated in
argument is required
CommandParser & addCommand(const CommandDefinition &def, const ExecuteCommand &execute, std::underlying_type< AvailableIn >::type modes=AVAILABLE_IN_ALL)
add an available command
argument is optional
void helpList(std::ostream &os, const CommandParser &parser, ParseMode mode, const std::string &noun)
Definition: help.cpp:40
print a number of whitespaces