class TTY::Option::Usage
Public Class Methods
Create an usage
@api public
# File lib/tty/option/usage.rb, line 11 def self.create(**properties, &block) new(**properties, &block) end
Create an usage
@api public
# 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
Public Instance Methods
Action name for display in help and error messages
@param [Array<String>] values
@api public
# 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
Check for command definition
@return [Boolean]
@api public
# File lib/tty/option/usage.rb, line 88 def command? @properties.key?(:command) && !@properties[:command].empty? end
Description
@param [Array<String>] values
@api public
# File lib/tty/option/usage.rb, line 141 def desc(*values) if values.empty? @properties.fetch(:desc) { [] } else (@properties[:desc] ||= []) << values end end
Whether or not to show description in usage
@return [Boolean]
@api public
# File lib/tty/option/usage.rb, line 155 def desc? @properties.key?(:desc) && !@properties[:desc].empty? end
Collects usage examples
@param [Array<String>] values
@api public
# File lib/tty/option/usage.rb, line 165 def example(*values) if values.empty? @properties.fetch(:example) { [] } else (@properties[:example] ||= []) << values end end
Whether or not to show example in usage
@return [Boolean]
@api public
# File lib/tty/option/usage.rb, line 179 def example? @properties.key?(:example) && !@properties[:example].empty? end
Display info before anything else in the usage help
@param [Array<String>] values
@api public
# File lib/tty/option/usage.rb, line 97 def header(*values) if values.empty? @properties.fetch(:header) { [] } else (@properties[:header] ||= []) << values end end
Whether or not to show header in usage
@return [Boolean]
@api public
# File lib/tty/option/usage.rb, line 110 def header? @properties.key?(:header) && !@properties[:header].empty? end
Remove default commands
@api public
# File lib/tty/option/usage.rb, line 69 def no_command @no_command = true @properties[:command] = [] end
Whether or not to show command in usage
@retrun [Boolean]
@api public
# File lib/tty/option/usage.rb, line 79 def no_command? @no_command end
Program name for display in help and error messages
@param [String] name
@api public
# 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
Return a hash of this usage properties
@return [Hash] the names and values of this usage
@api public
# 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