Class: TTY::Option::Usage

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/option/usage.rb

Class Method Summary collapse

Instance Method Summary collapse

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

Main way to show how all parameters can be used

Parameters:

  • value (String) (defaults to: (not_set = true))


119
120
121
122
123
124
125
# File 'lib/tty/option/usage.rb', line 119

def banner(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

Returns:

  • (Boolean)


132
133
134
# File 'lib/tty/option/usage.rb', line 132

def banner?
  @properties.key?(:banner) && !@properties[:banner].nil?
end

#command(*values) ⇒ Object Also known as: commands

Action name for display in help and error messages

Parameters:

  • values (Array<String>)


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

Returns:

  • (Boolean)


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

Parameters:

  • values (Array<String>)


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

Returns:

  • (Boolean)


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

Parameters:

  • values (Array<String>)


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

Returns:

  • (Boolean)


179
180
181
# File 'lib/tty/option/usage.rb', line 179

def example?
  @properties.key?(:example) && !@properties[:example].empty?
end

Display info after everyting else in the usage help

Parameters:

  • values (Array<String>)


189
190
191
192
193
194
195
# File 'lib/tty/option/usage.rb', line 189

def footer(*values)
  if values.empty?
    @properties.fetch(:footer) { [] }
  else
    (@properties[:footer] ||= []) << values
  end
end

#footer?Boolean

Whether or not to show footer in usage

Returns:

  • (Boolean)


202
203
204
# File 'lib/tty/option/usage.rb', line 202

def footer?
  @properties.key?(:footer) && !@properties[:footer].empty?
end

#header(*values) ⇒ Object

Display info before anything else in the usage help

Parameters:

  • values (Array<String>)


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

Returns:

  • (Boolean)


110
111
112
# File 'lib/tty/option/usage.rb', line 110

def header?
  @properties.key?(:header) && !@properties[:header].empty?
end

#no_commandObject

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

Returns:

  • (Boolean)


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

Parameters:

  • name (String) (defaults to: (not_set = true))


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

Returns:

  • (Hash)

    the names and values of this usage



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