Class: Kwaff::CommandOptionParser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(noarg_opts = '', argneed_opts = '', argoptional_opts = '', flag_str2value = true) ⇒ CommandOptionParser

Returns a new instance of CommandOptionParser.



18
19
20
21
22
23
24
25
# File 'lib/kwaff/optparse.rb', line 18

def initialize(noarg_opts='', argneed_opts='', argoptional_opts='', flag_str2value=true)
   @noarg_opts       = noarg_opts
   @argneed_opts     = argneed_opts
   @argoptional_opts = argoptional_opts
   @flag_str2value   = flag_str2value
   @options    = nil
   @properties = nil
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



26
27
28
# File 'lib/kwaff/optparse.rb', line 26

def options
  @options
end

#properitesObject (readonly)

Returns the value of attribute properites.



26
27
28
# File 'lib/kwaff/optparse.rb', line 26

def properites
  @properites
end

Instance Method Details

#parse(argv = ARGV) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/kwaff/optparse.rb', line 29

def parse(argv=ARGV)
   options    = {}
   properties = {}

   while !argv.empty? && argv[0][0] == ?-
      opt = argv.shift
      if opt[1] == ?-			## properteis
         propstr = opt.sub(/^--/, '')
         unless propstr =~ /^([-\w]+)(?:=(.*))?$/
            raise CommandOptionError.new("--#{optstr}: invalid property.")
         end
         name  = $1
         value = $2
         name.gsub!(/-/, '_')
         if !value
            value = true
         elsif flag_str2value
            value = self.to_value(value)
         end
         properties[name.intern] = value

      else				## options
         optstr = opt.sub(/^-/, '')
         while optstr && !optstr.empty?
            optchar = optstr[0]
            optstr = optstr[1, optstr.length-1]
            if @noarg_opts.include?(optchar)
               options[optchar] = true
            elsif @argneed_opts.include?(optchar)
               arg = optstr
               optstr = nil
               arg = argv.shift if !arg || arg.empty?
               unless arg
                  raise CommandOptionError.new("-#{optchar.chr}: argument required.")
               end
               options[optchar] = arg
            elsif @argoptional_opts.include?(optchar)
               arg = optstr && !optstr.empty? ? optstr : true
               optstr = nil
               options[optchar] = arg
            else
               raise CommandOptionError.new("-#{optchar.chr}: invalid option.")
            end
         end

      end   # if

   end   # while

   @options = options
   @properties = properties
   return options, properties
end

#to_value(str) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/kwaff/optparse.rb', line 84

def to_value(str)
   case str
   when 'true', 'yes'     ;  return true
   when 'false', 'no'     ;  return false
   when 'null', 'nil'     ;  return nil
   when /\A\d+\z/         ;  return str.to_i
   when /\A\d+\.\d+\z/    ;  return str.to_f
   when /\A\/(.*)\/\z/    ;  return Regexp.new($1)
   when /\A'.*'\z/        ;  return eval(str)
   when /\A".*"\z/        ;  return eval(str)
   else
      return str
   end
end