Class: Climate::Help

Inherits:
Object
  • Object
show all
Defined in:
lib/climate/help.rb,
lib/climate/help/man.rb

Defined Under Namespace

Classes: Man

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command_class, options = {}) ⇒ Help

Returns a new instance of Help.



6
7
8
9
10
# File 'lib/climate/help.rb', line 6

def initialize(command_class, options={})
  @command_class = command_class
  @indent = 0
  @output = options[:output] || $stdout
end

Instance Attribute Details

#command_classObject (readonly)

Returns the value of attribute command_class.



4
5
6
# File 'lib/climate/help.rb', line 4

def command_class
  @command_class
end

Instance Method Details

#indent(&block) ⇒ Object



86
87
88
89
90
# File 'lib/climate/help.rb', line 86

def indent(&block)
  @indent += 1
  yield if block_given?
  unindent if block_given?
end

#newlineObject



100
101
102
# File 'lib/climate/help.rb', line 100

def newline
  @output.puts("\n")
end


12
13
14
15
16
17
18
# File 'lib/climate/help.rb', line 12

def print(options={})
  run_pager if options[:pager]
  print_usage
  print_description
  print_options if command_class.has_options? || command_class.has_arguments?
  print_subcommands if command_class.has_subcommands?
end


32
33
34
35
36
37
38
# File 'lib/climate/help.rb', line 32

def print_description
  newline
  puts "Description"
  indent do
    puts(command_class.description || '')
  end
end


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/climate/help.rb', line 50

def print_options
  newline
  puts "Options"
  indent do

    if command_class.has_subcommands?
      puts "<subcommand>"
      indent do
        puts "Name of subcommand to execute"
      end
      newline
      puts "<arguments>"
      indent do
        puts "Arguments for subcommand"
      end
      newline
    end

    command_class.cli_arguments.each do |argument|
      puts "<#{argument.name}>"
      indent do
        puts argument.description
      end
      newline
    end

    command_class.cli_options.each do |option|
      puts "#{option.usage(:with_long => true, :hide_optional => true, :separator => ', ')}"
      indent do
        puts option.description
      end
      newline
    end
  end
end


40
41
42
43
44
45
46
47
48
# File 'lib/climate/help.rb', line 40

def print_subcommands
  newline
  puts "Available subcommands:"
  indent do
    command_class.subcommands.each do |subcommand_class|
      puts "#{subcommand_class.command_name}"
    end
  end
end


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/climate/help.rb', line 20

def print_usage
  ancestor_list = command_class.ancestors.map(&:command_name).join(' ')
  opts_usage = command_class.cli_options.map {|opt| opt.usage }
  args_usage =
    if command_class.has_subcommands?
      ["<subcommand> [<arguments>...]"]
    else
      command_class.cli_arguments.map {|arg| arg.usage }
    end
  puts("usage: #{ancestor_list} #{(opts_usage + args_usage).join(' ')}")
end

#puts(string = '') ⇒ Object



104
105
106
107
108
# File 'lib/climate/help.rb', line 104

def puts(string='')
  wrap(string).split("\n").each do |line|
    @output.puts((' ' * spaces) + line)
  end
end

#spacesObject



96
97
98
# File 'lib/climate/help.rb', line 96

def spaces
  @indent * 4
end

#unindentObject



92
93
94
# File 'lib/climate/help.rb', line 92

def unindent
  @indent -= 1
end