Class: Option
- Inherits:
-
BaseOptArg
- Object
- BaseOptArg
- Option
- Defined in:
- lib/yaggo/dsl.rb
Instance Attribute Summary collapse
-
#access_types ⇒ Object
Returns the value of attribute access_types.
-
#at_least ⇒ Object
readonly
Returns the value of attribute at_least.
-
#conflict ⇒ Object
Returns the value of attribute conflict.
-
#default ⇒ Object
Returns the value of attribute default.
-
#description ⇒ Object
Returns the value of attribute description.
-
#enum ⇒ Object
Returns the value of attribute enum.
-
#hidden ⇒ Object
Returns the value of attribute hidden.
-
#imply ⇒ Object
Returns the value of attribute imply.
-
#long ⇒ Object
readonly
Returns the value of attribute long.
-
#multiple ⇒ Object
Returns the value of attribute multiple.
-
#noflag ⇒ Object
Returns the value of attribute noflag.
-
#required ⇒ Object
Returns the value of attribute required.
-
#secret ⇒ Object
Returns the value of attribute secret.
-
#short ⇒ Object
readonly
Returns the value of attribute short.
-
#suffix ⇒ Object
readonly
Returns the value of attribute suffix.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#typestr ⇒ Object
Returns the value of attribute typestr.
-
#var ⇒ Object
readonly
Returns the value of attribute var.
Instance Method Summary collapse
- #check ⇒ Object
- #convert_double(x) ⇒ Object
- #convert_int(x, signed = true) ⇒ Object
- #default_str ⇒ Object
- #dump ⇒ Object
- #help ⇒ Object
- #init ⇒ Object
-
#initialize(long, short) ⇒ Option
constructor
A new instance of Option.
- #long_enum ⇒ Object
- #no ⇒ Object
- #off ⇒ Object
- #on ⇒ Object
- #parse_arg(no = false) ⇒ Object
- #short_str ⇒ Object
- #static_decl ⇒ Object
- #struct ⇒ Object
- #switches ⇒ Object
- #tf_to_on_off(v) ⇒ Object
- #var_decl ⇒ Object
Methods inherited from BaseOptArg
#access=, #at_least=, #suffix=, #type=
Constructor Details
#initialize(long, short) ⇒ Option
Returns a new instance of Option.
145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/yaggo/dsl.rb', line 145 def initialize(long, short) @long, @short = long, short @var = (@long || @short).gsub(/[^a-zA-Z0-9_]/, "_") @type = nil @no = false # Also generate the --noswitch for a flag @default = nil @suffix = false @at_least = nil @conflict = [] @enum = [] @imply = [] @access_types = [] end |
Instance Attribute Details
#access_types ⇒ Object
Returns the value of attribute access_types.
141 142 143 |
# File 'lib/yaggo/dsl.rb', line 141 def access_types @access_types end |
#at_least ⇒ Object (readonly)
Returns the value of attribute at_least.
142 143 144 |
# File 'lib/yaggo/dsl.rb', line 142 def at_least @at_least end |
#conflict ⇒ Object
Returns the value of attribute conflict.
141 142 143 |
# File 'lib/yaggo/dsl.rb', line 141 def conflict @conflict end |
#default ⇒ Object
Returns the value of attribute default.
142 143 144 |
# File 'lib/yaggo/dsl.rb', line 142 def default @default end |
#description ⇒ Object
Returns the value of attribute description.
140 141 142 |
# File 'lib/yaggo/dsl.rb', line 140 def description @description end |
#enum ⇒ Object
Returns the value of attribute enum.
142 143 144 |
# File 'lib/yaggo/dsl.rb', line 142 def enum @enum end |
#hidden ⇒ Object
Returns the value of attribute hidden.
141 142 143 |
# File 'lib/yaggo/dsl.rb', line 141 def hidden @hidden end |
#imply ⇒ Object
Returns the value of attribute imply.
143 144 145 |
# File 'lib/yaggo/dsl.rb', line 143 def imply @imply end |
#long ⇒ Object (readonly)
Returns the value of attribute long.
142 143 144 |
# File 'lib/yaggo/dsl.rb', line 142 def long @long end |
#multiple ⇒ Object
Returns the value of attribute multiple.
141 142 143 |
# File 'lib/yaggo/dsl.rb', line 141 def multiple @multiple end |
#noflag ⇒ Object
Returns the value of attribute noflag.
141 142 143 |
# File 'lib/yaggo/dsl.rb', line 141 def noflag @noflag end |
#required ⇒ Object
Returns the value of attribute required.
140 141 142 |
# File 'lib/yaggo/dsl.rb', line 140 def required @required end |
#secret ⇒ Object
Returns the value of attribute secret.
141 142 143 |
# File 'lib/yaggo/dsl.rb', line 141 def secret @secret end |
#short ⇒ Object (readonly)
Returns the value of attribute short.
142 143 144 |
# File 'lib/yaggo/dsl.rb', line 142 def short @short end |
#suffix ⇒ Object (readonly)
Returns the value of attribute suffix.
142 143 144 |
# File 'lib/yaggo/dsl.rb', line 142 def suffix @suffix end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
142 143 144 |
# File 'lib/yaggo/dsl.rb', line 142 def type @type end |
#typestr ⇒ Object
Returns the value of attribute typestr.
140 141 142 |
# File 'lib/yaggo/dsl.rb', line 140 def typestr @typestr end |
#var ⇒ Object (readonly)
Returns the value of attribute var.
142 143 144 |
# File 'lib/yaggo/dsl.rb', line 142 def var @var end |
Instance Method Details
#check ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/yaggo/dsl.rb', line 287 def check pref = "Option #{long || ""}|#{short || ""}:" raise "#{pref} No type specified" if type.nil? if multiple raise "#{pref} Multiple is meaningless with a flag" if type == :flag raise "#{pref} An option marked multiple cannot have a default value" unless default.nil? raise "#{pref} Multiple is incompatible with enum type" if type == :enum end if @type == :flag && noflag && !short.nil? raise "#{pref} flag with 'no' option cannot have a short switch" end super # case @type # when nil # raise "#{pref} No type specified" # when :uint32, :uint64 # @default.nil? || @default =~ /^\d+$/ or # raise "#{pref} Invalid unsigned integer #{@default}" # when :int32, :int64, :int, :long # @default.nil? || @default =~ /^[+-]?\d+$/ or # raise "#{pref} Invalid integer #{@default}" # when :double # @default.nil? || @default =~ /^[+-]?[\d.]+([eE][+-]?\d+)?$/ or # raise "#{pref} Invalid double #{@default}" # when :flag # raise "#{pref} A flag cannot be declared multiple" if @multiple # raise "#{pref} Suffix is meaningless for a flag" if @suffix # end end |
#convert_double(x) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/yaggo/dsl.rb', line 206 def convert_double(x) x =~ /^([+-]?[\d]+(?:\.\d*))?(?:([afpnumkMGTPE])|([eE][+-]?\d+))?$/ or return nil v = "#{$1}#{$3}".to_f case $2 when "a" v *= 1e-18 when "f" v *= 1e-15 when "p" v *= 1e-12 when "n" v *= 1e-9 when "u" v *= 1e-6 when "m" v *= 1e-3 when "k" v *= 1e3 when "M" v *= 1e6 when "G" v *= 1e9 when "T" v *= 1e12 when "P" v *= 1e15 when "E" v *= 1e18 end return v end |
#convert_int(x, signed = true) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/yaggo/dsl.rb', line 185 def convert_int(x, signed = true) x =~ /^([+-]?\d+)([kMGTPE]?)$/ or return nil v = $1.to_i return nil if v < 0 && !signed case $2 when "k" v *= 1000 when "M" v *= 1000_000 when "G" v *= 1000_000_000 when "T" v *= 1000_000_000_000 when "P" v *= 1000_000_000_000_000 when "E" v *= 1000_000_000_000_000_000 end return v end |
#default_str ⇒ Object
393 394 395 396 |
# File 'lib/yaggo/dsl.rb', line 393 def default_str return @default unless @type == :enum @enum[@default || 0] end |
#dump ⇒ Object
407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/yaggo/dsl.rb', line 407 def dump case @type when :flag ["\"#{@var}_flag:\"", "#{@var}_flag"] when :enum ["\"#{@var}_given:\"", "#{@var}_given", "\" #{@var}_arg:\"", "#{@var}_arg", '"|"', "#{@var}::strs[#{@var}_arg]"] else ["\"#{@var}_given:\"", "#{@var}_given", "\" #{@var}_arg:\"", @multiple ? "vec_str(#{@var}_arg)" : "#{@var}_arg"] end end |
#help ⇒ Object
398 399 400 401 402 403 404 405 |
# File 'lib/yaggo/dsl.rb', line 398 def help s = @required ? "*" : " " @description ||= "Switch #{switches}" s += @description.gsub(/"/, '\"') || "" default = default_str s += " (#{default})" unless default.nil? s end |
#init ⇒ Object
349 350 351 352 353 354 355 356 357 |
# File 'lib/yaggo/dsl.rb', line 349 def init s = "#{@var}_#{@type == :flag ? "flag" : "arg"}(" s += default_val(@default, @type, @enum) unless @multiple s += ")" unless @type == :flag s += ", #{@var}_given(false)" end s end |
#long_enum ⇒ Object
359 360 361 362 363 364 365 366 |
# File 'lib/yaggo/dsl.rb', line 359 def long_enum return nil if !@short.nil? res = [@var.upcase + "_OPT"] if @type == :flag && noflag res << "NO#{@var.upcase}_OPT" end res end |
#no ⇒ Object
169 170 171 172 |
# File 'lib/yaggo/dsl.rb', line 169 def no self.type = :flag self.noflag = true end |
#off ⇒ Object
164 165 166 167 |
# File 'lib/yaggo/dsl.rb', line 164 def off self.type = :flag self.default = "false" end |
#on ⇒ Object
159 160 161 162 |
# File 'lib/yaggo/dsl.rb', line 159 def on self.type = :flag self.default = "true" end |
#parse_arg(no = false) ⇒ Object
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
# File 'lib/yaggo/dsl.rb', line 420 def parse_arg(no = false) a = @imply.map { |ios| "#{$opt_hash[ios].var}_flag = true;" } a << "#{@var}_given = true;" unless @type == :flag case @type when :flag if @noflag a << ["#{@var}_flag = #{no ? "false" : "true"};"] else a << ["#{@var}_flag = #{@default == "true" ? "false" : "true"};"] end when :string a << (@multiple ? "#{@var}_arg.push_back(#{str_conv("optarg", @type, false)});" : "#{@var}_arg.assign(optarg);") when :c_string a << (@multiple ? "#{@var}_arg.push_back(#{str_conv("optarg", @type, false)});" : "#{@var}_arg = optarg;") when :uint32, :uint64, :int32, :int64, :int, :long, :double a << (@multiple ? "#{@var}_arg.push_back(#{str_conv("optarg", @type, @suffix)});" : "#{@var}_arg = #{str_conv("optarg", @type, @suffix)};") a << "CHECK_ERR(#{@type}_t, optarg, \"#{switches}\")" when :enum a << "#{@var}_arg = #{str_conv("optarg", @type, "#{@var}::strs")};" a << "CHECK_ERR(#{@type}, optarg, \"#{switches}\")" end a end |
#short_str ⇒ Object
375 376 377 378 |
# File 'lib/yaggo/dsl.rb', line 375 def short_str return nil if @short.nil? @short + (@type == :flag ? "" : ":") end |
#static_decl ⇒ Object
321 322 323 324 325 326 327 328 329 330 |
# File 'lib/yaggo/dsl.rb', line 321 def static_decl a = [] if @type == :enum a << "struct #{@var} {" a << " enum { #{@enum.map { |x| x.gsub(/[^a-zA-Z0-9_]/, "_") }.join(", ")} };" a << " static const char* const strs[#{@enum.size + 1}];" a << "};" end a end |
#struct ⇒ Object
368 369 370 371 372 373 374 |
# File 'lib/yaggo/dsl.rb', line 368 def struct res = ["{\"#{long}\", #{@type == :flag ? 0 : 1}, 0, #{@short ? "'" + @short + "'" : long_enum[0]}}"] if @type == :flag && noflag res << "{\"no#{long}\", 0, 0, #{long_enum()[1]}}" end res end |
#switches ⇒ Object
379 380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'lib/yaggo/dsl.rb', line 379 def switches s = @short.nil? ? " " : "-#{@short}" s += ", " unless @short.nil? || @long.nil? unless @long.nil? if @type == :flag && @noflag s += "--[no]#{@long}" else s += "--#{@long}" end s += "=#{@typestr || dflt_typestr(@type, @enum)}" unless @type == :flag end s end |
#tf_to_on_off(v) ⇒ Object
174 175 176 177 178 179 180 181 182 183 |
# File 'lib/yaggo/dsl.rb', line 174 def tf_to_on_off v case v when "true" "on" when "false" "off" else v end end |
#var_decl ⇒ Object
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/yaggo/dsl.rb', line 332 def var_decl if @type == :flag ["#{"bool".ljust($typejust)} #{@var}_flag;"] else a = [] if @multiple c_type = "::std::vector<#{$type_to_C_type[@type]}>" a << (c_type.ljust($typejust) + " #{@var}_arg;") a << ("typedef #{c_type}::iterator #{@var}_arg_it;") a << ("typedef #{c_type}::const_iterator #{@var}_arg_const_it;") else a << "#{$type_to_C_type[@type].ljust($typejust)} #{@var}_arg;" end a << "#{"bool".ljust($typejust)} #{@var}_given;" end end |