Class: XOptionParser::Switch::SimpleArgument

Inherits:
SummarizeArgument show all
Defined in:
lib/xoptparse.rb

Instance Attribute Summary

Attributes inherited from SummarizeArgument

#arg_parameters, #ranges

Instance Method Summary collapse

Methods inherited from SummarizeArgument

#match_nonswitch?, #summarize, #switch_name

Constructor Details

#initializeSimpleArgument



215
216
217
218
219
220
# File 'lib/xoptparse.rb', line 215

def initialize(*)
  super
  @ranges = arg_parameters.map do |_name, type, rest|
    (type == :req ? 1 : 0)..(rest == :rest ? nil : 1)
  end
end

Instance Method Details

#add_banner(to) ⇒ Object



222
223
224
# File 'lib/xoptparse.rb', line 222

def add_banner(to)
  to << " #{arg}"
end

#parse(arg, argv) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/xoptparse.rb', line 226

def parse(arg, argv) # rubocop:disable Metrics/CyclomaticComplexity
  case ranges.size
  when 0
    yield(NeedlessArgument, arg) if arg
    conv_arg(arg)
  when 1
    unless arg
      raise XOptionParser::MissingArgument if argv.empty?

      arg = argv.shift
    end
    arg = [arg] if ranges.first.end.nil?
    conv_arg(*parse_arg(arg, &method(:raise)))
  else
    super(arg, argv)
  end
end