Skip to content

Commit d6e3037

Browse files
committed
Merge pull request #348 from maxmeyer/feature/current_command
make the current command chain accessible in command
2 parents ea7bb84 + 4929c95 commit d6e3037

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

lib/thor/invocation.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ def initialize(args = [], options = {}, config = {}, &block) #:nodoc:
2525
super
2626
end
2727

28+
# Make the current command chain accessible with in a Thor-(sub)command
29+
def current_command_chain
30+
@_invocations.values.flatten.map(&:to_sym)
31+
end
32+
2833
# Receives a name and invokes it. The name can be a string (either "command" or
2934
# "namespace:command"), a Thor::Command, a Class or a Thor instance. If the
3035
# command cannot be guessed by name, it can also be supplied as second argument.

spec/fixtures/invoke.thor

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,16 @@ class H < Thor::Group
116116
class_option :defined, :type => :boolean, :default => true
117117
invoke_from_option :defined
118118
end
119+
120+
class I < Thor
121+
desc "two", "Two"
122+
def two
123+
current_command_chain
124+
end
125+
end
126+
127+
class J < Thor
128+
desc "i", "I"
129+
subcommand :one, I
130+
end
131+

spec/invocation_spec.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,19 @@
5858
expect(A.new([], :defaulted_value => 'not default').invoke('b:four')).to eq('not default')
5959
end
6060

61-
it 'dump configuration values to be used in the invoked class' do
61+
it "returns the command chain" do
62+
expect(I.new.invoke("two")).to eq([ :two ])
63+
64+
if RUBY_VERSION < '1.9.3'
65+
result = J.start(["one", "two" ])
66+
expect(result).to include(:one)
67+
expect(result).to include(:two)
68+
else
69+
expect(J.start(["one", "two" ])).to eq([ :one, :two ])
70+
end
71+
end
72+
73+
it "dump configuration values to be used in the invoked class" do
6274
base = A.new
6375
expect(base.invoke('b:three').shell).to eq(base.shell)
6476
end

0 commit comments

Comments
 (0)