Class: RakeCommander::Option

Inherits:
Object
  • Object
show all
Extended by:
Base::ClassHelpers, RakeCommander::Options::Name
Includes:
RakeCommander::Options::Description, RakeCommander::Options::Type
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::BOOLEAN_NAME_REGEX, RakeCommander::Options::Name::BOOLEAN_TOKEN, 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::UNDERSCORE_REGEX, RakeCommander::Options::Name::WORD_DELIMITER

Constants included from RakeCommander::Options::Type

RakeCommander::Options::Type::ALLOWED_TYPES

Constants included from RakeCommander::Options::Description

RakeCommander::Options::Description::DESC_MAX_LENGTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Base::ClassHelpers

class_resolver, descendants, descendants?, new_class, redef_without_warning, resolve_class, sort_classes, to_constant, used_param?

Methods included from RakeCommander::Options::Name

argument_optional?, capture_argument_with!, double_hyphen?, name_argument, name_argument?, name_sym, name_word_sym, short_sym, single_hyphen?, valid_name?, valid_short?

Constructor Details

#initialize(*args, sample: false, **kargs, &block) ⇒ Option

Returns a new instance of Option.

Parameters:

  • sample (Boolean) (defaults to: false)

    allows to skip the short and name validations



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

def initialize(*args, sample: false, **kargs, &block)
  short, name = capture_arguments_short_n_name!(args, kargs, sample: sample)

  @name_full  = name.freeze
  super(short.freeze, @name_full)

  @default        = kargs[:default]  if kargs.key?(:default)
  @desc           = kargs[:desc]     if kargs.key?(:desc)
  @required       = kargs[:required] if kargs.key?(:required)
  @type_coercion  = kargs[:type]     if kargs.key?(:type)
  @other_args     = args
  @original_block = block

  configure_other
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



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

def default
  @default
end

#descObject (readonly)

Returns the value of attribute desc.



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

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

Instance Method Details

#add_switch(opts_parser, where: :base, implicit_short: false, &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

Parameters:

  • opt_parser (OptionParser)

    the option parser to add this option's switch.

  • implicit_short (Boolean) (defaults to: false)

    whether the implicit short of this option is active in the opts_parser.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rake-commander/option.rb', line 128

def add_switch(opts_parser, where: :base, implicit_short: false, &middleware)
  msg = "Expecting OptionParser. Given: #{opts_parser.class}"
  raise msg unless opts_parser.is_a?(OptionParser)

  args  = switch_args(implicit_short: implicit_short)
  block = option_block(&middleware)

  case where
  when :head, :top
    opts_parser.on_head(*args, &block)
  when :tail, :end
    opts_parser.on_tail(*args, &block)
  else # :base
    opts_parser.on(*args, &block)
  end
  opts_parser
end

#argumentObject

Parameters:

  • the (String, Nil)

    argument, may it exist



88
89
90
91
92
# File 'lib/rake-commander/option.rb', line 88

def argument
  return unless argument?

  self.class.name_argument(name_full)
end

#argument?Boolean

Parameters:

  • whether (Boolean)

    this option allows an argument

Returns:

  • (Boolean)


83
84
85
# File 'lib/rake-commander/option.rb', line 83

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)


95
96
97
# File 'lib/rake-commander/option.rb', line 95

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

#boolean_name?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/rake-commander/option.rb', line 78

def boolean_name?
  self.class.boolean_name?(name_full)
end

#default?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/rake-commander/option.rb', line 120

def default?
  instance_variable_defined?(:@default)
end

#dup(**kargs, &block) ⇒ RakeCommander::Option Also known as: deep_dup

Makes a copy of this option



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

def dup(**kargs, &block)
  block ||= original_block
  self.class.new(**dup_key_arguments.merge(kargs), &block)
end

#enum?Boolean

Returns whether the option is an enum with fixed values.

Returns:

  • (Boolean)

    whether the option is an enum with fixed values.



108
109
110
# File 'lib/rake-commander/option.rb', line 108

def enum?
  type_coercion.is_a?(Array)
end

#enum_optionsArray, NilClass

Returns the valid options when is enum?.

Returns:

  • (Array, NilClass)

    the valid options when is enum?



113
114
115
116
117
# File 'lib/rake-commander/option.rb', line 113

def enum_options
  return unless enum?

  type_coercion
end

#merge(opt, **kargs) ⇒ RakeCommander::Option

Creates a new option, result of merging this opt with this option,

Returns:



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

def merge(opt, **kargs)
  msg = "Expecting RakeCommander::Option. Given: #{opt.class}"
  raise msg unless opt.is_a?(RakeCommander::Option)

  dup(**opt.dup_key_arguments.merge(kargs), &opt.original_block)
end

#nameSymbol

Returns:

  • (Symbol)


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

def name
  self.class.name_word_sym(super)
end

#name_hyphenString

Returns:

  • (String)


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

def name_hyphen
  self.class.name_hyphen(name_full)
end

#required?Boolean

Returns whether this option is required.

Returns:

  • (Boolean)

    whether this option is required.



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

def required?
  !!@required
end

#shortSymbol

Returns:

  • (Symbol)


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

def short
  self.class.short_sym(super)
end

#short_hyphenString

Returns:

  • (String)


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

def short_hyphen
  self.class.short_hyphen(short)
end

#short_implicitObject

OptionParser interprets free shorts that match the first letter of an option name as an invocation of that option. This method allows to identify this. return [Symbol]



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

def short_implicit
  self.class.short_sym(@name_full)
end

#type_coercionClass, NilClass

Returns:

  • (Class, NilClass)


100
101
102
103
104
105
# File 'lib/rake-commander/option.rb', line 100

def type_coercion
  value = @type_coercion || (default? && default.class)
  return unless allowed_type?(value)

  value
end