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 void
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 }
74 
75 void
76 help(ExecuteContext& ctx, const CommandParser& parser)
77 {
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()) {
82  helpList(ctx.out, parser, ParseMode::ONE_SHOT, noun);
83  }
84  else {
85  helpSingle(noun, verb); // should not return
86  ctx.exitCode = 1;
87  }
88 }
89 
90 void
92 {
93  CommandDefinition defHelp("help", "");
94  defHelp
95  .setTitle("display help information")
98  parser.addCommand(defHelp, bind(&help, _1, cref(parser)));
99 }
100 
101 } // namespace nfdc
102 } // namespace tools
103 } // namespace nfd
const CommandArguments & args
declares semantics of a command
std::ostream & out
output stream
context for command execution
void help(ExecuteContext &ctx, const CommandParser &parser)
the &#39;help&#39; command
Definition: help.cpp:76
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...
Definition: algorithm.hpp:32
const int LIST_COMMAND_NAME_COLUMN_WIDTH
Definition: help.cpp:37
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 &#39;help&#39; command
Definition: help.cpp:91
ParseMode
indicates which mode is the parser operated in
argument is required
static void helpSingle(const std::string &noun, const std::string &verb)
Definition: help.cpp:67
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