Class: Migr8::Util::CommandOptionParser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix = nil) ⇒ CommandOptionParser

Returns a new instance of CommandOptionParser.



2022
2023
2024
2025
# File 'lib/migr8.rb', line 2022

def initialize(prefix=nil)
  @prefix = prefix
  @optdefs = []
end

Instance Attribute Details

#optdefsObject (readonly)

Returns the value of attribute optdefs.



2020
2021
2022
# File 'lib/migr8.rb', line 2020

def optdefs
  @optdefs
end

Instance Method Details

#add(optdef) ⇒ Object



2027
2028
2029
2030
2031
2032
2033
# File 'lib/migr8.rb', line 2027

def add(optdef)
  #; [!tm89j] parses definition string and adds optdef object.
  optdef = CommandOptionDefinition.new(optdef) if optdef.is_a?(String)
  @optdefs << optdef
  #; [!00kvl] returns self.
  return self
end

#parse(args) ⇒ Object



2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
# File 'lib/migr8.rb', line 2035

def parse(args)
  options = {}
  while ! args.empty? && args[0] =~ /\A-/
    optstr = args.shift
    if optstr =~ /\A--/
      #; [!2jo9d] stops to parse options when '--' found.
      break if optstr == '--'
      #; [!7pa2x] raises error when invalid long option.
      optstr =~ /\A--(\w[-\w]+)(?:=(.*))?\z/  or
        raise cmdopterr("#{optstr}: invalid option format.")
      #; [!sj0cv] raises error when unknown long option.
      long, argval = $1, $2
      optdef = @optdefs.find {|x| x.long == long }  or
        raise cmdopterr("#{optstr}: unknown option.")
      #; [!a7qxw] raises error when argument required but not provided.
      if optdef.arg_required == true && argval.nil?
        raise cmdopterr("#{optstr}: argument required.")
      #; [!8eu9s] raises error when option takes no argument but provided.
      elsif optdef.arg_required == false && argval
        raise cmdopterr("#{optstr}: unexpected argument.")
      end
      #; [!1l2dn] when argname is 'N'...
      if optdef.arg == 'N' && argval
        #; [!cfjp3] raises error when argval is not an integer.
        argval =~ /\A-?\d+\z/  or
          raise cmdopterr("#{optstr}: integer expected.")
        #; [!18p1g] raises error when argval <= 0.
        argval = argval.to_i
        argval > 0  or
          raise cmdopterr("#{optstr}: positive value expected.")
      end
      #; [!dtbdd] uses option name instead of long name when option name specified.
      #; [!7mp75] sets true as value when argument is not provided.
      options[optdef.name] = argval.nil? ? true : argval
    elsif optstr =~ /\A-/
      i = 1
      while i < optstr.length
        ch = optstr[i].chr
        #; [!8aaj0] raises error when unknown short option provided.
        optdef = @optdefs.find {|x| x.short == ch }  or
          raise cmdopterr("-#{ch}: unknown option.")
        #; [!mnwxw] when short option takes no argument...
        if optdef.arg_required == false      # no argument
          #; [!8atm1] sets true as value.
          options[optdef.name] = true
          i += 1
        #; [!l5mee] when short option takes required argument...
        elsif optdef.arg_required == true    # required argument
          #; [!crvxx] uses following string as argument.
          argval = optstr[(i+1)..-1]
          if argval.empty?
            #; [!7t6l3] raises error when no argument provided.
            ! args.empty?  or
              raise cmdopterr("-#{ch}: argument required.")
            argval = args.shift
          end
          #; [!h3gt8] when argname is 'N'...
          if optdef.arg == 'N'
            #; [!yzr2p] argument must be an integer.
            argval =~ /\A-?\d+\z/  or
              raise cmdopterr("-#{ch} #{argval}: integer expected.")
            #; [!mcwu7] argument must be positive value.
            argval = argval.to_i
            argval > 0  or
              raise cmdopterr("-#{ch} #{argval}: positive value expected.")
          end
          #
          options[optdef.name] = argval
          break
        #; [!pl97z] when short option takes optional argument...
        elsif optdef.arg_required == nil     # optional argument
          #; [!4k3zy] uses following string as argument if provided.
          argval = optstr[(i+1)..-1]
          if argval.empty?
            #; [!9k2ip] uses true as argument value if not provided.
            argval = true
          end
          #; [!lk761] when argname is 'N'...
          if optdef.arg == 'N' && argval.is_a?(String)
            #; [!6oy04] argument must be an integer.
            argval =~ /\A-?\d+\z/  or
              raise cmdopterr("-#{ch}#{argval}: integer expected.")
            #; [!nc3av] argument must be positive value.
            argval = argval.to_i
            argval > 0  or
              raise cmdopterr("-#{ch}#{argval}: positive value expected.")
          end
          #
          options[optdef.name] = argval
          break
        else
          raise "** unreachable"
        end
      end#while
    end#if
  end#while
  #; [!35eof] returns parsed options.
  return options
end

#usage(width = 20, indent = '') ⇒ Object

def



2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
# File 'lib/migr8.rb', line 2135

def usage(width=20, indent='')
  width = 20 if width.nil?
  #; [!w9v9c] returns usage string of all options.
  s = ""
  @optdefs.each do |optdef|
    #; [!i0uvr] adds indent when specified.
    #; [!lbjai] skips options when desc is empty.
    s << "#{indent}#{optdef.usage(width)}\n" if optdef.desc
  end
  return s
end