Module: QB::Options::Option::OptionParserConcern

Extended by:
ActiveSupport::Concern, MethodDecorators
Includes:
NRSER::Log::Mixin
Included in:
QB::Options::Option
Defined in:
lib/qb/options/option/option_parser_concern.rb

Defined Under Namespace

Classes: TypeAcceptable

Instance Method Summary collapse

Instance Method Details

#option_parser_add(option_parser, included:) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/qb/options/option/option_parser_concern.rb', line 202

def option_parser_add option_parser, included:
  args = option_parser_args( included: included ).to_a
  
  args.find do |arg|
    if arg.is_a?( Class )
      option_parser.accept( arg ) do |value|
        arg.parse value
      end
      
      true
    end
  end
  
  logger.trace "Adding option to {OptionParser}",
    option_meta_name: meta_name,
    args: args
  
  option_parser.on( *args ) do |value|
    logger.debug "Setting option value",
      option_meta_name: meta_name,
      value: value
    
    self.value = value
  end
end

#option_parser_args(included:) {|"REQUIRED."| ... } ⇒ Object

Yields:

  • ("REQUIRED.")


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/qb/options/option/option_parser_concern.rb', line 180

def option_parser_args included:, &block
  if boolean?
    option_parser_bool_args included: included, &block
  else
    option_parser_non_bool_args included: included, &block
  end
  
  option_parser_description &block
  
  yield "REQUIRED." if required?
  
  option_parser_type &block
  
  option_parser_default &block
  
  option_parser_examples &block
  
  yield option_parser_spacer
  
end

#option_parser_bool_args(included:) {|"--[no-]#{ cli_name }"| ... } ⇒ Object

Yields:



108
109
110
111
112
113
114
115
# File 'lib/qb/options/option/option_parser_concern.rb', line 108

def option_parser_bool_args included:, &block
  # Don't use short names when included (for now)
  if !included && meta[:short]
    yield "-#{ meta[:short] }"
  end
  
  yield "--[no-]#{ cli_name }"
end

#option_parser_default(&block) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/qb/options/option/option_parser_concern.rb', line 137

def option_parser_default &block
  ans_src_default = role.defaults[var_name]
  
  # If we don't have shit in the role default Ansible source file, nothing
  # to do here
  return if ans_src_default.nil?
  
  if boolean?
    yield "DEFAULT: --#{ ans_src_default ? '' : 'no-' }#{ cli_name }"
  else
    # This is just the Ansible "source code", which is shitty and ugly
    # for anything that's not a literal, but it at least gives you something
    # to see
    option_parser_format_multiline "DEFAULT: #{ ans_src_default }", &block
  end
end

#option_parser_description(&block) ⇒ Object



102
103
104
# File 'lib/qb/options/option/option_parser_concern.rb', line 102

def option_parser_description &block
  option_parser_format_multiline description, &block
end

#option_parser_examples {|'Examples:'| ... } ⇒ Object

Yields:

  • ('Examples:')


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/qb/options/option/option_parser_concern.rb', line 156

def option_parser_examples &block
  return unless has_examples?
  
  yield 'Examples:'
  
  examples.each_with_index do |example, index|
    lines = example.lines.to_a
    
    yield ((index + 1).to_s + '.').ljust(4) + lines.first.chomp
    
    lines[1..-1].each do |line|
      yield " ".ljust(4) + line.chomp
    end
  end
end

#option_parser_format_multiline(string) {|option_parser_spacer| ... } ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/qb/options/option/option_parser_concern.rb', line 82

def option_parser_format_multiline string, &block
  lines = string.lines.to_a
  
  lines.map do |line|
    yield case line
    when "\n"
      # Need a space for {OptionParser} to respect it
      option_parser_spacer + line
    when /\A\s*\-\ /
      line.sub '-', '*'
    else
      line
    end
  end
  
  yield option_parser_spacer if lines.length > 1
end

#option_parser_non_bool_args(included:) {|option_parser_type_acceptable| ... } ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/qb/options/option/option_parser_concern.rb', line 119

def option_parser_non_bool_args included:, &block
  # don't use short names when included (for now)
  if !included && meta[:short]
    yield "-#{ meta[:short] } #{ option_parser_value_name }"
  end
  
  # We allow options to also accept
  if accept_false?
    yield "--[no-]#{ cli_name }=#{ option_parser_value_name }"
  else
    yield "--#{ cli_name }=#{ option_parser_value_name }"
  end
  
  yield option_parser_type_acceptable
end

#option_parser_spacerObject



64
65
66
# File 'lib/qb/options/option/option_parser_concern.rb', line 64

def option_parser_spacer
  ' '
end

#option_parser_type(&block) ⇒ Object



174
175
176
# File 'lib/qb/options/option/option_parser_concern.rb', line 174

def option_parser_type &block
  option_parser_format_multiline "TYPE: #{ type }", &block
end

#option_parser_type_acceptableObject



69
70
71
72
73
# File 'lib/qb/options/option/option_parser_concern.rb', line 69

def option_parser_type_acceptable
  acceptable = Class.new TypeAcceptable
  acceptable.instance_variable_set :@type, self.type
  acceptable
end

#option_parser_value_nameObject



76
77
78
# File 'lib/qb/options/option/option_parser_concern.rb', line 76

def option_parser_value_name
  meta_name.upcase
end