Class: Baf::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/baf/option.rb

Direct Known Subclasses

Flag

Constant Summary collapse

LONG_PREFIX =
'--'.freeze
LONG_NORMALIZE_SEARCH =
?_.freeze
LONG_NORMALIZE_REPLACE =
?-.freeze
LONG_WITH_ARG_GLUE =
' '.freeze
PARSER_MESSAGE =
:on
PARSER_MESSAGE_TAIL =
:on_tail

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, tail: false) ⇒ Option

Returns a new instance of Option.



12
13
14
15
16
# File 'lib/baf/option.rb', line 12

def initialize *args, tail: false
  attrs = args.size > 1 ? build_attrs(*args) : args[0]
  attrs.each { |k, v| send :"#{k}=", v }
  self.tail = tail
end

Instance Attribute Details

#argObject

Returns the value of attribute arg.



10
11
12
# File 'lib/baf/option.rb', line 10

def arg
  @arg
end

#blockObject

Returns the value of attribute block.



10
11
12
# File 'lib/baf/option.rb', line 10

def block
  @block
end

#descObject

Returns the value of attribute desc.



10
11
12
# File 'lib/baf/option.rb', line 10

def desc
  @desc
end

#longObject

Returns the value of attribute long.



10
11
12
# File 'lib/baf/option.rb', line 10

def long
  @long
end

#shortObject

Returns the value of attribute short.



10
11
12
# File 'lib/baf/option.rb', line 10

def short
  @short
end

#tailObject

Returns the value of attribute tail.



10
11
12
# File 'lib/baf/option.rb', line 10

def tail
  @tail
end

Instance Method Details

#block?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/baf/option.rb', line 22

def block?
  !!block
end

#env_definitionObject



18
19
20
# File 'lib/baf/option.rb', line 18

def env_definition
  :accessor
end

#tail?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/baf/option.rb', line 26

def tail?
  !!tail
end

#to_parser_arguments(env) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/baf/option.rb', line 30

def to_parser_arguments env
  message = tail? ? PARSER_MESSAGE_TAIL : PARSER_MESSAGE
  mblock = if block?
    -> *args { block[*args, env] }
  else
    parser_argument_block env
  end
  [message, "-#{short}", parser_argument_long, parser_argument_desc, mblock]
end