Skip to content

Commit

Permalink
Fix problem with help not including parent command names for subcommands
Browse files Browse the repository at this point in the history
See problem identified in github Issue 128. This patch has been submitted
as Issue 137
  • Loading branch information
rleber committed Jul 4, 2011
1 parent c7fe53f commit 0726f56
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/thor.rb
Expand Up @@ -199,9 +199,14 @@ def printable_tasks(all = true, subcommand = false)
def subcommands
@subcommands ||= from_superclass(:subcommands, [])
end

def parent_commands
@parent_commands ||= from_superclass(:parent_commands, [])
end

def subcommand(subcommand, subcommand_class)
self.subcommands << subcommand.to_s
subcommand_class.parent_commands << subcommand.to_s
subcommand_class.subcommand_help subcommand
define_method(subcommand) { |*args| invoke subcommand_class, args }
end
Expand Down
4 changes: 4 additions & 0 deletions lib/thor/group.rb
Expand Up @@ -203,6 +203,10 @@ def printable_tasks(*)
item << (desc ? "# #{desc.gsub(/\s+/m,' ')}" : "")
[item]
end

def parent_commands
[]
end

def handle_argument_error(task, error) #:nodoc:
raise error, "#{task.name.inspect} was called incorrectly. Are you sure it has arity equals to 0?"
Expand Down
3 changes: 3 additions & 0 deletions lib/thor/task.rb
Expand Up @@ -39,6 +39,9 @@ def formatted_usage(klass, namespace = true, subcommand = false)

formatted ||= ""

# Add parent commands (for subcommands)
klass.parent_commands.each {|parent_command| formatted << parent_command + ' ' }

# Add usage with required arguments
formatted << if klass && !klass.arguments.empty?
usage.to_s.gsub(/^#{name}/) do |match|
Expand Down
3 changes: 3 additions & 0 deletions spec/task_spec.rb
Expand Up @@ -13,18 +13,21 @@ def task(options={})
it "includes namespace within usage" do
Object.stub!(:namespace).and_return("foo")
Object.stub!(:arguments).and_return([])
Object.stub!(:parent_commands).and_return([])
task(:bar => :required).formatted_usage(Object).should == "foo:can_has --bar=BAR"
end

it "removes default from namespace" do
Object.stub!(:namespace).and_return("default:foo")
Object.stub!(:arguments).and_return([])
Object.stub!(:parent_commands).and_return([])
task(:bar => :required).formatted_usage(Object).should == ":foo:can_has --bar=BAR"
end

it "injects arguments into usage" do
Object.stub!(:namespace).and_return("foo")
Object.stub!(:arguments).and_return([ Thor::Argument.new(:bar, nil, true, :string) ])
Object.stub!(:parent_commands).and_return([])
task(:foo => :required).formatted_usage(Object).should == "foo:can_has BAR --foo=FOO"
end
end
Expand Down

0 comments on commit 0726f56

Please sign in to comment.