Module: RakeCommander::Options::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

Overrides the auto-generated banner



54
55
56
57
58
# File 'lib/rake-commander/options.rb', line 54

def banner(desc = :not_used)
  return @banner = desc unless desc == :not_used
  return @banner if @banner
  return task_options_banner if respond_to?(:task_options_banner, true)
end

#clear_options!Object



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

def clear_options!
  @options_hash = {}
end

#option(*args, **kargs, &block) ⇒ Object

Note:
  • It will override with a Warning options with same short or name

Defines a new option



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

def option(*args, **kargs, &block)
  opt = RakeCommander::Option.new(*args, **kargs, &block)
  add_to_options(opt)
  self
end

#optionsObject



22
23
24
# File 'lib/rake-commander/options.rb', line 22

def options
  options_hash.values.uniq
end

#options?Boolean

Returns:

  • (Boolean)


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

def options?
  !options.empty?
end

#options_hashObject



18
19
20
# File 'lib/rake-commander/options.rb', line 18

def options_hash
  @options_hash ||= {}
end

#parse_options(argv = ARGV, leftovers: [], &middleware) ⇒ Hash

It builds the OptionParser injecting the middleware block.

Returns:

  • (Hash)

    with short option as key and final value as value.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rake-commander/options.rb', line 72

def parse_options(argv = ARGV, leftovers: [], &middleware)
  options_parser_with_results(middleware) do |options_parser|
    argv = pre_parse_arguments(argv, options: options_hash)
    pp argv
    leftovers.push(*options_parser.parse(argv))
  rescue OptionParser::MissingArgument => e
    raise RakeCommander::Options::MissingArgument, e, cause: nil
  rescue OptionParser::InvalidArgument => e
    error = RakeCommander::Options::InvalidArgument
    error = error.new(e)
    opt   = options_hash[error.option_sym]
    msg = "missing required argument: #{opt&.name_hyphen} (#{opt&.short_hyphen})"
    raise RakeCommander::Options::MissingArgument, msg, cause: nil if opt&.argument_required?
    raise error, e, cause: nil
  end.tap do |results|
    check_required_presence!(results)
  end
end

#results_with_all_defaults(value = nil) ⇒ Boolean

Returns whether results should include options defined with a default, regarless if they are invoked.

Returns:

  • (Boolean)

    whether results should include options defined with a default, regarless if they are invoked



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

def results_with_all_defaults(value = nil)
  if value.nil?
    @results_with_all_defaults || false
  else
    @results_with_all_defaults = !!value
  end
end

#use_options(options) ⇒ Object

Allows to use a set of options

Parameters:



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

def use_options(options)
  options = options.values if options.is_a?(Hash)
  options.each do |opt|
    add_to_options(opt)
  end
  self
end