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(options = {}) ⇒ 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
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 |
#newline ⇒ Object
100 101 102 |
# File 'lib/climate/help.rb', line 100 def newline @output.puts("\n") end |
#print(options = {}) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/climate/help.rb', line 12 def print(={}) run_pager if [:pager] print_usage print_description if command_class. || command_class.has_arguments? print_subcommands if command_class.has_subcommands? end |
#print_description ⇒ Object
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 |
#print_options ⇒ Object
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 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
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 |
#print_usage ⇒ Object
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..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 |
#spaces ⇒ Object
96 97 98 |
# File 'lib/climate/help.rb', line 96 def spaces @indent * 4 end |
#unindent ⇒ Object
92 93 94 |
# File 'lib/climate/help.rb', line 92 def unindent @indent -= 1 end |