legacy-status.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "legacy-status.hpp"
27 #include "core/version.hpp"
28 
29 #include <boost/program_options.hpp>
30 
31 namespace nfd {
32 namespace tools {
33 namespace nfdc {
34 
35 static void
36 showUsage(std::ostream& os, const boost::program_options::options_description& cmdOptions)
37 {
38  os << "Usage: nfd-status [options]\n\n"
39  << "Show NFD version and status information.\n\n"
40  << cmdOptions;
41 }
42 
47 static std::tuple<int, StatusReportOptions>
48 parseCommandLine(const std::vector<std::string>& args)
49 {
50  StatusReportOptions options;
51 
52  namespace po = boost::program_options;
53  po::options_description cmdOptions("StatusReportOptions");
54  cmdOptions.add_options()
55  ("help,h", "print this help message")
56  ("version,V", "show program version")
57  ("general,v", po::bool_switch(&options.wantForwarderGeneral), "show general status")
58  ("channels,c", po::bool_switch(&options.wantChannels), "show channels")
59  ("faces,f", po::bool_switch(&options.wantFaces), "show faces")
60  ("fib,b", po::bool_switch(&options.wantFib), "show FIB entries")
61  ("rib,r", po::bool_switch(&options.wantRib), "show RIB routes")
62  ("sc,s", po::bool_switch(&options.wantStrategyChoice), "show strategy choice entries")
63  ("xml,x", "output as XML instead of text (implies -vcfbrs)");
64  po::variables_map vm;
65  try {
66  po::store(po::command_line_parser(args).options(cmdOptions).run(), vm);
67  po::notify(vm);
68  }
69  catch (const po::error& e) {
70  std::cerr << e.what() << "\n";
71  showUsage(std::cerr, cmdOptions);
72  return std::make_tuple(2, options);
73  }
74 
75  if (vm.count("help") > 0) {
76  showUsage(std::cout, cmdOptions);
77  return std::make_tuple(0, options);
78  }
79  if (vm.count("version") > 0) {
80  std::cout << "nfd-status " << NFD_VERSION_BUILD_STRING << "\n";
81  return std::make_tuple(0, options);
82  }
83 
84  if (vm.count("xml") > 0) {
85  options.output = ReportFormat::XML;
86  }
87  if (options.output == ReportFormat::XML ||
88  (!options.wantForwarderGeneral && !options.wantChannels && !options.wantFaces &&
89  !options.wantFib && !options.wantRib && !options.wantStrategyChoice)) {
90  options.wantForwarderGeneral = options.wantChannels = options.wantFaces =
91  options.wantFib = options.wantRib = options.wantStrategyChoice = true;
92  }
93 
94  return std::make_tuple(-1, options);
95 }
96 
99 static int
101 {
102  auto args = ctx.args.get<std::vector<std::string>>("args");
103 
104  int exitCode = -1;
105  StatusReportOptions options;
106  std::tie(exitCode, options) = parseCommandLine(args);
107  if (exitCode >= 0) {
108  return exitCode;
109  }
110 
111  return reportStatus(ctx, options);
112 }
113 
114 void
116 {
117  CommandDefinition defLegacyNfdStatus("legacy-nfd-status", "");
118  defLegacyNfdStatus
120  parser.addCommand(defLegacyNfdStatus, &legacyNfdStatus, AVAILABLE_IN_ALL & ~AVAILABLE_IN_HELP);
121 }
122 
123 } // namespace nfdc
124 } // namespace tools
125 } // namespace nfd
declares semantics of a command
context for command execution
static int legacyNfdStatus(ExecuteContext &ctx)
the 'legacy-nfd-status' command
T get(const std::string &key, const T &defaultValue=T()) const
static std::tuple< int, StatusReportOptions > parseCommandLine(const std::vector< std::string > &args)
parse legacy nfd-status command line, and show usage if necessary
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
int reportStatus(ExecuteContext &ctx, const StatusReportOptions &options)
collect a status report and write to stdout
Definition: status.cpp:41
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
argument is required
static void showUsage(std::ostream &os, const boost::program_options::options_description &cmdOptions)
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 registerLegacyStatusCommand(CommandParser &parser)
registers 'legacy-nfd-status' command