Class: Arg

Inherits:
BaseOptArg show all
Defined in:
lib/yaggo/dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseOptArg

#access=, #at_least=, #suffix=

Constructor Details

#initialize(str) ⇒ Arg

Returns a new instance of Arg.



448
449
450
451
452
453
454
455
# File 'lib/yaggo/dsl.rb', line 448

def initialize(str)
  @name = str
  @var = @name.gsub(/[^a-zA-Z0-9_]/, "_")
  @type = nil
  @at_least = 0
  @suffix = false
  @access_types = []
end

Instance Attribute Details

#access_typesObject

Returns the value of attribute access_types.



446
447
448
# File 'lib/yaggo/dsl.rb', line 446

def access_types
  @access_types
end

#at_leastObject (readonly)

Returns the value of attribute at_least.



447
448
449
# File 'lib/yaggo/dsl.rb', line 447

def at_least
  @at_least
end

#descriptionObject

Returns the value of attribute description.



446
447
448
# File 'lib/yaggo/dsl.rb', line 446

def description
  @description
end

#multipleObject

Returns the value of attribute multiple.



446
447
448
# File 'lib/yaggo/dsl.rb', line 446

def multiple
  @multiple
end

#nameObject (readonly)

Returns the value of attribute name.



447
448
449
# File 'lib/yaggo/dsl.rb', line 447

def name
  @name
end

#suffixObject (readonly)

Returns the value of attribute suffix.



447
448
449
# File 'lib/yaggo/dsl.rb', line 447

def suffix
  @suffix
end

#typeObject

Returns the value of attribute type.



446
447
448
# File 'lib/yaggo/dsl.rb', line 446

def type
  @type
end

#typestrObject

Returns the value of attribute typestr.



446
447
448
# File 'lib/yaggo/dsl.rb', line 446

def typestr
  @typestr
end

#varObject (readonly)

Returns the value of attribute var.



447
448
449
# File 'lib/yaggo/dsl.rb', line 447

def var
  @var
end

Instance Method Details

#checkObject



481
482
483
484
485
486
# File 'lib/yaggo/dsl.rb', line 481

def check
  super

  pref = "Arg #{name}:"
  raise "#{pref} No type specified" if type.nil?
end

#default=(*args) ⇒ Object



465
466
467
# File 'lib/yaggo/dsl.rb', line 465

def default=(*args)
  raise "An arg cannot have a default value (#{args[0]})"
end

#dumpObject



506
507
508
509
# File 'lib/yaggo/dsl.rb', line 506

def dump
  ["\"#{@var}_arg:\"",
   @multiple ? "vec_str(#{@var}_arg)" : "#{@var}_arg"]
end

#hidden=(*args) ⇒ Object



469
470
471
# File 'lib/yaggo/dsl.rb', line 469

def hidden=(*args)
  raise "An arg cannot be marked hidden"
end

#initObject



499
500
501
502
503
504
# File 'lib/yaggo/dsl.rb', line 499

def init
  s = "#{@var}_arg("
  s += default_val(@default, @type) unless @multiple
  s += ")"
  s
end

#offObject



463
# File 'lib/yaggo/dsl.rb', line 463

def off; raise "An arg cannot be a flag with default value off"; end

#onObject



462
# File 'lib/yaggo/dsl.rb', line 462

def on; raise "An arg cannot be a flag with default value on"; end

#parse_argObject



511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/yaggo/dsl.rb', line 511

def parse_arg
  a = []
  off = ""
  if @multiple
    a << "for( ; optind < argc; ++optind) {"
    a << "  #{@var}_arg.push_back(#{str_conv("argv[optind]", @type, @suffix)});"
    off = "  "
  else
    a << "#{@var}_arg = #{str_conv("argv[optind]", @type, @suffix)};"
  end
  unless @type == :string || @type == :c_string
    a << (off + "CHECK_ERR(#{@type}_t, argv[optind], \"#{@var}\")")
  end
  a << (@multiple ? "}" : "++optind;")
  a
end

#required=(*args) ⇒ Object



477
478
479
# File 'lib/yaggo/dsl.rb', line 477

def required=(*args)
  raise "An arg cannot be marked required"
end

#secret=(*args) ⇒ Object



473
474
475
# File 'lib/yaggo/dsl.rb', line 473

def secret=(*args)
  raise "An arg cannot be marked secret"
end

#var_declObject



488
489
490
491
492
493
494
495
496
497
# File 'lib/yaggo/dsl.rb', line 488

def var_decl
  if @multiple
    c_type = "::std::vector<#{$type_to_C_type[@type]}>"
    [c_type.ljust($typejust) + " #{@var}_arg;",
     "typedef #{c_type}::iterator #{@var}_arg_it;",
     "typedef #{c_type}::const_iterator #{@var}_arg_const_it;"]
  else
    ["#{$type_to_C_type[@type]}".ljust($typejust) + " #{@var}_arg;"]
  end
end