Class: CommandOptionParser
- Inherits:
-
Object
- Object
- CommandOptionParser
- Defined in:
- 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]
, properties = parser.parse(argv)
p #=> { ?h=>true, ?v=>true, ?f=>"filename", ?i=>true }
p argv #=> ["10", "aaa", "bbb"]
## parse options #2
argv = %w[-hvx.txt -ffilename -i10 aaa bbb]
, properties = parser.parse(argv)
p #=> { ?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]
, properties = parser.parse(argv)
p #=> {?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]
, properties = parser.parse(argv, true)
p #=> {?h=>true, ?i=>true}
p properties #=> {:index=>10, :user_name=>foo, :help=>true}
## -a: unknown option.
argv = %w[-abc]
begin
, properties = parser.parse(argv)
rescue CommandOptionError => ex
$stderr.puts ex. # -a: unknown option.
end
## -f: argument required.
argv = %w[-f]
begin
, properties = parser.parse(argv)
rescue CommandOptionError => ex
$stderr.puts ex. # -f: argument required.
end
## --@prop=10: invalid property.
argv = %w[--@prop=10]
begin
, properties = parser.parse(argv)
rescue CommandOptionError => ex
$stderr.puts ex. # --@prop=10: invalid property.
end
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(arg_none = nil, arg_required = nil, arg_optional = nil) ⇒ CommandOptionParser
constructor
arg_none: option string which takes no argument arg_required: option string which takes argument arg_otpional: option string which may takes argument optionally.
-
#parse(argv, auto_convert = false) ⇒ Object
- argv
- array of string auto_convert
-
if true, convert properties value to int, boolean, string, regexp, …
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/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/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/kwalify/util/option-parser.rb', line 115 def parse(argv, auto_convert=false) = {} 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) [optchar] = true elsif @arg_required.include?(optchar) arg = optstr.empty? ? argv.shift : optstr raise CommandOptionError.new(optchar.chr, :no_argument) unless arg [optchar] = arg optstr = nil elsif @arg_optional.include?(optchar) arg = optstr.empty? ? true : optstr [optchar] = arg optstr = nil else raise CommandOptionError.new(optchar.chr, :unknown_option) end end end # end # end of while return , properties end |