Skip to content

Commit 58c22cb

Browse files
committed
Merge pull request #359 from lucas-heinlen/command_options_on_invoke
Fixes #285 - Process a command's options when it is executed with invoke
2 parents 2b347d4 + 425fb85 commit 58c22cb

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

lib/thor/base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ def initialize(args=[], options={}, config={})
4646
# new, passing in the two halves of the arguments Array as the
4747
# first two parameters.
4848

49+
command_options = config.delete(:command_options) # hook for start
50+
parse_options = parse_options.merge(command_options) if command_options
4951
if options.is_a?(Array)
50-
command_options = config.delete(:command_options) # hook for start
51-
parse_options = parse_options.merge(command_options) if command_options
5252
array_options, hash_options = options, {}
5353
else
5454
# Handle the case where the class was explicitly instantiated

spec/fixtures/invoke.thor

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ class B < Thor
5353
def three
5454
self
5555
end
56+
57+
desc "four", "invoke four"
58+
option :defaulted_value, :type => :string, :default => 'default'
59+
def four
60+
options.defaulted_value
61+
end
5662
end
5763

5864
class C < Thor::Group

spec/invocation_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@
5050
expect(A.new([], :foo => :bar).invoke("b:two")).to eq({ "foo" => :bar })
5151
end
5252

53+
it "uses default options from invoked class if no matching arguments are given" do
54+
expect(A.new([]).invoke("b:four")).to eq("default")
55+
end
56+
57+
it "overrides default options if options are passed to the invoker" do
58+
expect(A.new([], { :defaulted_value => "not default"}).invoke("b:four")).to eq("not default")
59+
end
60+
5361
it "dump configuration values to be used in the invoked class" do
5462
base = A.new
5563
expect(base.invoke("b:three").shell).to eq(base.shell)

0 commit comments

Comments
 (0)