Module: RakeCommander::Options::ClassMethods

Defined in:
lib/rake-commander/options.rb

Instance Method Summary collapse

Instance Method Details

Overrides the auto-generated banner



36
37
38
39
40
41
# File 'lib/rake-commander/options.rb', line 36

def banner(desc = :not_used)
  return @banner = desc unless desc == :not_used
  return @banner        if @banner

  task_options_banner   if respond_to?(:task_options_banner, true)
end

#clear_options!Object

Clears all the options.



146
147
148
149
# File 'lib/rake-commander/options.rb', line 146

def clear_options!
  @options_hash = {}
  self
end

#option(*args, override: true, reopen: false, **kargs, &block) ⇒ Object

Note:
  • If override is true, it will with a Warning when same short or name clashes.

Defines a new option or opens for edition an existing one if reopen: true is used.



46
47
48
49
50
51
# File 'lib/rake-commander/options.rb', line 46

def option(*args, override: true, reopen: false, **kargs, &block) # rubocop:disable Naming/BlockForwarding, Style/ArgumentsForwarding
  return option_reopen(*args, override: override, **kargs, &block) if reopen

  opt = option_class.new(*args, **kargs, &block)
  add_to_options(opt, override: override)
end

#option?(sym) ⇒ Boolean

Returns whether an option has been declared.

Parameters:

  • sym (Symbol)

    the name or short of the option.

Returns:

  • (Boolean)

    whether an option has been declared.



136
137
138
# File 'lib/rake-commander/options.rb', line 136

def option?(sym)
  options_hash.key?(sym.to_sym)
end

#option_get(sym) ⇒ RakeCommander::Option, NilClass

Returns retrieves the option.

Parameters:

  • sym (Symbol)

    the name or short of the option.

Returns:



130
131
132
# File 'lib/rake-commander/options.rb', line 130

def option_get(sym)
  options_hash[sym.to_sym]
end

#option_remove(*keys) ⇒ Object

Removes options with short or name keys from options



75
76
77
78
79
80
81
# File 'lib/rake-commander/options.rb', line 75

def option_remove(*keys)
  keys.map do |key|
    aux = option_class.new(key, sample: true)
    opt = options_hash.values_at(aux.name, aux.short).compact.first
    delete_from_options(opt) if opt
  end
end

#option_reopen(*args, override: false, **kargs, &block) ⇒ Object

Note:

To allow reopen using the name without modifying the argument, use a Symbol Example: option_reopen :opt_with_arg will keep the argument of 'opt_with_arg'

Note:
  1. If override is false, it will fail to change thenameor theshort` when they are already taken by some other option.
  2. It will have the effect of overriding existing options
Note:

when short and name are provided, name takes precedence over short in the lookup (to identify the existing option)

It re-opens an option for edition. If it does not exist, it upserts it.



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rake-commander/options.rb', line 62

def option_reopen(*args, override: false, **kargs, &block) # rubocop:disable Naming/BlockForwarding, Style/ArgumentsForwarding
  aux = option_class.new(*args, **kargs, sample: true, &block) # rubocop:disable Naming/BlockForwarding, Style/ArgumentsForwarding
  opt = options_hash.values_at(aux.name, aux.short).compact.first
  return option(*args, **kargs, &block) unless opt

  mod = {}.tap do |mkargs|
    mkargs.merge!(name: opt.name_full) if aux.name_full.is_a?(Symbol)
  end

  replace_in_options(opt, opt.merge(aux, **mod), override: override)
end

#optionsArray<RakeCommander::Option>

List of configured options

Returns:



124
125
126
# File 'lib/rake-commander/options.rb', line 124

def options
  options_hash.values.uniq
end

#options?Boolean

Returns are there options defined?.

Returns:

  • (Boolean)

    are there options defined?



141
142
143
# File 'lib/rake-commander/options.rb', line 141

def options?
  !options.empty?
end

#options_hash(with_implicit: false) ⇒ Hash

The options indexed by the short and the name (so doubled up in the hash).

Parameters:

  • with_implicit (Boolean) (defaults to: false)

    whether free implicit shorts of declared options should be included among the keys (pointing to the specific option that has it implicit).

Returns:

  • (Hash)

    with Symbol name and shorts as keys, and RakeCommander::Option as values.



115
116
117
118
119
120
# File 'lib/rake-commander/options.rb', line 115

def options_hash(with_implicit: false)
  @options_hash ||= {}
  return @options_hash unless with_implicit

  @options_hash.merge(implicit_shorts)
end

#options_use(opts, override: true) ⇒ Object

Note:

it does a deep dup of each option.

Allows to use a set of options

Parameters:

  • override (Boolean) (defaults to: true)

    wheter existing options with same option name should be overriden, may they clash

  • options (Enumerable<RakeCommander::Option>)


88
89
90
91
92
93
94
95
96
97
# File 'lib/rake-commander/options.rb', line 88

def options_use(opts, override: true)
  msg = "Could not obtain list of RakeCommander::Option from #{opts.class}"
  raise msg unless (opts = to_options(opts))

  opts.each do |opt|
    add_to_options(opt.deep_dup, override: override)
  end

  self
end

#parse_options(argv = ARGV, method: :parse, &middleware) ⇒ Array<String>

Note:

this method is extended in via the following modules:

  1. RakeCommander::Options::Result: makes the method to return a Hash with results, as well as captures/moves the leftovers to their own keyed argument.
  2. RakeCommander::Options:Error: adds error handling (i.e. forward to rake commander errors)

It builds the OptionParser injecting the middleware block and parses argv

Parameters:

  • argv (Array<String>) (defaults to: ARGV)

    the command line arguments to be parsed.

  • method (Symbol) (defaults to: :parse)

    the parsing method (default is :parse; others: :order)

Returns:

  • (Array<String>)

    the leftovers of the OptionParser#parse call.



107
108
109
# File 'lib/rake-commander/options.rb', line 107

def parse_options(argv = ARGV, method: :parse, &middleware)
  options_parser(&middleware).send(method, argv)
end