| 1 |
#include <pbcopper/cli2/CLI.h> |
| 2 |
|
| 3 |
#include <pbcopper/cli2/Results.h> |
| 4 |
#include <pbcopper/cli2/internal/BuiltinOptions.h> |
| 5 |
#include <pbcopper/cli2/internal/CommandLineParser.h> |
| 6 |
#include <pbcopper/cli2/internal/InterfaceHelpPrinter.h> |
| 7 |
#include <pbcopper/cli2/internal/MultiToolInterfaceHelpPrinter.h> |
| 8 |
#include <pbcopper/cli2/internal/VersionPrinter.h> |
| 9 |
#include <pbcopper/logging/Logging.h> |
| 10 |
#include <pbcopper/utility/Alarm.h> |
| 11 |
|
| 12 |
#include <exception> |
| 13 |
#include <filesystem> |
| 14 |
#include <fstream> |
| 15 |
#include <iostream> |
| 16 |
#include <string> |
| 17 |
|
| 18 |
#include <cassert> |
| 19 |
#include <cstdlib> |
| 20 |
|
| 21 |
using CommandLineParser = PacBio::CLI_v2::internal::CommandLineParser; |
| 22 |
using InterfaceHelpPrinter = PacBio::CLI_v2::internal::InterfaceHelpPrinter; |
| 23 |
using MultiToolInterfaceHelpPrinter = PacBio::CLI_v2::internal::MultiToolInterfaceHelpPrinter; |
| 24 |
|
| 25 |
namespace PacBio { |
| 26 |
namespace CLI_v2 { |
| 27 |
|
| 28 |
bool LogLevelIsUserProvided(const Interface& interface, const Results& results) |
| 29 |
{ |
| 30 |
const auto& logLevelOption = interface.LogLevelOption(); |
| 31 |
if (!logLevelOption) { |
| 32 |
return false; |
| 33 |
} |
| 34 |
|
| 35 |
assert(!logLevelOption->Names.empty()); |
| 36 |
const auto& logLevelResult = results[Builtin::LogLevel]; |
| 37 |
return logLevelResult.IsUserProvided(); |
| 38 |
} |
| 39 |
|
| 40 |
int Run(int argc, char* argv[], const Interface& interface, const ResultsHandler& handler) |
| 41 |
{ |
| 42 |
return Run(std::vector<std::string>{argv, argv + argc}, interface, handler); |
| 43 |
} |
| 44 |
|
| 45 |
int Run(const std::vector<std::string>& args, const Interface& interface, |
| 46 |
const ResultsHandler& handler) |
| 47 |
{ |
| 48 |
// |
| 49 |
// If application name only & positional arguments are required, show help. |
| 50 |
// This allows, for example, tools that to stream stdin without requiring |
| 51 |
// input filenames. |
| 52 |
// |
| 53 |
if (args.size() == 1 && (interface.HasRequiredPosArgs() || interface.HasRequiredOptions())) { |
| 54 |
const InterfaceHelpPrinter help{interface, internal::HiddenOptionMode::HIDE}; |
| 55 |
std::cout << help; |
| 56 |
return EXIT_SUCCESS; |
| 57 |
} |
| 58 |
|
| 59 |
// parse command line args |
| 60 |
Results results; |
| 61 |
try { |
| 62 |
CommandLineParser parser{interface}; |
| 63 |
results = parser.Parse(args); |
| 64 |
} catch (const std::exception& e) { |
| 65 |
// No logging or application workflow error-handling (e.g. alarms) have |
| 66 |
// been configured yet. Print a graceful message rather than let any |
| 67 |
// uncaught CLI parsing exceptions trigger terminate(). |
| 68 |
std::cerr << interface.ApplicationName() << " ERROR: " << e.what() << '\n'; |
| 69 |
return EXIT_FAILURE; |
| 70 |
} |
| 71 |
|
| 72 |
// help |
| 73 |
if (results[Builtin::Help] || results[Builtin::ShowAllHelp]) { |
| 74 |
const internal::HiddenOptionMode showHiddenOptions = results[Builtin::ShowAllHelp] |
| 75 |
? internal::HiddenOptionMode::SHOW |
| 76 |
: internal::HiddenOptionMode::HIDE; |
| 77 |
const InterfaceHelpPrinter help{interface, showHiddenOptions}; |
| 78 |
std::cout << help; |
| 79 |
return EXIT_SUCCESS; |
| 80 |
} |
| 81 |
|
| 82 |
// version |
| 83 |
if (results[Builtin::Version]) { |
| 84 |
interface.PrintVersion(); |
| 85 |
return EXIT_SUCCESS; |
| 86 |
} |
| 87 |
|
| 88 |
const std::filesystem::path alarmsOutputFilename{results.AlarmsFile()}; |
| 89 |
const bool allowExceptionsPassthrough{results.ExceptionPassthrough()}; |
| 90 |
|
| 91 |
// |
| 92 |
// Initialize logging |
| 93 |
// |
| 94 |
// Setup logging configuration. Priority is given in the following (decreasing) order: |
| 95 |
// * command line-provided options |
| 96 |
// * multi-tool interface config |
| 97 |
// * interface config |
| 98 |
// |
| 99 |
// If 'log-level' or 'log-file' were set via command line, use it. Otherwise, |
| 100 |
// use the application's default config provided. |
| 101 |
// |
| 102 |
// Also ensures that the Results object passed to the application runner |
| 103 |
// reflects the actual, active log level. |
| 104 |
// |
| 105 |
Logging::LogConfig logConfig = interface.LogConfig(); |
| 106 |
if (LogLevelIsUserProvided(interface, results)) { |
| 107 |
logConfig.Level = results.LogLevel(); |
| 108 |
} else { |
| 109 |
results.AddObservedValue("log-level", logConfig.Level.ToString(), SetByMode::DEFAULT); |
| 110 |
} |
| 111 |
|
| 112 |
const auto logger = [&]() { |
| 113 |
if (interface.LogFileOption()) { |
| 114 |
const auto logFile = results.LogFile(); |
| 115 |
if (!logFile.empty()) { |
| 116 |
return std::make_unique<Logging::Logger>(logFile, logConfig); |
| 117 |
} |
| 118 |
} |
| 119 |
return std::make_unique<Logging::Logger>(std::cerr, logConfig); |
| 120 |
}(); |
| 121 |
|
| 122 |
Logging::Logger::Current(logger.get()); |
| 123 |
|
| 124 |
// bypass exception logging/alarm on exit, if requested |
| 125 |
if (allowExceptionsPassthrough) { |
| 126 |
return handler(results); |
| 127 |
} |
| 128 |
|
| 129 |
const auto& applicationName = interface.ApplicationName(); |
| 130 |
|
| 131 |
auto printToAlarmFile = [&alarmsOutputFilename, &applicationName](const Utility::Alarm alarm) { |
| 132 |
std::ofstream alarmsOutput{alarmsOutputFilename}; |
| 133 |
if (alarmsOutput) { |
| 134 |
Utility::Alarm::WriteAlarms(alarmsOutput, {alarm}, applicationName); |
| 135 |
} else { |
| 136 |
PBLOG_FATAL << "Could not rewrite alarms JSON to " << alarmsOutputFilename; |
| 137 |
} |
| 138 |
}; |
| 139 |
|
| 140 |
// run application |
| 141 |
try { |
| 142 |
return handler(results); |
| 143 |
} catch (const Utility::AlarmException& a) { |
| 144 |
Logging::LogMessage(a.SourceFilename(), a.FunctionName(), a.LineNumber(), |
| 145 |
Logging::LogLevel::FATAL, Logging::Logger::Current()) |
| 146 |
<< applicationName << " ERROR: " << a.Message(); |
| 147 |
if (!alarmsOutputFilename.empty()) { |
| 148 |
printToAlarmFile( |
| 149 |
Utility::Alarm{a.Name(), a.Message(), a.Severity(), a.Info(), a.Exception()}); |
| 150 |
} |
| 151 |
} catch (const std::exception& e) { |
| 152 |
PBLOG_FATAL << applicationName << " ERROR: " << e.what(); |
| 153 |
if (!alarmsOutputFilename.empty()) { |
| 154 |
printToAlarmFile( |
| 155 |
Utility::Alarm{applicationName, e.what(), "FATAL", "", "std::exception"}); |
| 156 |
} |
| 157 |
} catch (...) { |
| 158 |
PBLOG_FATAL << "caught unknown exception type"; |
| 159 |
if (!alarmsOutputFilename.empty()) { |
| 160 |
printToAlarmFile(Utility::Alarm{applicationName, "", "FATAL", "", "unknown exception"}); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
return EXIT_FAILURE; |
| 165 |
} |
| 166 |
|
| 167 |
int Run(int argc, char* argv[], const MultiToolInterface& interface) |
| 168 |
{ |
| 169 |
return Run(std::vector<std::string>{argv, argv + argc}, interface); |
| 170 |
} |
| 171 |
|
| 172 |
int Run(const std::vector<std::string>& args, const MultiToolInterface& interface) |
| 173 |
{ |
| 174 |
assert(!args.empty()); |
| 175 |
|
| 176 |
// |
| 177 |
// if application name only, go straight to help |
| 178 |
// |
| 179 |
// "$ tool" |
| 180 |
// |
| 181 |
if (args.size() == 1) { |
| 182 |
const MultiToolInterfaceHelpPrinter help{interface, internal::HiddenOptionMode::HIDE}; |
| 183 |
std::cout << help; |
| 184 |
return EXIT_SUCCESS; |
| 185 |
} |
| 186 |
|
| 187 |
// |
| 188 |
// otherwise fetch subtool interface, pop top-level application name, and run |
| 189 |
// |
| 190 |
// "$ tool subtool" |
| 191 |
// "$ tool subtool ..." |
| 192 |
// |
| 193 |
if (interface.HasTool(args.at(1))) { |
| 194 |
const Tool& tool = interface.ToolFor(args.at(1)); |
| 195 |
const std::vector<std::string> toolArgs{args.cbegin() + 1, args.cend()}; |
| 196 |
return Run(toolArgs, tool.interface, tool.runner); |
| 197 |
} |
| 198 |
|
| 199 |
// |
| 200 |
// command line has a second arg that is not a subtool, but could still be |
| 201 |
// top-level help/version |
| 202 |
// |
| 203 |
// "$ multi-tool -h" |
| 204 |
// "$ multi-tool --help" |
| 205 |
// "$ multi-tool --show-all-help" |
| 206 |
// "$ multi-tool --all-help" |
| 207 |
// "$ multi-tool --version" |
| 208 |
// |
| 209 |
// we have not done full command-line parsing (saving that for each subtool), |
| 210 |
// so do "manual" check here on special options |
| 211 |
// |
| 212 |
for (const auto& arg : args) { |
| 213 |
if ((arg == "-h") || (arg == "--help") || (arg == "--show-all-help") || |
| 214 |
(arg == "--all-help")) { |
| 215 |
const internal::HiddenOptionMode showHiddenOptions = |
| 216 |
((arg == "--show-all-help") || (arg == "--all-help")) |
| 217 |
? internal::HiddenOptionMode::SHOW |
| 218 |
: internal::HiddenOptionMode::HIDE; |
| 219 |
const MultiToolInterfaceHelpPrinter help{interface, showHiddenOptions}; |
| 220 |
std::cout << help; |
| 221 |
return EXIT_SUCCESS; |
| 222 |
} else if (arg == "--version") { |
| 223 |
interface.PrintVersion(); |
| 224 |
return EXIT_SUCCESS; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
// no matching tool |
| 229 |
std::cerr << interface.ApplicationName() |
| 230 |
<< " ERROR: [pbcopper] command line ERROR: unknown tool '" + args.at(1) + |
| 231 |
"' requested\n"; |
| 232 |
return EXIT_FAILURE; |
| 233 |
} |
| 234 |
|
| 235 |
} // namespace CLI_v2 |
| 236 |
} // namespace PacBio |