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



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

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

#newlineObject



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

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


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

def print
  print_usage
  print_description
  print_options if command_class.has_options? || command_class.has_arguments?
  print_subcommands if command_class.has_subcommands?
end


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

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


49
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
# File 'lib/climate/help.rb', line 49

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


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

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


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

def print_usage
  ancestor_list = command_class.ancestors.map(&: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



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

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

#spacesObject



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

def spaces
  @indent * 4
end

#unindentObject



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

def unindent
  @indent -= 1
end