Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #5062 - Properly forward the --help flag to ruby/python scripts #5065

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,17 @@ if(BUILD_TESTING)

# Test via optparse and check file exists
# Historical CLI versions
add_test(NAME OpenStudioCLI.Classic.execute_ruby_script.forward_flags.optparse.path_forwardslash
add_test(NAME OpenStudioCLI.Classic.execute_ruby_script.forward_flags.optparse.path_forwardslash
COMMAND $<TARGET_FILE:openstudio> classic execute_ruby_script execute_ruby_script_optparse_path.rb -x "./test_folder/hello.xml"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/test/"
)
add_test(NAME OpenStudioCLI.Classic.execute_ruby_script.forward_flags.forward_help
COMMAND $<TARGET_FILE:openstudio> classic execute_ruby_script ${CMAKE_CURRENT_SOURCE_DIR}/test/execute_ruby_script_optparse_path.rb --help
)
set_tests_properties(OpenStudioCLI.Classic.execute_ruby_script.forward_flags.forward_help PROPERTIES
PASS_REGULAR_EXPRESSION "The Ruby help description."
)

if (WIN32)
# Posix paths don't understand a backward slash anyways
add_test(NAME OpenStudioCLI.Classic.execute_ruby_script.forward_flags.optparse.path_backwardslash
Expand All @@ -313,6 +320,21 @@ if(BUILD_TESTING)
COMMAND $<TARGET_FILE:openstudio> execute_ruby_script execute_ruby_script_optparse_path.rb -x "./test_folder/hello.xml"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/test/"
)

add_test(NAME OpenStudioCLI.execute_ruby_script.forward_flags.forward_help
COMMAND $<TARGET_FILE:openstudio> execute_ruby_script ${CMAKE_CURRENT_SOURCE_DIR}/test/execute_ruby_script_optparse_path.rb --help
)
set_tests_properties(OpenStudioCLI.execute_ruby_script.forward_flags.forward_help PROPERTIES
PASS_REGULAR_EXPRESSION "The Ruby help description."
)

add_test(NAME OpenStudioCLI.execute_python_script.forward_flags.forward_help
COMMAND $<TARGET_FILE:openstudio> execute_python_script ${CMAKE_CURRENT_SOURCE_DIR}/test/execute_python_script_argparse_path.py --help
)
set_tests_properties(OpenStudioCLI.execute_python_script.forward_flags.forward_help PROPERTIES
PASS_REGULAR_EXPRESSION "The Python help description."
)

if (WIN32)
add_test(NAME OpenStudioCLI.execute_ruby_script.forward_flags.optparse.path_backwardslash
COMMAND $<TARGET_FILE:openstudio> execute_ruby_script execute_ruby_script_optparse_path.rb -x ".\\test_folder\\hello.xml"
Expand Down
2 changes: 2 additions & 0 deletions src/cli/UpdateCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ namespace cli {
cmd += fmt::format(R"(
begin
require '{}'
rescue SystemExit
# puts "help was called"
rescue Exception => e
puts
puts "Error: #{{e.message}}"
Expand Down
40 changes: 21 additions & 19 deletions src/cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,30 +244,32 @@ int main(int argc, char* argv[]) {
// {
auto* execute_ruby_scriptCommand = app.add_subcommand("execute_ruby_script", "Executes a ruby file");
openstudio::filesystem::path rubyScriptPath;
execute_ruby_scriptCommand->add_option("path", rubyScriptPath, "Path to ruby file")->required(true)->check(CLI::ExistingFile);
// We can't do this because that means we can't pass extra **flags**
// std::vector<std::string> executeRubyScriptCommandArgs;
// execute_ruby_scriptCommand->add_option("arguments", executeRubyScriptCommandArgs, "Arguments to pass to the ruby file")
// ->required(false)
// ->option_text("args");
execute_ruby_scriptCommand->allow_extras(true);
execute_ruby_scriptCommand->footer("You can pass extra arguments after the ruby file, they will be forwarded.");

execute_ruby_scriptCommand->callback([&rubyScriptPath, &rubyEngine, &execute_ruby_scriptCommand] {
openstudio::cli::executeRubyScriptCommand(rubyScriptPath, rubyEngine, execute_ruby_scriptCommand->remaining());
});
std::vector<std::string> ruby_fwd_args;
execute_ruby_scriptCommand->add_option("path", rubyScriptPath, "Path to Ruby file")
->option_text("RUBY_SCRIPT")
->required(true)
->check(CLI::ExistingFile);
execute_ruby_scriptCommand->add_option("args", ruby_fwd_args, "Extra Arguments forwarded to the Ruby script")->option_text("ARG ...");
execute_ruby_scriptCommand->positionals_at_end(true);
execute_ruby_scriptCommand->footer("Any additional arguments passed after the Ruby file are forwarded");
execute_ruby_scriptCommand->callback(
[&rubyScriptPath, &rubyEngine, &ruby_fwd_args] { openstudio::cli::executeRubyScriptCommand(rubyScriptPath, rubyEngine, ruby_fwd_args); });
// }

// {
auto* execute_python_scriptCommand = app.add_subcommand("execute_python_script", "Executes a python file");
openstudio::filesystem::path pythonScriptPath;
execute_python_scriptCommand->add_option("path", pythonScriptPath, "Path to python file")->required(true)->check(CLI::ExistingFile);

execute_python_scriptCommand->allow_extras(true);
execute_python_scriptCommand->footer("You can pass extra arguments after the python file, they will be forwarded.");

execute_python_scriptCommand->callback([&pythonScriptPath, &pythonEngine, &execute_python_scriptCommand] {
openstudio::cli::executePythonScriptCommand(pythonScriptPath, pythonEngine, execute_python_scriptCommand->remaining());
std::vector<std::string> python_fwd_args;
execute_python_scriptCommand->add_option("path", pythonScriptPath, "Path to Python file")
->option_text("PYTHON_SCRIPT")
->required(true)
->check(CLI::ExistingFile);
execute_python_scriptCommand->add_option("args", python_fwd_args, "Extra Arguments forwarded to the Python script")->option_text("ARG ...");
execute_python_scriptCommand->positionals_at_end(true);
execute_python_scriptCommand->footer("You can pass extra arguments after the Python file, they will be forwarded.");

execute_python_scriptCommand->callback([&pythonScriptPath, &pythonEngine, &python_fwd_args] {
openstudio::cli::executePythonScriptCommand(pythonScriptPath, pythonEngine, python_fwd_args);
});
// }

Expand Down
21 changes: 21 additions & 0 deletions src/cli/test/execute_python_script_argparse_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import argparse
from pathlib import Path


def validate_file(arg):
if (filepath := Path(arg)).is_file():
return filepath
else:
raise FileNotFoundError(arg)

def validate_xml_file(arg):
filepath = validate_file(arg)
if (filepath.suffix != '.xml'):
raise FileNotFoundError(f"{arg} is not a .xml file")
return filepath

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="The Python help description.")
parser.add_argument("-x,--xml", metavar='FILE', required=True, type=validate_xml_file, help="HPXML file")
args = parser.parse_args()
print(args)
1 change: 1 addition & 0 deletions src/cli/test/execute_ruby_script_optparse_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename(__FILE__)} -x building.xml"
opts.banner += "\n\nThe Ruby help description.\n\n"

opts.on('-x', '--xml <FILE>', 'HPXML file') do |t|
options[:hpxml] = t
Expand Down