src/cli/main.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include "RubyCLI.hpp" | ||
| 7 | #include "MeasureUpdateCommand.hpp" | ||
| 8 | #include "RunCommand.hpp" | ||
| 9 | #include "UpdateCommand.hpp" | ||
| 10 | #include "../scriptengine/ScriptEngine.hpp" | ||
| 11 | #include "../workflow/OSWorkflow.hpp" | ||
| 12 | #include "../utilities/core/ASCIIStrings.hpp" | ||
| 13 | #include "../utilities/core/Logger.hpp" | ||
| 14 | #include "../utilities/core/StringStreamLogSink.hpp" | ||
| 15 | #include "../utilities/bcl/BCLMeasure.hpp" | ||
| 16 | #include "../measure/ModelMeasure.hpp" | ||
| 17 | #include "../measure/EnergyPlusMeasure.hpp" | ||
| 18 | #include "../measure/ReportingMeasure.hpp" | ||
| 19 | #include "../measure/OSMeasureInfoGetter.hpp" | ||
| 20 | |||
| 21 | #include <OpenStudio.hxx> | ||
| 22 | |||
| 23 | #include <fmt/format.h> | ||
| 24 | #include <fmt/color.h> | ||
| 25 | #include <fmt/ranges.h> // for std::vector format | ||
| 26 | |||
| 27 | #include <CLI/CLI.hpp> | ||
| 28 | |||
| 29 | #include <algorithm> | ||
| 30 | #include <iterator> | ||
| 31 | #include <string> | ||
| 32 | #include <string_view> | ||
| 33 | #include <vector> | ||
| 34 | |||
| 35 | 276 | int main(int argc, char* argv[]) { | |
| 36 | |||
| 37 | 276 | constexpr auto rubySpecificOptionsGroupName = "Ruby Options"; | |
| 38 | 276 | constexpr auto pythonSpecificOptionsGroupName = "Python Options"; | |
| 39 | |||
| 40 | // constexpr auto executeGroupName = "Script Execution"; | ||
| 41 | 276 | constexpr auto versionGroupname = "Version Info"; | |
| 42 | |||
| 43 | 276 | int result = 0; | |
| 44 | |||
| 45 |
1/1✓ Branch 1 taken 276 times.
|
552 | std::vector<std::string> args(argv, std::next(argv, static_cast<std::ptrdiff_t>(argc))); |
| 46 | // fmt::print("Original arguments: {}\n", args); | ||
| 47 |
1/1✓ Branch 3 taken 276 times.
|
276 | std::for_each(args.begin(), args.end(), [](auto& s) { |
| 48 | 1049 | openstudio::ascii_trim(s); | |
| 49 | // Windows cmd.exe treats single quotes as regular chars so we strip them if found (cf #4779 and #4834) | ||
| 50 |
5/6✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1047 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 1047 times.
|
1049 | if (s.starts_with('\'') && s.ends_with('\'')) { |
| 51 |
1/1✓ Branch 3 taken 2 times.
|
4 | s.erase(std::prev(s.end())); |
| 52 |
1/1✓ Branch 3 taken 2 times.
|
2 | s.erase(s.begin()); |
| 53 | } | ||
| 54 | 1049 | }); | |
| 55 | // fmt::print("Cleaned arguments: {}\n", args); | ||
| 56 | // erase the first element, which is the name of the program | ||
| 57 | 276 | const std::string programName = std::move(args.front()); | |
| 58 |
1/1✓ Branch 3 taken 276 times.
|
276 | args.erase(args.begin()); |
| 59 | |||
| 60 |
4/5✓ Branch 1 taken 276 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 276 times.
✓ Branch 7 taken 14 times.
✓ Branch 8 taken 262 times.
|
276 | const bool is_classic = !args.empty() && (args[0] == "classic"); |
| 61 | |||
| 62 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 262 times.
|
276 | if (is_classic) { |
| 63 | // The "classic" cli implementation will not be expecting the first arg | ||
| 64 | // to be the word classic so we need to remove the first arg | ||
| 65 |
1/1✓ Branch 3 taken 14 times.
|
14 | args.erase(args.begin()); |
| 66 | } else { | ||
| 67 | // Replace backward slashes with forward slashes... cf #4856 | ||
| 68 |
1/1✓ Branch 4 taken 262 times.
|
968 | std::for_each(args.begin(), args.end(), [](auto& s) { boost::replace_all(s, "\\", "\\\\"); }); |
| 69 | } | ||
| 70 | |||
| 71 | // ScriptEngineInstance will delay load the engines | ||
| 72 |
2/2✓ Branch 1 taken 276 times.
✓ Branch 5 taken 276 times.
|
276 | openstudio::ScriptEngineInstance rubyEngine("rubyengine", args); |
| 73 |
2/2✓ Branch 1 taken 276 times.
✓ Branch 5 taken 276 times.
|
276 | openstudio::ScriptEngineInstance pythonEngine("pythonengine", args); |
| 74 | |||
| 75 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 262 times.
|
276 | if (is_classic) { |
| 76 |
1/1✓ Branch 2 taken 14 times.
|
14 | fmt::print(fmt::fg(fmt::color::orange), |
| 77 | "┌{0:─^{2}}┐\n" | ||
| 78 | "│{1: ^{2}}│\n" | ||
| 79 | "└{0:─^{2}}┘", | ||
| 80 | 14 | "", "The `classic` command is deprecated and will be removed in a future release", 80); | |
| 81 |
1/1✓ Branch 1 taken 14 times.
|
14 | fmt::print("\n"); |
| 82 |
1/1✓ Branch 1 taken 14 times.
|
14 | result = openstudio::rubyCLI(rubyEngine); |
| 83 | } else { | ||
| 84 |
3/3✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
|
786 | CLI::App app{"openstudio"}; |
| 85 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
262 | app.name(programName); |
| 86 | |||
| 87 | // Preprocess the arguments, insert execute_xxx_script if a script is passed but the previous arg isn't the command. | ||
| 88 | // So you can ommit "execute_xxx_script" like historical behavior: `openstudio --include INCLUDE_DIR test.rb` | ||
| 89 |
7/7✓ Branch 1 taken 657 times.
✓ Branch 2 taken 8 times.
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 647 times.
✓ Branch 9 taken 262 times.
✓ Branch 11 taken 244 times.
✓ Branch 12 taken 18 times.
|
927 | if (std::none_of(args.begin(), args.end(), [](const auto& arg) { return (arg == "execute_ruby_script") || (arg == "execute_python_script"); })) { |
| 90 |
5/5✓ Branch 1 taken 289 times.
✓ Branch 2 taken 180 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 285 times.
✓ Branch 9 taken 244 times.
|
713 | auto it = std::find_if(args.begin(), args.end(), [](const auto& arg) { return arg.ends_with(".rb") || arg.ends_with(".py"); }); |
| 91 |
2/2✓ Branch 2 taken 184 times.
✓ Branch 3 taken 60 times.
|
244 | if (it != args.end()) { |
| 92 |
2/2✓ Branch 2 taken 180 times.
✓ Branch 3 taken 4 times.
|
184 | if (it->ends_with(".rb")) { |
| 93 |
2/2✓ Branch 1 taken 180 times.
✓ Branch 5 taken 180 times.
|
360 | args.insert(it, "execute_ruby_script"); |
| 94 | } else { | ||
| 95 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 5 taken 4 times.
|
8 | args.insert(it, "execute_python_script"); |
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | 262 | app.get_formatter()->column_width(35); | |
| 101 | |||
| 102 |
3/3✓ Branch 1 taken 262 times.
✓ Branch 5 taken 262 times.
✓ Branch 8 taken 262 times.
|
786 | auto* const verboseOpt = app.add_flag_function( |
| 103 | "--verbose", | ||
| 104 | 262 | [](auto count) { | |
| 105 |
1/2✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 | if (count == 1) { |
| 106 |
3/3✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
|
1 | openstudio::Logger::instance().standardOutLogger().setLogLevel(LogLevel::Debug); |
| 107 |
7/7✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 16 taken 1 time.
✓ Branch 19 taken 1 time.
|
3 | LOG_FREE(Debug, "openstudio.CLI", "Setting Log Level to Debug (" << LogLevel::Debug << ")"); |
| 108 | ✗ | } else if (count == 2) { | |
| 109 | ✗ | openstudio::Logger::instance().standardOutLogger().setLogLevel(LogLevel::Trace); | |
| 110 | ✗ | LOG_FREE(Debug, "openstudio.CLI", "Setting Log Level to Trace (" << LogLevel::Trace << ")"); | |
| 111 | } | ||
| 112 | 1 | }, | |
| 113 | "Print the full log to STDOUT - sets verbosity to Debug if given once and Trace if given twice."); | ||
| 114 | |||
| 115 | // specify string->value mappings | ||
| 116 | const std::map<std::string, LogLevel> logLevelMap{ | ||
| 117 | ✗ | {"Trace", LogLevel::Trace}, {"Debug", LogLevel::Debug}, {"Info", LogLevel::Info}, | |
| 118 | ✗ | {"Warn", LogLevel::Warn}, {"Error", LogLevel::Error}, {"Fatal", LogLevel::Fatal}, | |
| 119 |
3/5✓ Branch 1 taken 262 times.
✓ Branch 3 taken 1572 times.
✓ Branch 4 taken 262 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
2096 | }; |
| 120 | static constexpr std::array<std::string_view, 6> logLevelStrs = {"Trace", "Debug", "Info", "Warn", "Error", "Fatal"}; | ||
| 121 | |||
| 122 | auto* const logLevelOpt = app | ||
| 123 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
524 | .add_option_function<LogLevel>( |
| 124 | "-l,--loglevel", | ||
| 125 | ✗ | [](const LogLevel& level) { | |
| 126 | 17 | const auto loglLevelStr = logLevelStrs[static_cast<size_t>(level) - static_cast<size_t>(LogLevel::Trace)]; | |
| 127 |
3/3✓ Branch 1 taken 17 times.
✓ Branch 4 taken 17 times.
✓ Branch 7 taken 17 times.
|
17 | openstudio::Logger::instance().standardOutLogger().setLogLevel(level); |
| 128 |
9/9✓ Branch 1 taken 17 times.
✓ Branch 4 taken 17 times.
✓ Branch 7 taken 17 times.
✓ Branch 10 taken 17 times.
✓ Branch 13 taken 17 times.
✓ Branch 16 taken 17 times.
✓ Branch 19 taken 17 times.
✓ Branch 22 taken 17 times.
✓ Branch 25 taken 17 times.
|
51 | LOG_FREE(Debug, "openstudio.CLI", "Setting Log Level to " << loglLevelStr << " (" << level << ")"); |
| 129 | 17 | }, | |
| 130 | "LogLevel settings: One of {Trace, Debug, Info, Warn, Error, Fatal} [Default: Warn] Excludes: --verbose") | ||
| 131 | ->excludes(verboseOpt) | ||
| 132 |
1/1✓ Branch 1 taken 262 times.
|
262 | ->option_text("LEVEL") |
| 133 |
6/6✓ Branch 2 taken 262 times.
✓ Branch 5 taken 262 times.
✓ Branch 9 taken 262 times.
✓ Branch 12 taken 262 times.
✓ Branch 15 taken 262 times.
✓ Branch 19 taken 262 times.
|
1834 | ->transform(CLI::CheckedTransformer(logLevelMap, CLI::ignore_case)); |
| 134 | |||
| 135 |
1/1✓ Branch 1 taken 262 times.
|
262 | verboseOpt->excludes(logLevelOpt); |
| 136 | |||
| 137 | 262 | std::vector<std::string> executeRubyCmds; | |
| 138 | CLI::Option* execRubyOption = app | ||
| 139 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
524 | .add_option("-e,--execute", executeRubyCmds, |
| 140 | "Execute one line of Ruby script (may be used more than once). Returns after executing commands.") | ||
| 141 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
1048 | ->option_text("CMD"); |
| 142 | |||
| 143 | 262 | std::vector<std::string> executePythonCmds; | |
| 144 | CLI::Option* execPythonOption = app | ||
| 145 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
524 | .add_option("-c,--pyexecute", executePythonCmds, |
| 146 | "Execute one line of Python script (may be used more than once). Returns after executing commands.") | ||
| 147 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
1048 | ->option_text("CMD"); |
| 148 | |||
| 149 | // ========================== R U B Y O P T I O N S ========================== | ||
| 150 | 262 | std::vector<openstudio::path> includeDirs; | |
| 151 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
524 | app.add_option("-I,--include", includeDirs, "Add additional directory to add to front of Ruby $LOAD_PATH (may be used more than once)") |
| 152 |
1/1✓ Branch 1 taken 262 times.
|
262 | ->option_text("DIR") |
| 153 |
4/4✓ Branch 1 taken 262 times.
✓ Branch 5 taken 262 times.
✓ Branch 8 taken 262 times.
✓ Branch 11 taken 262 times.
|
1834 | ->check(CLI::ExistingDirectory) |
| 154 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
786 | ->group(rubySpecificOptionsGroupName); |
| 155 | |||
| 156 | 262 | std::vector<openstudio::path> gemPathDirs; | |
| 157 | app | ||
| 158 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
524 | .add_option("--gem_path", gemPathDirs, "Add additional directory to add to front of GEM_PATH environment variable (may be used more than once)") |
| 159 |
1/1✓ Branch 1 taken 262 times.
|
262 | ->option_text("DIR") |
| 160 |
4/4✓ Branch 1 taken 262 times.
✓ Branch 5 taken 262 times.
✓ Branch 8 taken 262 times.
✓ Branch 11 taken 262 times.
|
1834 | ->check(CLI::ExistingDirectory) |
| 161 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
786 | ->group(rubySpecificOptionsGroupName); |
| 162 | |||
| 163 | 262 | openstudio::path gemHomeDir; | |
| 164 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
524 | app.add_option("--gem_home", gemHomeDir, "Set GEM_HOME environment variable") |
| 165 |
1/1✓ Branch 1 taken 262 times.
|
262 | ->option_text("DIR") |
| 166 |
4/4✓ Branch 1 taken 262 times.
✓ Branch 5 taken 262 times.
✓ Branch 8 taken 262 times.
✓ Branch 11 taken 262 times.
|
1834 | ->check(CLI::ExistingDirectory) |
| 167 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
786 | ->group(rubySpecificOptionsGroupName); |
| 168 | |||
| 169 | 262 | openstudio::path bundleGemFilePath; | |
| 170 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
524 | app.add_option("--bundle", bundleGemFilePath, "Use bundler for GEMFILE") |
| 171 |
1/1✓ Branch 1 taken 262 times.
|
262 | ->option_text("GEMFILE") |
| 172 |
4/4✓ Branch 1 taken 262 times.
✓ Branch 5 taken 262 times.
✓ Branch 8 taken 262 times.
✓ Branch 11 taken 262 times.
|
1834 | ->check(CLI::ExistingFile) |
| 173 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
786 | ->group(rubySpecificOptionsGroupName); |
| 174 | |||
| 175 | 262 | openstudio::path bundleGemDirPath; | |
| 176 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
524 | app.add_option("--bundle_path", bundleGemDirPath, "Use bundler installed gems in BUNDLE_PATH") |
| 177 |
1/1✓ Branch 1 taken 262 times.
|
262 | ->option_text("BUNDLE_PATH") |
| 178 |
4/4✓ Branch 1 taken 262 times.
✓ Branch 5 taken 262 times.
✓ Branch 8 taken 262 times.
✓ Branch 11 taken 262 times.
|
1834 | ->check(CLI::ExistingDirectory) |
| 179 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
786 | ->group(rubySpecificOptionsGroupName); |
| 180 | |||
| 181 | // std::vector<std::string> | ||
| 182 | 262 | std::string bundleWithoutGroups; | |
| 183 | app | ||
| 184 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
524 | .add_option( |
| 185 | "--bundle_without", bundleWithoutGroups, | ||
| 186 | "Space separated list of groups for bundler to exclude in WITHOUT_GROUPS. Surround multiple groups with quotes like \"test development\"") | ||
| 187 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
1048 | ->option_text("WITHOUT_GROUPS") |
| 188 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
786 | ->group(rubySpecificOptionsGroupName); // ->delimiter(' '); |
| 189 | |||
| 190 | // This is a callback that's stored on the ScriptEngineInstance, triggered only the first time. Just need to make sure it doesn't go out of scope | ||
| 191 | // nor its captures | ||
| 192 | ✗ | std::function<void()> runSetupEmbeddedGems = [&rubyEngine, &includeDirs, &gemPathDirs, &gemHomeDir, &bundleGemFilePath, &bundleGemDirPath, | |
| 193 | &bundleWithoutGroups]() { | ||
| 194 | 223 | rubyEngine->setupEmbeddedGems(includeDirs, gemPathDirs, gemHomeDir, bundleGemFilePath, bundleGemDirPath, bundleWithoutGroups); | |
| 195 |
2/2✓ Branch 3 taken 222 times.
✓ Branch 6 taken 222 times.
|
444 | rubyEngine->registerType<openstudio::measure::OSMeasure*>("openstudio::measure::OSMeasure *"); |
| 196 |
2/2✓ Branch 3 taken 222 times.
✓ Branch 6 taken 222 times.
|
444 | rubyEngine->registerType<openstudio::measure::ModelMeasure*>("openstudio::measure::ModelMeasure *"); |
| 197 |
2/2✓ Branch 3 taken 222 times.
✓ Branch 6 taken 222 times.
|
444 | rubyEngine->registerType<openstudio::measure::EnergyPlusMeasure*>("openstudio::measure::EnergyPlusMeasure *"); |
| 198 |
2/2✓ Branch 3 taken 222 times.
✓ Branch 6 taken 222 times.
|
444 | rubyEngine->registerType<openstudio::measure::ReportingMeasure*>("openstudio::measure::ReportingMeasure *"); |
| 199 |
2/2✓ Branch 3 taken 222 times.
✓ Branch 6 taken 222 times.
|
444 | rubyEngine->registerType<openstudio::measure::MeasureInfoBinding*>("openstudio::measure::MeasureInfoBinding *"); |
| 200 |
1/1✓ Branch 4 taken 222 times.
|
222 | rubyEngine->exec("OpenStudio::init_rest_of_openstudio()"); |
| 201 |
1/1✓ Branch 1 taken 262 times.
|
484 | }; |
| 202 |
1/1✓ Branch 1 taken 262 times.
|
262 | rubyEngine.registerInitializationFunction(runSetupEmbeddedGems); |
| 203 | |||
| 204 | // ========================== P Y T H O N O P T I O N S ========================== | ||
| 205 | |||
| 206 | 262 | std::vector<openstudio::path> pythonPathDirs; | |
| 207 | app | ||
| 208 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
524 | .add_option("--python_path", pythonPathDirs, |
| 209 | "Add additional directory to add to front of PYTHONPATH environment variable (may be used more than once)") | ||
| 210 |
1/1✓ Branch 1 taken 262 times.
|
262 | ->option_text("DIR") |
| 211 |
4/4✓ Branch 1 taken 262 times.
✓ Branch 5 taken 262 times.
✓ Branch 8 taken 262 times.
✓ Branch 11 taken 262 times.
|
1834 | ->check(CLI::ExistingDirectory) |
| 212 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
786 | ->group(pythonSpecificOptionsGroupName); |
| 213 | |||
| 214 | 262 | openstudio::path pythonHomeDir; | |
| 215 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
524 | app.add_option("--python_home", pythonHomeDir, "Set PYTHONHOME environment variable") |
| 216 |
1/1✓ Branch 1 taken 262 times.
|
262 | ->option_text("DIR") |
| 217 |
4/4✓ Branch 1 taken 262 times.
✓ Branch 5 taken 262 times.
✓ Branch 8 taken 262 times.
✓ Branch 11 taken 262 times.
|
1834 | ->check(CLI::ExistingDirectory) |
| 218 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
786 | ->group(pythonSpecificOptionsGroupName); |
| 219 | |||
| 220 | // This is a callback that's stored on the ScriptEngineInstance, triggered only the first time | ||
| 221 | 553 | std::function<void()> runSetupPythonPath = [&pythonEngine, &pythonPathDirs]() { | |
| 222 | // pythonHomeDir is retrieved from (argc, argv) actually, as Py_SetPythonHome has to be called before Py_Initialize | ||
| 223 | 29 | pythonEngine->setupPythonPath(pythonPathDirs); | |
| 224 |
2/2✓ Branch 3 taken 29 times.
✓ Branch 6 taken 29 times.
|
58 | pythonEngine->registerType<openstudio::measure::OSMeasure*>("openstudio::measure::OSMeasure *"); |
| 225 |
2/2✓ Branch 3 taken 29 times.
✓ Branch 6 taken 29 times.
|
58 | pythonEngine->registerType<openstudio::measure::ModelMeasure*>("openstudio::measure::ModelMeasure *"); |
| 226 |
2/2✓ Branch 3 taken 29 times.
✓ Branch 6 taken 29 times.
|
58 | pythonEngine->registerType<openstudio::measure::EnergyPlusMeasure*>("openstudio::measure::EnergyPlusMeasure *"); |
| 227 |
2/2✓ Branch 3 taken 29 times.
✓ Branch 6 taken 29 times.
|
58 | pythonEngine->registerType<openstudio::measure::ReportingMeasure*>("openstudio::measure::ReportingMeasure *"); |
| 228 | 291 | }; | |
| 229 |
1/1✓ Branch 1 taken 262 times.
|
262 | pythonEngine.registerInitializationFunction(runSetupPythonPath); |
| 230 | |||
| 231 | // { | ||
| 232 |
3/3✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
|
786 | auto* execute_ruby_scriptCommand = app.add_subcommand("execute_ruby_script", "Executes a ruby file"); |
| 233 | 262 | openstudio::filesystem::path rubyScriptPath; | |
| 234 | 262 | std::vector<std::string> ruby_fwd_args; | |
| 235 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
524 | execute_ruby_scriptCommand->add_option("path", rubyScriptPath, "Path to Ruby file") |
| 236 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
1310 | ->option_text("RUBY_SCRIPT") |
| 237 | ->required(true) | ||
| 238 |
3/3✓ Branch 2 taken 262 times.
✓ Branch 5 taken 262 times.
✓ Branch 8 taken 262 times.
|
786 | ->check(CLI::ExistingFile); |
| 239 |
4/4✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
✓ Branch 10 taken 262 times.
|
1572 | execute_ruby_scriptCommand->add_option("args", ruby_fwd_args, "Extra Arguments forwarded to the Ruby script")->option_text("ARG ..."); |
| 240 | 262 | execute_ruby_scriptCommand->positionals_at_end(true); | |
| 241 |
1/1✓ Branch 1 taken 262 times.
|
524 | execute_ruby_scriptCommand->footer("Any additional arguments passed after the Ruby file are forwarded"); |
| 242 | 262 | execute_ruby_scriptCommand->callback( | |
| 243 |
3/3✓ Branch 1 taken 262 times.
✓ Branch 4 taken 188 times.
✓ Branch 7 taken 187 times.
|
712 | [&rubyScriptPath, &rubyEngine, &ruby_fwd_args] { openstudio::cli::executeRubyScriptCommand(rubyScriptPath, rubyEngine, ruby_fwd_args); }); |
| 244 | // } | ||
| 245 | |||
| 246 | // { | ||
| 247 |
3/3✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
|
786 | auto* execute_python_scriptCommand = app.add_subcommand("execute_python_script", "Executes a python file"); |
| 248 | 262 | openstudio::filesystem::path pythonScriptPath; | |
| 249 | 262 | std::vector<std::string> python_fwd_args; | |
| 250 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
524 | execute_python_scriptCommand->add_option("path", pythonScriptPath, "Path to Python file") |
| 251 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
1310 | ->option_text("PYTHON_SCRIPT") |
| 252 | ->required(true) | ||
| 253 |
3/3✓ Branch 2 taken 262 times.
✓ Branch 5 taken 262 times.
✓ Branch 8 taken 262 times.
|
786 | ->check(CLI::ExistingFile); |
| 254 |
4/4✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
✓ Branch 10 taken 262 times.
|
1572 | execute_python_scriptCommand->add_option("args", python_fwd_args, "Extra Arguments forwarded to the Python script")->option_text("ARG ..."); |
| 255 | 262 | execute_python_scriptCommand->positionals_at_end(true); | |
| 256 |
1/1✓ Branch 1 taken 262 times.
|
524 | execute_python_scriptCommand->footer("You can pass extra arguments after the Python file, they will be forwarded."); |
| 257 | |||
| 258 |
1/1✓ Branch 1 taken 262 times.
|
262 | execute_python_scriptCommand->callback([&pythonScriptPath, &pythonEngine, &python_fwd_args] { |
| 259 |
2/2✓ Branch 1 taken 14 times.
✓ Branch 4 taken 13 times.
|
14 | openstudio::cli::executePythonScriptCommand(pythonScriptPath, pythonEngine, python_fwd_args); |
| 260 | 13 | }); | |
| 261 | // } | ||
| 262 | |||
| 263 | // { | ||
| 264 |
3/3✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
|
786 | auto* ruby_repl_command = app.add_subcommand("interactive_ruby", "Executes a ruby REPL"); |
| 265 | 262 | ruby_repl_command->callback([&rubyEngine] { openstudio::cli::executeRubyRepl(rubyEngine); }); | |
| 266 | // } | ||
| 267 | |||
| 268 | // { | ||
| 269 |
3/3✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
|
786 | auto* python_repl_command = app.add_subcommand("interactive_python", "Executes a python REPL"); |
| 270 | 262 | python_repl_command->callback([&pythonEngine] { openstudio::cli::executePythonRepl(pythonEngine); }); | |
| 271 | // } | ||
| 272 | |||
| 273 | [[maybe_unused]] auto* gem_listCommand = | ||
| 274 |
3/3✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
|
1048 | app.add_subcommand("gem_list", "Lists the set of gems available to openstudio")->callback([&rubyEngine]() { |
| 275 | ✗ | openstudio::cli::executeGemListCommand(rubyEngine); | |
| 276 | ✗ | }); | |
| 277 | |||
| 278 | [[maybe_unused]] auto* pip_listCommand = | ||
| 279 |
3/3✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
|
786 | app.add_subcommand("pip_list", "Lists the set of python modules available to openstudio")->callback([&pythonEngine]() { |
| 280 | ✗ | openstudio::cli::executePipListCommand(pythonEngine); | |
| 281 | ✗ | }); | |
| 282 | |||
| 283 | // Not hidding any commands right now | ||
| 284 | // [[maybe_unused]] auto* list_commandsCommand = app.add_subcommand("list_commands", "Lists the entire set of available commands"); | ||
| 285 | |||
| 286 | // run command | ||
| 287 |
1/1✓ Branch 1 taken 262 times.
|
262 | openstudio::cli::setupRunOptions(&app, rubyEngine, pythonEngine); |
| 288 | |||
| 289 | // update (model) command | ||
| 290 | // openstudio::cli::setupUpdateCommand(&app); | ||
| 291 | { | ||
| 292 | 262 | bool keep = false; | |
| 293 |
3/3✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
|
786 | auto* updateCommand = app.add_subcommand("update", "Updates OpenStudio Models to the current version"); |
| 294 |
3/3✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
|
1048 | updateCommand->add_flag("-k,--keep", keep, "Keep original files"); |
| 295 | |||
| 296 | 262 | openstudio::filesystem::path updateOsmPath; | |
| 297 |
3/3✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
|
1048 | updateCommand->add_option("path", updateOsmPath, "Path to OSM or directory containing osms")->required(true); |
| 298 | |||
| 299 | 262 | updateCommand->callback([&keep, &updateOsmPath] { | |
| 300 | ✗ | if (!openstudio::cli::runModelUpdateCommand(updateOsmPath, keep)) { | |
| 301 | ✗ | throw std::runtime_error("Failed to update some models"); | |
| 302 | } | ||
| 303 | ✗ | }); | |
| 304 | 262 | } | |
| 305 | |||
| 306 |
1/1✓ Branch 1 taken 262 times.
|
262 | openstudio::cli::MeasureUpdateOptions::setupMeasureUpdateOptions(&app, rubyEngine, pythonEngine); |
| 307 | |||
| 308 | // ========================== V E R S I O N ========================== | ||
| 309 |
4/4✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
✓ Branch 10 taken 262 times.
|
1048 | app.set_version_flag("-v,--version", openStudioLongVersion()); |
| 310 | |||
| 311 | [[maybe_unused]] auto* openstudio_versionCommand = | ||
| 312 |
5/5✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
✓ Branch 10 taken 262 times.
✓ Branch 13 taken 262 times.
|
1572 | app.add_subcommand("openstudio_version", "Returns the OpenStudio version used by the CLI")->group(versionGroupname)->callback([]() { |
| 313 |
1/1✓ Branch 1 taken 1 time.
|
2 | fmt::print("{}\n", openStudioLongVersion()); |
| 314 | 1 | }); | |
| 315 | |||
| 316 | [[maybe_unused]] auto* energyplus_versionCommand = | ||
| 317 |
5/5✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
✓ Branch 10 taken 262 times.
✓ Branch 13 taken 262 times.
|
1572 | app.add_subcommand("energyplus_version", "Returns the EnergyPlus version used by the CLI")->group(versionGroupname)->callback([]() { |
| 318 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
2 | fmt::print("{}+{}\n", energyPlusVersion(), energyPlusBuildSHA()); |
| 319 | 1 | }); | |
| 320 | [[maybe_unused]] auto* ruby_versionCommand = | ||
| 321 |
5/5✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
✓ Branch 10 taken 262 times.
✓ Branch 13 taken 262 times.
|
1572 | app.add_subcommand("ruby_version", "Returns the Ruby version used by the CLI")->group(versionGroupname)->callback([&rubyEngine]() { |
| 322 |
1/1✓ Branch 4 taken 2 times.
|
2 | rubyEngine->exec("puts RUBY_VERSION"); |
| 323 | 2 | }); | |
| 324 | [[maybe_unused]] auto* python_versionCommand = | ||
| 325 |
5/5✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
✓ Branch 10 taken 262 times.
✓ Branch 13 taken 262 times.
|
1572 | app.add_subcommand("python_version", "Returns the Python version used by the CLI")->group(versionGroupname)->callback([&pythonEngine]() { |
| 326 |
1/1✓ Branch 4 taken 1 time.
|
1 | pythonEngine->exec("import sys; print(sys.version)"); |
| 327 | 1 | }); | |
| 328 | |||
| 329 | // ==================================================================== | ||
| 330 | |||
| 331 | // ========================== C L A S S I C ========================== | ||
| 332 | // This exists to document access to the classic implementation | ||
| 333 |
3/3✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
|
786 | app.add_subcommand("classic", "For backwards compatibility, invoke an older version of the OpenStudio CLI that does not support Python"); |
| 334 | // ==================================================================== | ||
| 335 | |||
| 336 | // CLI11_PARSE(app, argc, argv); | ||
| 337 | // CLI11_PARSE(app, args); | ||
| 338 | try { | ||
| 339 | // app.parse(argc, argv); | ||
| 340 | // CLI11 when passing argc, argv creates a vector<string> but **in reverse** order: | ||
| 341 | // https://github.com/CLIUtils/CLI11/blob/291c58789c031208f08f4f261a858b5b7083e8e2/include/CLI/impl/App_inl.hpp#L476-L488 | ||
| 342 |
1/1✓ Branch 3 taken 262 times.
|
262 | std::reverse(args.begin(), args.end()); |
| 343 |
1/1✓ Branch 1 taken 248 times.
|
262 | app.parse(args); |
| 344 |
1/3✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
1 | } catch (const CLI::ParseError& e) { |
| 345 |
1/1✓ Branch 1 taken 1 time.
|
1 | return app.exit(e); |
| 346 | 1 | } catch (const std::exception& e) { | |
| 347 | ✗ | std::cout << e.what() << std::endl; | |
| 348 | ✗ | return 1; | |
| 349 | ✗ | } catch (...) { | |
| 350 | ✗ | std::cout << "OpenStudio encountered an unknown error" << std::endl; | |
| 351 | ✗ | return 1; | |
| 352 | ✗ | } | |
| 353 | |||
| 354 |
2/2✓ Branch 1 taken 19 times.
✓ Branch 2 taken 229 times.
|
248 | if (*execRubyOption) { |
| 355 | // fmt::print("--execute Flag received {} times.\n", execRubyOption->count()); | ||
| 356 |
2/2✓ Branch 5 taken 19 times.
✓ Branch 6 taken 19 times.
|
38 | for (auto& cmd : executeRubyCmds) { |
| 357 | // fmt::print("{}\n", cmd); | ||
| 358 |
2/2✓ Branch 1 taken 19 times.
✓ Branch 6 taken 19 times.
|
19 | rubyEngine->exec(cmd); |
| 359 | } | ||
| 360 | } | ||
| 361 |
2/2✓ Branch 1 taken 7 times.
✓ Branch 2 taken 241 times.
|
248 | if (*execPythonOption) { |
| 362 | // fmt::print("--pyexecute Flag received {} times.\n", execPythonOption->count()); | ||
| 363 |
2/2✓ Branch 5 taken 7 times.
✓ Branch 6 taken 7 times.
|
14 | for (auto& cmd : executePythonCmds) { |
| 364 | // fmt::print("{}\n", cmd); | ||
| 365 |
2/2✓ Branch 1 taken 7 times.
✓ Branch 6 taken 7 times.
|
7 | pythonEngine->exec(cmd); |
| 366 | } | ||
| 367 | } | ||
| 368 | // fmt::print("includeDirs={}\n", fmt::join(includeDirs, ",")); | ||
| 369 | // fmt::print("gemPathDirs={}\n", fmt::join(gemPathDirs, ",")); | ||
| 370 | // fmt::print("gemHomeDir={}\n", gemHomeDir); | ||
| 371 | 266 | } | |
| 372 | |||
| 373 | // Important to destroy RubyEngine and finalize Ruby at the right time | ||
| 374 | // After the main function returns it is (apparently) too late | ||
| 375 | 262 | rubyEngine.reset(); | |
| 376 | 257 | pythonEngine.reset(); | |
| 377 | |||
| 378 | 257 | return result; | |
| 379 | 520 | } | |
| 380 |