Skip to content

Commit

Permalink
feat: support --help and -h flags
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Oct 13, 2023
1 parent df309d1 commit a6458ab
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/pact_broker/client/cli/custom_thor.rb
Expand Up @@ -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
Expand All @@ -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 <command>`, 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']
Expand Down
15 changes: 15 additions & 0 deletions spec/lib/pact_broker/client/cli/custom_thor_spec.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -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}
Expand Down

0 comments on commit a6458ab

Please sign in to comment.