Class: RakeCommander::Option

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



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

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.



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

def default
  @default
end

#descObject (readonly)

Returns the value of attribute desc.



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

def desc
  @desc
end

#name_fullObject (readonly)

Returns the value of attribute name_full.



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

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.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rake-commander/option.rb', line 109

def add_switch(opts_parser, where: :base, implicit_short: false, &middleware)
  raise "Expecting OptionParser. Given: #{opts_parser.class}" 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



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

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)


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

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)


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

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

#boolean_name?Boolean

Returns:

  • (Boolean)


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

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

#default?Boolean

Returns:

  • (Boolean)


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

def default?
  instance_variable_defined?(:@default)
end

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

Makes a copy of this option



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

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

#merge(opt) ⇒ RakeCommander::Option

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

Returns:



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

def merge(opt)
  raise "Expecting RakeCommander::Option. Given: #{opt.class}" unless opt.is_a?(RakeCommander::Option)
  dup(**opt.dup_key_arguments, &opt.original_block)
end

#nameSymbol

Returns:

  • (Symbol)


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

def name
  self.class.name_word_sym(super)
end

#name_hyphenString

Returns:

  • (String)


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

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.



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

def required?
  !!@required
end

#shortSymbol

Returns:

  • (Symbol)


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

def short
  self.class.short_sym(super)
end

#short_hyphenString

Returns:

  • (String)


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

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]



53
54
55
# File 'lib/rake-commander/option.rb', line 53

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

#type_coercionClass, NilClass

Returns:

  • (Class, NilClass)


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

def type_coercion
  value = @type_coercion || (default? && default.class)
  return nil unless value.is_a?(Class)
  value
end