Class: CommandOptionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/util/option-parser.rb

Overview

ex.

## create parser
arg_none     = "hv"      # ex. -h -v
arg_required = "xf"      # ex. -x suffix -f filename
arg_optional = "i"       # ex. -i  (or -i10)
parser = CommandOptionParser.new(arg_none, arg_required, arg_optional)

## parse options
argv = %w[-h -v -f filename -i 10 aaa bbb]
options, properties = parser.parse(argv)
p options   #=> { ?h=>true, ?v=>true, ?f=>"filename", ?i=>true }
p argv      #=> ["10", "aaa", "bbb"]

## parse options #2
argv = %w[-hvx.txt -ffilename -i10 aaa bbb]
options, properties = parser.parse(argv)
p options   #=> { ?h=>true, ?v=>true, ?x=>".txt", ?f=>"filename", ?i=>10 }
p argv      #=> ["aaa", "bbb"]

## parse properties
argv = %w[-hi --index=10 --user-name=foo --help]
options, properties = parser.parse(argv)
p options     #=> {?h=>true, ?i=>true}
p properties  #=> {"index"=>"10", "user-name"=>"foo", "help"=>nil}

## parse properties with auto-convert
argv = %w[-hi --index=10 --user-name=foo --help]
options, properties = parser.parse(argv, true)
p options     #=> {?h=>true, ?i=>true}
p properties  #=> {:index=>10, :user_name=>foo, :help=>true}

## -a: unknown option.
argv = %w[-abc]
begin
   options, properties = parser.parse(argv)
rescue CommandOptionError => ex
   $stderr.puts ex.message     # -a: unknown option.
end

## -f: argument required.
argv = %w[-f]
begin
   options, properties = parser.parse(argv)
rescue CommandOptionError => ex
   $stderr.puts ex.message     # -f: argument required.
end

## --@prop=10: invalid property.
argv = %w[--@prop=10]
begin
   options, properties = parser.parse(argv)
rescue CommandOptionError => ex
   $stderr.puts ex.message     # --@prop=10: invalid property.
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg_none = nil, arg_required = nil, arg_optional = nil) ⇒ CommandOptionParser

arg_none: option string which takes no argument arg_required: option string which takes argument arg_otpional: option string which may takes argument optionally



92
93
94
95
96
# File 'lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/util/option-parser.rb', line 92

def initialize(arg_none=nil, arg_required=nil, arg_optional=nil)
  @arg_none      = arg_none     || ""
  @arg_required  = arg_required || ""
  @arg_optional  = arg_optional || ""
end

Class Method Details

.to_value(str) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/util/option-parser.rb', line 99

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

Instance Method Details

#parse(argv, auto_convert = false) ⇒ Object

argv

array of string

auto_convert

if true, convert properties value to int, boolean, string, regexp, … (default false)



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/util/option-parser.rb', line 115

def parse(argv, auto_convert=false)
  options = {}
  properties = {}
  while argv[0] && argv[0][0] == ?-
    optstr = argv.shift
    optstr = optstr[1, optstr.length-1]
    #
    if optstr[0] == ?-    ## property
      unless optstr =~ /\A\-([-\w]+)(?:=(.*))?/
        raise CommandOptionError.new(optstr, :invalid_property)
      end
      prop_name = $1;  prop_value = $2
      if auto_convert
        key   = prop_name.gsub(/-/, '_').intern
        value = prop_value.nil? ? true : CommandOptionParser.to_value(prop_value)
        properties[key] = value
      else
        properties[prop_name] = prop_value
      end
      #
    else                  ## options
      while optstr && !optstr.empty?
        optchar = optstr[0]
        optstr[0,1] = ""
        #puts "*** debug: optchar=#{optchar.chr}, optstr=#{optstr.inspect}"
        if @arg_none.include?(optchar)
          options[optchar] = true
        elsif @arg_required.include?(optchar)
          arg = optstr.empty? ? argv.shift : optstr
          raise CommandOptionError.new(optchar.chr, :no_argument) unless arg
          options[optchar] = arg
          optstr = nil
        elsif @arg_optional.include?(optchar)
          arg = optstr.empty? ? true : optstr
          options[optchar] = arg
          optstr = nil
        else
          raise CommandOptionError.new(optchar.chr, :unknown_option)
        end
      end
    end
    #
  end  # end of while

  return options, properties
end