tools: add exit code and streams to nfdc::ExecuteContext
Moving exit code to the context allows error handling routines to
rely only on the context.
Moving stdout and stderr streams to the context allows mocking in
unit tests, and allows sending output to a buffer for pre-processing
when we implement interactive mode.
refs #3864
Change-Id: Ibf59c12405d0eaca0597835cb2e30125b7f70adb
diff --git a/tools/nfdc/execute-command.hpp b/tools/nfdc/execute-command.hpp
index 31b2d52..c8ea5d2 100644
--- a/tools/nfdc/execute-command.hpp
+++ b/tools/nfdc/execute-command.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014-2016, Regents of the University of California,
+ * Copyright (c) 2014-2017, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -45,15 +45,18 @@
const std::string& verb;
const CommandArguments& args;
+ int exitCode; ///< program exit code
+ std::ostream& out; ///< output stream
+ std::ostream& err; ///< error stream
+
Face& face;
KeyChain& keyChain;
///\todo validator
};
/** \brief a function to execute a command
- * \return exit code
*/
-typedef std::function<int(ExecuteContext& ctx)> ExecuteCommand;
+typedef std::function<void(ExecuteContext& ctx)> ExecuteCommand;
} // namespace nfdc
} // namespace tools
diff --git a/tools/nfdc/help.cpp b/tools/nfdc/help.cpp
index 6088607..902bf67 100644
--- a/tools/nfdc/help.cpp
+++ b/tools/nfdc/help.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014-2016, Regents of the University of California,
+ * Copyright (c) 2014-2017, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -63,28 +63,27 @@
os << "\nSee 'nfdc help <command>' to read about a specific subcommand.\n";
}
-static int
+static void
helpSingle(const std::string& noun, const std::string& verb)
{
std::string manpage = "nfdc-" + noun;
execlp("man", "man", manpage.data(), nullptr);
NDN_LOG_FATAL("Error opening man page for " << manpage);
- return 1;
}
-int
-help(ExecuteContext& ctx, const CommandParser& parser, std::ostream& os)
+void
+help(ExecuteContext& ctx, const CommandParser& parser)
{
std::string noun = ctx.args.get<std::string>("noun", "");
std::string verb = ctx.args.get<std::string>("verb", "");
if (noun.empty()) {
- helpList(os, parser, ParseMode::ONE_SHOT, noun);
- return 0;
+ helpList(ctx.out, parser, ParseMode::ONE_SHOT, noun);
}
else {
- return helpSingle(noun, verb);
+ helpSingle(noun, verb); // should not return
+ ctx.exitCode = 1;
}
}
@@ -96,7 +95,7 @@
.setTitle("display help information")
.addArg("noun", ArgValueType::STRING, Required::NO, Positional::YES)
.addArg("verb", ArgValueType::STRING, Required::NO, Positional::YES);
- parser.addCommand(defHelp, bind(&help, _1, cref(parser), ref(std::cout)));
+ parser.addCommand(defHelp, bind(&help, _1, cref(parser)));
}
} // namespace nfdc
diff --git a/tools/nfdc/help.hpp b/tools/nfdc/help.hpp
index 4fd2dc5..07fce90 100644
--- a/tools/nfdc/help.hpp
+++ b/tools/nfdc/help.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014-2016, Regents of the University of California,
+ * Copyright (c) 2014-2017, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -38,8 +38,8 @@
/** \brief the 'help' command
*/
-int
-help(ExecuteContext& ctx, const CommandParser& parser, std::ostream& os);
+void
+help(ExecuteContext& ctx, const CommandParser& parser);
/** \brief registers 'help' command
*/
diff --git a/tools/nfdc/legacy-nfdc.cpp b/tools/nfdc/legacy-nfdc.cpp
index 467789e..86d9ad5 100644
--- a/tools/nfdc/legacy-nfdc.cpp
+++ b/tools/nfdc/legacy-nfdc.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014-2016, Regents of the University of California,
+ * Copyright (c) 2014-2017, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -325,7 +325,7 @@
<< std::endl;
}
-int
+void
legacyNfdcMain(ExecuteContext& ctx)
{
LegacyNfdc p(ctx.face, ctx.keyChain);
@@ -357,7 +357,8 @@
catch (const po::error& e) {
std::cerr << e.what() << std::endl;
legacyNfdcUsage();
- return 2;
+ ctx.exitCode = 2;
+ return;
}
if (wantUnsetChildInherit) {
@@ -378,17 +379,18 @@
[] (const std::string& s) { return s.empty() || s[0] == '-'; })) {
// unrecognized -option
legacyNfdcUsage();
- return 2;
+ ctx.exitCode = 2;
+ return;
}
p.m_commandLineArguments = unparsed;
bool isOk = p.dispatch(subcommand);
if (!isOk) {
legacyNfdcUsage();
- return 2;
+ ctx.exitCode = 2;
+ return;
}
ctx.face.processEvents();
- return 0;
}
} // namespace nfdc
diff --git a/tools/nfdc/legacy-nfdc.hpp b/tools/nfdc/legacy-nfdc.hpp
index 9acb228..43e82c0 100644
--- a/tools/nfdc/legacy-nfdc.hpp
+++ b/tools/nfdc/legacy-nfdc.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014-2016, Regents of the University of California,
+ * Copyright (c) 2014-2017, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -173,7 +173,7 @@
void
legacyNfdcUsage();
-int
+void
legacyNfdcMain(ExecuteContext& ctx);
} // namespace nfdc
diff --git a/tools/nfdc/legacy-status.cpp b/tools/nfdc/legacy-status.cpp
index 5eaa581..a61defd 100644
--- a/tools/nfdc/legacy-status.cpp
+++ b/tools/nfdc/legacy-status.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014-2016, Regents of the University of California,
+ * Copyright (c) 2014-2017, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -45,7 +45,7 @@
* otherwise, caller should immediately exit with the specified exit code
*/
static std::tuple<int, StatusReportOptions>
-parseCommandLine(const std::vector<std::string>& args)
+parseCommandLine(ExecuteContext& ctx, const std::vector<std::string>& args)
{
StatusReportOptions options;
@@ -67,17 +67,17 @@
po::notify(vm);
}
catch (const po::error& e) {
- std::cerr << e.what() << "\n";
- showUsage(std::cerr, cmdOptions);
+ ctx.err << e.what() << "\n";
+ showUsage(ctx.err, cmdOptions);
return std::make_tuple(2, options);
}
if (vm.count("help") > 0) {
- showUsage(std::cout, cmdOptions);
+ showUsage(ctx.out, cmdOptions);
return std::make_tuple(0, options);
}
if (vm.count("version") > 0) {
- std::cout << "nfd-status " << NFD_VERSION_BUILD_STRING << "\n";
+ ctx.out << "nfd-status " << NFD_VERSION_BUILD_STRING << "\n";
return std::make_tuple(0, options);
}
@@ -96,19 +96,20 @@
/** \brief the 'legacy-nfd-status' command
*/
-static int
+static void
legacyNfdStatus(ExecuteContext& ctx)
{
auto args = ctx.args.get<std::vector<std::string>>("args");
int exitCode = -1;
StatusReportOptions options;
- std::tie(exitCode, options) = parseCommandLine(args);
+ std::tie(exitCode, options) = parseCommandLine(ctx, args);
if (exitCode >= 0) {
- return exitCode;
+ ctx.exitCode = exitCode;
+ return;
}
- return reportStatus(ctx, options);
+ reportStatus(ctx, options);
}
void
diff --git a/tools/nfdc/main.cpp b/tools/nfdc/main.cpp
index da403c8..1f96c83 100644
--- a/tools/nfdc/main.cpp
+++ b/tools/nfdc/main.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014-2016, Regents of the University of California,
+ * Copyright (c) 2014-2017, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -63,8 +63,9 @@
try {
ndn::Face face;
ndn::KeyChain keyChain;
- ExecuteContext ctx{noun, verb, ca, face, keyChain};
- return execute(ctx);
+ ExecuteContext ctx{noun, verb, ca, 0, std::cout, std::cerr, face, keyChain};
+ execute(ctx);
+ return ctx.exitCode;
}
catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
diff --git a/tools/nfdc/status.cpp b/tools/nfdc/status.cpp
index c14faa3..e7de814 100644
--- a/tools/nfdc/status.cpp
+++ b/tools/nfdc/status.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014-2016, Regents of the University of California,
+ * Copyright (c) 2014-2017, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -37,7 +37,7 @@
namespace tools {
namespace nfdc {
-int
+void
reportStatus(ExecuteContext& ctx, const StatusReportOptions& options)
{
unique_ptr<Validator> validator = make_unique<ndn::ValidatorNull>();
@@ -75,46 +75,45 @@
uint32_t code = report.collect(ctx.face, ctx.keyChain, *validator, ctrlOptions);
if (code != 0) {
+ ctx.exitCode = 1;
// Give a simple error code for end user.
// Technical support personnel:
// 1. get the exact command from end user
// 2. code div 1000000 is zero-based section index
// 3. code mod 1000000 is a Controller.fetch error code
- std::cerr << "Error while collecting status report (" << code << ").\n";
- return 1;
+ ctx.err << "Error while collecting status report (" << code << ").\n";
}
switch (options.output) {
case ReportFormat::XML:
- report.formatXml(std::cout);
+ report.formatXml(ctx.out);
break;
case ReportFormat::TEXT:
- report.formatText(std::cout);
+ report.formatText(ctx.out);
break;
}
- return 0;
}
/** \brief single-section status command
*/
-static int
+static void
reportStatusSingleSection(ExecuteContext& ctx, bool StatusReportOptions::*wantSection)
{
StatusReportOptions options;
options.*wantSection = true;
- return reportStatus(ctx, options);
+ reportStatus(ctx, options);
}
/** \brief the 'status report' command
*/
-static int
+static void
reportStatusComprehensive(ExecuteContext& ctx)
{
StatusReportOptions options;
options.output = ctx.args.get<ReportFormat>("format", ReportFormat::TEXT);
options.wantForwarderGeneral = options.wantChannels = options.wantFaces =
options.wantFib = options.wantRib = options.wantStrategyChoice = true;
- return reportStatus(ctx, options);
+ reportStatus(ctx, options);
}
void
diff --git a/tools/nfdc/status.hpp b/tools/nfdc/status.hpp
index 57d0eb8..6cc799e 100644
--- a/tools/nfdc/status.hpp
+++ b/tools/nfdc/status.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014-2016, Regents of the University of California,
+ * Copyright (c) 2014-2017, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -46,7 +46,7 @@
/** \brief collect a status report and write to stdout
*/
-int
+void
reportStatus(ExecuteContext& ctx, const StatusReportOptions& options);
/** \brief registers status commands