Class: Homebrew::CLI::Args

Inherits:
OpenStruct
  • Object
show all
Extended by:
T::Sig
Defined in:
cli/args.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializevoid



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'cli/args.rb', line 17

def initialize
  require "cli/named_args"

  super()

  @processed_options = []
  @options_only = []
  @flags_only = []
  @cask_options = false

  # Can set these because they will be overwritten by freeze_named_args!
  # (whereas other values below will only be overwritten if passed).
  self[:named] = NamedArgs.new(parent: self)
  self[:remaining] = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object (private)



141
142
143
144
145
146
147
148
149
150
151
# File 'cli/args.rb', line 141

def method_missing(method_name, *args)
  return_value = super

  # Once we are frozen, verify any arg method calls are already defined in the table.
  # The default OpenStruct behaviour is to return nil for anything unknown.
  if frozen? && args.empty? && !@table.key?(method_name)
    raise NoMethodError, "CLI arg for `#{method_name}` is not declared for this command"
  end

  return_value
end

Instance Attribute Details

#flags_onlyObject (readonly)

Returns the value of attribute flags_only.



11
12
13
# File 'cli/args.rb', line 11

def flags_only
  @flags_only
end

#options_onlyObject (readonly)

Returns the value of attribute options_only.



11
12
13
# File 'cli/args.rb', line 11

def options_only
  @options_only
end

Instance Method Details

#build_from_source_formulaeObject



69
70
71
72
73
74
75
# File 'cli/args.rb', line 69

def build_from_source_formulae
  if build_from_source? || self[:HEAD?] || self[:build_bottle?]
    named.to_formulae_and_casks.select { |f| f.is_a?(Formula) }.map(&:full_name)
  else
    []
  end
end

#contextContext::ContextStruct



94
95
96
# File 'cli/args.rb', line 94

def context
  Context::ContextStruct.new(debug: debug?, quiet: quiet?, verbose: verbose?)
end

#freeze_named_args!(named_args, cask_options:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'cli/args.rb', line 37

def freeze_named_args!(named_args, cask_options:)
  self[:named] = NamedArgs.new(
    *named_args.freeze,
    override_spec: spec(nil),
    force_bottle:  self[:force_bottle?],
    flags:         flags_only,
    cask_options:  cask_options,
    parent:        self,
  )
end

#freeze_processed_options!(processed_options) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'cli/args.rb', line 48

def freeze_processed_options!(processed_options)
  # Reset cache values reliant on processed_options
  @cli_args = nil

  @processed_options += processed_options
  @processed_options.freeze

  @options_only = cli_args.select { |a| a.start_with?("-") }.freeze
  @flags_only = cli_args.select { |a| a.start_with?("--") }.freeze
end

#freeze_remaining_args!(remaining_args) ⇒ Object



33
34
35
# File 'cli/args.rb', line 33

def freeze_remaining_args!(remaining_args)
  self[:remaining] = remaining_args.freeze
end

#include_test_formulaeObject



77
78
79
80
81
82
83
# File 'cli/args.rb', line 77

def include_test_formulae
  if include_test?
    named.to_formulae.map(&:full_name)
  else
    []
  end
end

#namedNamedArgs

Returns:



60
61
62
63
# File 'cli/args.rb', line 60

def named
  require "formula"
  self[:named]
end

#no_named?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'cli/args.rb', line 65

def no_named?
  named.blank?
end

#only_formula_or_caskObject



98
99
100
101
# File 'cli/args.rb', line 98

def only_formula_or_cask
  return :formula if formula? && !cask?
  return :cask if cask? && !formula?
end

#value(name) ⇒ Object



85
86
87
88
89
90
91
# File 'cli/args.rb', line 85

def value(name)
  arg_prefix = "--#{name}="
  flag_with_value = flags_only.find { |arg| arg.start_with?(arg_prefix) }
  return unless flag_with_value

  flag_with_value.delete_prefix(arg_prefix)
end