Class: TTY::Option::Usage
- Inherits:
-
Object
- Object
- TTY::Option::Usage
- Defined in:
- lib/tty/option/usage.rb
Class Method Summary collapse
-
.create(**properties, &block) ⇒ Object
Create an usage.
Instance Method Summary collapse
-
#banner(value = (not_set = true)) ⇒ Object
Main way to show how all parameters can be used.
-
#banner? ⇒ Boolean
Whether or not to show banner in usage.
-
#command(*values) ⇒ Object
(also: #commands)
Action name for display in help and error messages.
-
#command? ⇒ Boolean
Check for command definition.
-
#desc(*values) ⇒ Object
(also: #description)
Description.
-
#desc? ⇒ Boolean
(also: #description?)
Whether or not to show description in usage.
-
#example(*values) ⇒ Object
(also: #examples)
Collects usage examples.
-
#example? ⇒ Boolean
(also: #examples?)
Whether or not to show example in usage.
-
#footer(*values) ⇒ Object
Display info after everyting else in the usage help.
-
#footer? ⇒ Boolean
Whether or not to show footer in usage.
-
#header(*values) ⇒ Object
Display info before anything else in the usage help.
-
#header? ⇒ Boolean
Whether or not to show header in usage.
-
#initialize(**properties, &block) ⇒ Usage
constructor
Create an usage.
-
#no_command ⇒ Object
Remove default commands.
-
#no_command? ⇒ Boolean
Whether or not to show command in usage.
-
#program(name = (not_set = true)) ⇒ Object
Program name for display in help and error messages.
-
#to_h(&block) ⇒ Hash
Return a hash of this usage properties.
Constructor Details
#initialize(**properties, &block) ⇒ Usage
Create an usage
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/tty/option/usage.rb', line 18 def initialize(**properties, &block) @properties = {} @no_command = false properties.each do |key, val| case key.to_sym when :command key, val = :command, Array(val) when :desc, :description key, val = :desc, [Array(val)] when :header, :footer val = [Array(val)] when :example, :examples key, val = :example, [Array(val)] end @properties[key.to_sym] = val end instance_eval(&block) if block_given? end |
Class Method Details
.create(**properties, &block) ⇒ Object
Create an usage
11 12 13 |
# File 'lib/tty/option/usage.rb', line 11 def self.create(**properties, &block) new(**properties, &block) end |
Instance Method Details
#banner(value = (not_set = true)) ⇒ Object
Main way to show how all parameters can be used
119 120 121 122 123 124 125 |
# File 'lib/tty/option/usage.rb', line 119 def (value = (not_set = true)) if not_set @properties[:banner] else @properties[:banner] = value end end |
#banner? ⇒ Boolean
Whether or not to show banner in usage
132 133 134 |
# File 'lib/tty/option/usage.rb', line 132 def @properties.key?(:banner) && !@properties[:banner].nil? end |
#command(*values) ⇒ Object Also known as: commands
Action name for display in help and error messages
56 57 58 59 60 61 62 63 |
# File 'lib/tty/option/usage.rb', line 56 def command(*values) if values.empty? @properties.fetch(:command) { [] } else @properties[:command] = [] values.each { |val| @properties[:command] << val } end end |
#command? ⇒ Boolean
Check for command definition
88 89 90 |
# File 'lib/tty/option/usage.rb', line 88 def command? @properties.key?(:command) && !@properties[:command].empty? end |
#desc(*values) ⇒ Object Also known as: description
Description
141 142 143 144 145 146 147 |
# File 'lib/tty/option/usage.rb', line 141 def desc(*values) if values.empty? @properties.fetch(:desc) { [] } else (@properties[:desc] ||= []) << values end end |
#desc? ⇒ Boolean Also known as: description?
Whether or not to show description in usage
155 156 157 |
# File 'lib/tty/option/usage.rb', line 155 def desc? @properties.key?(:desc) && !@properties[:desc].empty? end |
#example(*values) ⇒ Object Also known as: examples
Collects usage examples
165 166 167 168 169 170 171 |
# File 'lib/tty/option/usage.rb', line 165 def example(*values) if values.empty? @properties.fetch(:example) { [] } else (@properties[:example] ||= []) << values end end |
#example? ⇒ Boolean Also known as: examples?
Whether or not to show example in usage
179 180 181 |
# File 'lib/tty/option/usage.rb', line 179 def example? @properties.key?(:example) && !@properties[:example].empty? end |
#footer(*values) ⇒ Object
Display info after everyting else in the usage help
189 190 191 192 193 194 195 |
# File 'lib/tty/option/usage.rb', line 189 def (*values) if values.empty? @properties.fetch(:footer) { [] } else (@properties[:footer] ||= []) << values end end |
#footer? ⇒ Boolean
Whether or not to show footer in usage
202 203 204 |
# File 'lib/tty/option/usage.rb', line 202 def @properties.key?(:footer) && !@properties[:footer].empty? end |
#header(*values) ⇒ Object
Display info before anything else in the usage help
97 98 99 100 101 102 103 |
# File 'lib/tty/option/usage.rb', line 97 def header(*values) if values.empty? @properties.fetch(:header) { [] } else (@properties[:header] ||= []) << values end end |
#header? ⇒ Boolean
Whether or not to show header in usage
110 111 112 |
# File 'lib/tty/option/usage.rb', line 110 def header? @properties.key?(:header) && !@properties[:header].empty? end |
#no_command ⇒ Object
Remove default commands
69 70 71 72 |
# File 'lib/tty/option/usage.rb', line 69 def no_command @no_command = true @properties[:command] = [] end |
#no_command? ⇒ Boolean
Whether or not to show command in usage
79 80 81 |
# File 'lib/tty/option/usage.rb', line 79 def no_command? @no_command end |
#program(name = (not_set = true)) ⇒ Object
Program name for display in help and error messages
43 44 45 46 47 48 49 |
# File 'lib/tty/option/usage.rb', line 43 def program(name = (not_set = true)) if not_set @properties.fetch(:program) { ::File.basename($0, ".*") } else @properties[:program] = name end end |
#to_h(&block) ⇒ Hash
Return a hash of this usage properties
211 212 213 214 215 216 217 218 219 220 |
# File 'lib/tty/option/usage.rb', line 211 def to_h(&block) if block_given? @properties.each_with_object({}) do |(key, val), acc| k, v = *block.(key, val) acc[k] = v end else DeepDup.deep_dup(@properties) end end |