Class: Climate::Help
- Inherits:
-
Object
- Object
- Climate::Help
- Defined in:
- lib/climate/help.rb,
lib/climate/help/man.rb
Defined Under Namespace
Classes: Man
Instance Attribute Summary collapse
-
#command_class ⇒ Object
readonly
Returns the value of attribute command_class.
Instance Method Summary collapse
- #indent(&block) ⇒ Object
-
#initialize(command_class, options = {}) ⇒ Help
constructor
A new instance of Help.
- #newline ⇒ Object
- #print ⇒ Object
- #print_description ⇒ Object
- #print_options ⇒ Object
- #print_subcommands ⇒ Object
- #print_usage ⇒ Object
- #puts(string = '') ⇒ Object
- #spaces ⇒ Object
- #unindent ⇒ Object
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, ={}) @command_class = command_class @indent = 0 @output = [:output] || $stdout end |
Instance Attribute Details
#command_class ⇒ Object (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 |
#newline ⇒ Object
99 100 101 |
# File 'lib/climate/help.rb', line 99 def newline @output.puts("\n") end |
#print ⇒ Object
12 13 14 15 16 17 |
# File 'lib/climate/help.rb', line 12 def print print_usage print_description if command_class. || command_class.has_arguments? print_subcommands if command_class.has_subcommands? end |
#print_description ⇒ Object
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 |
#print_options ⇒ Object
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 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..each do |option| puts "#{option.usage(:with_long => true, :hide_optional => true, :separator => ', ')}" indent do puts option.description end newline end end end |
#print_subcommands ⇒ Object
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 |
#print_usage ⇒ Object
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..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 |
#spaces ⇒ Object
95 96 97 |
# File 'lib/climate/help.rb', line 95 def spaces @indent * 4 end |
#unindent ⇒ Object
91 92 93 |
# File 'lib/climate/help.rb', line 91 def unindent @indent -= 1 end |