From a6458abff071493c09f4ab308e507bf31fe1815b Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Fri, 13 Oct 2023 16:05:53 +1100 Subject: [PATCH] feat: support --help and -h flags --- lib/pact_broker/client/cli/custom_thor.rb | 12 +++++++++++- .../pact_broker/client/cli/custom_thor_spec.rb | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/pact_broker/client/cli/custom_thor.rb b/lib/pact_broker/client/cli/custom_thor.rb index 76bdfbc..5af622d 100644 --- a/lib/pact_broker/client/cli/custom_thor.rb +++ b/lib/pact_broker/client/cli/custom_thor.rb @@ -24,7 +24,7 @@ def self.start given_args = ARGV, config = {} end def self.massage_args argv - add_broker_config_from_environment_variables(turn_muliple_tag_options_into_array(argv)) + add_broker_config_from_environment_variables(turn_muliple_tag_options_into_array(handle_help(argv))) end def self.add_broker_config_from_environment_variables argv @@ -42,6 +42,16 @@ def self.add_option_from_environment_variable argv, long_name, short_name, envir end end + # Thor expects help to be invoked by typing `help `, which is very odd. + # Add support for `command --help|-h` by massaging the arguments into the format that Thor expects. + def self.handle_help(argv) + if argv.last == "--help" || argv.last == "-h" + argv[0..-3] + ["help", argv[-2]].compact + else + argv + end + end + # other task names, help, and the help shortcuts def self.known_first_arguments @known_first_arguments ||= tasks.keys + ::Thor::HELP_MAPPINGS + ['help'] diff --git a/spec/lib/pact_broker/client/cli/custom_thor_spec.rb b/spec/lib/pact_broker/client/cli/custom_thor_spec.rb index ca128fb..8ae970d 100644 --- a/spec/lib/pact_broker/client/cli/custom_thor_spec.rb +++ b/spec/lib/pact_broker/client/cli/custom_thor_spec.rb @@ -7,6 +7,10 @@ def self.call options; end end class TestThor < CustomThor + def self.exit_on_failure? + false + end + desc 'ARGUMENT', 'This is the description' def test_default(argument) Delegate.call(argument: argument) @@ -96,6 +100,17 @@ def test_without_parameters TestThor.start(%w{test_pact_broker_client_options}) end + describe ".handle_help" do + context "when the last argument is --help or -h" do + it "turns it into the form that Thor expects, which is a really odd one" do + expect(TestThor.handle_help(["foo", "--help"])).to eq ["help", "foo"] + expect(TestThor.handle_help(["foo", "-h"])).to eq ["help", "foo"] + expect(TestThor.handle_help(["-h"])).to eq ["help"] + expect(TestThor.handle_help(["--help"])).to eq ["help"] + end + end + end + describe ".turn_muliple_tag_options_into_array" do it "turns '--tag foo --tag bar' into '--tag foo bar'" do input = %w{--ignore this --tag foo --tag bar --wiffle --that}