Class: Thor::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/petra/patches/thor/command.rb

Instance Method Summary collapse

Instance Method Details

#formatted_usage(klass, namespace = true, subcommand = false) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/petra/patches/thor/command.rb', line 3

def formatted_usage(klass, namespace = true, subcommand = false)
  if namespace
    namespace = klass.namespace
    formatted = "#{namespace.gsub(/^(default)/,'')}:"
  end
  formatted = "#{klass.namespace.split(':').last} " if subcommand

  formatted ||= ""

  # PATCH :: properly display subcommands
  #
  # Tasks/Commands registered in subcommands incorrectly
  # display at the top level with the default Thor help
  # task. This attempts to fix the issue.
  #
  # WARN :: it probably won't work for subcommands of subcommands
  #
  if !subcommand and klass.ancestors.include?( Petra::CLI::Subcommand )
    formatted << "#{ klass.command_invocation } "
  end

  # Add usage with required arguments
  formatted << if klass && !klass.arguments.empty?
    usage.to_s.gsub(/^#{name}/) do |match|
      match << " " << klass.arguments.map{ |a| a.usage }.compact.join(' ')
    end
  else
    usage.to_s
  end

  # Add required options
  formatted << " #{required_options}"

  # Strip and go!
  formatted.strip
end