Class: OptionParser::Switch::RequiredArgument

Inherits:
Object
  • Object
show all
Defined in:
lib/ridl/optparse_ext.rb

Overview

Customize OptionParser RequiredArgument switch class to support multi character short switches (single ‘-’ prefix) with (optional) arguments. These must be defined using a format like ‘-X<text>’ or ‘-Xtext’ where ‘X’ is the common start character for a group of short multichar switches. Switch arguments should be indicated by either appending ‘=ARG’ or ‘ ARG’ giving something like ‘-X<text>=ARG’ or ‘-X<text> ARG’ where ‘ARG’ is an arbitrary non-blank text

Instance Method Summary collapse

Constructor Details

#initialize(pattern = nil, conv = nil, short = nil, long = nil, arg = nil, desc = ([] if short or long), block = Proc.new) ⇒ RequiredArgument

Returns a new instance of RequiredArgument.



25
26
27
28
29
30
31
32
33
# File 'lib/ridl/optparse_ext.rb', line 25

def initialize(pattern = nil, conv = nil,
               short = nil, long = nil, arg = nil,
               desc = ([] if short or long), block = Proc.new)
  super
  if (@long.nil? || @long.empty?) && (@arg =~ /^(<.*>|[\{].*[\}])((=|\s).*)?/)
    @multichar_short = true
    @has_arg = (@arg =~ /^(<.*>|[\{].*[\}])(=|\s).*$/ ? true : false)
  end
end

Instance Method Details

#_org_parseObject



34
# File 'lib/ridl/optparse_ext.rb', line 34

alias :_org_parse :parse

#parse(arg, argv) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ridl/optparse_ext.rb', line 35

def parse(arg, argv)
  if @multichar_short && @has_arg
    # unless arg included in rest of switch or next arg is not a switch
    unless (arg && arg =~ /.*=.*/) || (argv.first =~ /^-/)
      # concatenate next arg
      arg ||= ''
      arg += "=#{argv.shift}"
    end
  end
  self._org_parse(arg, argv)
end