class TTY::Option::Usage

Public Class Methods

create(**properties, &block) click to toggle source

Create an usage

@api public

# File lib/tty/option/usage.rb, line 11
def self.create(**properties, &block)
  new(**properties, &block)
end
new(**properties, &block) click to toggle source

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

banner(value = (not_set = true)) click to toggle source

Main way to show how all parameters can be used

@param [String] value

@api public

banner?() click to toggle source

Whether or not to show banner in usage

@return [Boolean]

@api public

command(*values) click to toggle source

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
Also aliased as: commands
command?() click to toggle source

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
commands(*values)
Alias for: command
desc(*values) click to toggle source

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
Also aliased as: description
desc?() click to toggle source

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
Also aliased as: description?
description(*values)
Alias for: desc
description?()
Alias for: desc?
example(*values) click to toggle source

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
Also aliased as: examples
example?() click to toggle source

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
Also aliased as: examples?
examples(*values)
Alias for: example
examples?()
Alias for: example?
header(*values) click to toggle source

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
header?() click to toggle source

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
no_command() click to toggle source

Remove default commands

@api public

# File lib/tty/option/usage.rb, line 69
def no_command
  @no_command = true
  @properties[:command] = []
end
no_command?() click to toggle source

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 = (not_set = true)) click to toggle source

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
to_h(&block) click to toggle source

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