Class: RakeCommander::Option

Inherits:
Object
  • Object
show all
Extended by:
Base::ClassHelpers, RakeCommander::Options::Name
Defined in:
lib/rake-commander/option.rb

Constant Summary

Constants included from Base::ClassHelpers

Base::ClassHelpers::NOT_USED

Constants included from RakeCommander::Options::Name

RakeCommander::Options::Name::DOUBLE_HYPHEN_REGEX, RakeCommander::Options::Name::HYPEN_REGEX, RakeCommander::Options::Name::HYPHEN_START_REGEX, RakeCommander::Options::Name::OPTIONAL_REGEX, RakeCommander::Options::Name::SINGLE_HYPHEN_REGEX, RakeCommander::Options::Name::SPACE_REGEX, RakeCommander::Options::Name::UNDERSCORE_REGEX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Base::ClassHelpers

class_resolver, descendants, descendants?, inheritable_attrs, inheritable_class_vars, inherited, instance_variable_name, new_class, redef_without_warning, resolve_class, to_constant, used_param?

Methods included from RakeCommander::Options::Name

argument_optional?, double_hyphen?, name_argument, name_argument?, name_hyphen?, name_sym, name_word_sym, short_hyphen?, short_sym, single_hyphen?, valid_name?, valid_short?

Constructor Details

#initialize(short, name, *args, **kargs) {|_self| ... } ⇒ Option

Returns a new instance of Option.

Yields:

  • (_self)

Yield Parameters:

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rake-commander/option.rb', line 11

def initialize(short, name, *args, **kargs, &block)
  raise ArgumentError, "A short of one letter should be provided. Given: #{short}" unless self.class.valid_short?(short)
  raise ArgumentError, "A name should be provided. Given: #{name}" unless  self.class.valid_name?(name)

  @name_full = name
  super(short, name)
  @default        = kargs[:default]  if kargs.key?(:default)
  @desc           = kargs[:desc]     if kargs.key?(:desc)
  @required       = kargs[:required] if kargs.key?(:required)
  @other_args     = args
  @original_block = block
  yield(self) if block_given?
  configure_other
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



7
8
9
# File 'lib/rake-commander/option.rb', line 7

def default
  @default
end

#descObject

Returns the value of attribute desc.



7
8
9
# File 'lib/rake-commander/option.rb', line 7

def desc
  @desc
end

#name_fullObject (readonly)

Returns the value of attribute name_full.



9
10
11
# File 'lib/rake-commander/option.rb', line 9

def name_full
  @name_full
end

#required=(value) ⇒ Object (writeonly)

Sets the attribute required

Parameters:

  • value

    the value to set the attribute required to.



8
9
10
# File 'lib/rake-commander/option.rb', line 8

def required=(value)
  @required = value
end

#type_coertionClass, NilClass

Returns:

  • (Class, NilClass)


67
68
69
# File 'lib/rake-commander/option.rb', line 67

def type_coertion
  @type_coertion || (default? && default.class)
end

Instance Method Details

#add_switch(opts_parser, where: :base, &middleware) ⇒ Object

Note:

it allows to add a middleware block that will be called at parse runtime

Adds this option's switch to the OptionParser



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rake-commander/option.rb', line 78

def add_switch(opts_parser, where: :base, &middleware)
  raise "Expecting OptionParser. Given: #{opts_parser.class}" unless opts_parser.is_a?(OptionParser)
  case where
  when :head, :top
    opts_parser.on_head(*switch_args, &option_block(&middleware))
  when :tail, :end
    opts_parser.on_tail(*switch_args, &option_block(&middleware))
  else # :base

    opts_parser.on(*switch_args, &option_block(&middleware))
  end
  opts_parser
end

#argumentObject

Parameters:

  • the (String, Nil)

    argument, may it exist



56
57
58
59
# File 'lib/rake-commander/option.rb', line 56

def argument
  return nil unless argument?
  self.class.name_argument(name_full)
end

#argument?Boolean

Parameters:

  • whether (Boolean)

    this option allows an argument

Returns:

  • (Boolean)


51
52
53
# File 'lib/rake-commander/option.rb', line 51

def argument?
  self.class.name_argument?(name_full)
end

#argument_required?Boolean

Parameters:

  • If (Boolean)

    there was an argument, whether it is required

Returns:

  • (Boolean)


62
63
64
# File 'lib/rake-commander/option.rb', line 62

def argument_required?
  self.class.argument_required?(argument)
end

#default?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/rake-commander/option.rb', line 72

def default?
  instance_variable_defined?(:@default)
end

#nameSymbol

Returns:

  • (Symbol)


41
42
43
# File 'lib/rake-commander/option.rb', line 41

def name
  self.class.name_word_sym(super)
end

#name_hyphenString

Returns:

  • (String)


46
47
48
# File 'lib/rake-commander/option.rb', line 46

def name_hyphen
  self.class.name_hyphen(name_full)
end

#required?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/rake-commander/option.rb', line 26

def required?
  !!@required
end

#shortSymbol

Returns:

  • (Symbol)


31
32
33
# File 'lib/rake-commander/option.rb', line 31

def short
  self.class.short_sym(super)
end

#short_hyphenString

Returns:

  • (String)


36
37
38
# File 'lib/rake-commander/option.rb', line 36

def short_hyphen
  self.class.short_hyphen(short)
end

#switch_argsArray<Variant>

Returns:

  • (Array<Variant>)


92
93
94
95
96
97
98
99
100
# File 'lib/rake-commander/option.rb', line 92

def switch_args
  configure_other
  args = [short_hyphen, name_hyphen]
  if str = switch_desc
    args << str
  end
  args << type_coertion if type_coertion
  args
end