Class: Homebrew::CLI::Parser

Inherits:
Object
  • Object
show all
Defined in:
brew/Library/Homebrew/cli/parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Parser

Returns a new instance of Parser



28
29
30
31
32
33
34
35
36
37
38
# File 'brew/Library/Homebrew/cli/parser.rb', line 28

def initialize(&block)
  @parser = OptionParser.new
  @args = Homebrew::CLI::Args.new(argv: ARGV_WITHOUT_MONKEY_PATCHING)
  @constraints = []
  @conflicts = []
  @switch_sources = {}
  @processed_options = []
  @hide_from_man_page = false
  instance_eval(&block)
  post_initialize
end

Instance Attribute Details

#hide_from_man_pageObject (readonly)

Returns the value of attribute hide_from_man_page



13
14
15
# File 'brew/Library/Homebrew/cli/parser.rb', line 13

def hide_from_man_page
  @hide_from_man_page
end

#processed_optionsObject (readonly)

Returns the value of attribute processed_options



13
14
15
# File 'brew/Library/Homebrew/cli/parser.rb', line 13

def processed_options
  @processed_options
end

Class Method Details

.global_optionsObject



19
20
21
22
23
24
25
26
# File 'brew/Library/Homebrew/cli/parser.rb', line 19

def self.global_options
  {
    quiet:   [["-q", "--quiet"], :quiet, "Suppress any warnings."],
    verbose: [["-v", "--verbose"], :verbose, "Make some output more verbose."],
    debug:   [["-d", "--debug"], :debug, "Display any debugging information."],
    force:   [["-f", "--force"], :force, "Override warnings and enable potentially unsafe operations."],
  }
end

.parse(args = ARGV, &block) ⇒ Object



15
16
17
# File 'brew/Library/Homebrew/cli/parser.rb', line 15

def self.parse(args = ARGV, &block)
  new(&block).parse(args)
end

Instance Method Details

#comma_array(name, description: nil) ⇒ Object



76
77
78
79
80
81
82
# File 'brew/Library/Homebrew/cli/parser.rb', line 76

def comma_array(name, description: nil)
  description = option_to_description(name) if description.nil?
  process_option(name, description)
  @parser.on(name, OptionParser::REQUIRED_ARGUMENT, Array, *wrap_option_desc(description)) do |list|
    @args[option_to_name(name)] = list
  end
end

#conflicts(*options) ⇒ Object



104
105
106
# File 'brew/Library/Homebrew/cli/parser.rb', line 104

def conflicts(*options)
  @conflicts << options.map { |option| option_to_name(option) }
end

#flag(*names, description: nil, required_for: nil, depends_on: nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'brew/Library/Homebrew/cli/parser.rb', line 84

def flag(*names, description: nil, required_for: nil, depends_on: nil)
  if names.any? { |name| name.end_with? "=" }
    required = OptionParser::REQUIRED_ARGUMENT
  else
    required = OptionParser::OPTIONAL_ARGUMENT
  end
  names.map! { |name| name.chomp "=" }
  description = option_to_description(*names) if description.nil?
  process_option(*names, description)
  @parser.on(*names, *wrap_option_desc(description), required) do |option_value|
    names.each do |name|
      @args[option_to_name(name)] = option_value
    end
  end

  names.each do |name|
    set_constraints(name, required_for: required_for, depends_on: depends_on)
  end
end

#formula_optionsObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'brew/Library/Homebrew/cli/parser.rb', line 159

def formula_options
  ARGV.formulae.each do |f|
    next if f.options.empty?

    f.options.each do |o|
      name = o.flag
      description = "`#{f.name}`: #{o.description}"
      if name.end_with? "="
        flag   name, description: description
      else
        switch name, description: description
      end
    end
  end
rescue FormulaUnavailableError
  []
end

#generate_help_textObject



151
152
153
154
155
156
157
# File 'brew/Library/Homebrew/cli/parser.rb', line 151

def generate_help_text
  @parser.to_s.sub(/^/, "#{Tty.bold}Usage: brew#{Tty.reset} ")
         .gsub(/`(.*?)`/m, "#{Tty.bold}\\1#{Tty.reset}")
         .gsub(%r{<([^\s]+?://[^\s]+?)>}) { |url| Formatter.url(url) }
         .gsub(/<(.*?)>/m, "#{Tty.underline}\\1#{Tty.reset}")
         .gsub(/\*(.*?)\*/m, "#{Tty.underline}\\1#{Tty.reset}")
end

#global_option?(name) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
# File 'brew/Library/Homebrew/cli/parser.rb', line 147

def global_option?(name)
  Homebrew::CLI::Parser.global_options.key?(name.to_sym)
end

#hide_from_man_page!Object



177
178
179
# File 'brew/Library/Homebrew/cli/parser.rb', line 177

def hide_from_man_page!
  @hide_from_man_page = true
end

#name_to_option(name) ⇒ Object



114
115
116
117
118
119
120
# File 'brew/Library/Homebrew/cli/parser.rb', line 114

def name_to_option(name)
  if name.length == 1
    "-#{name}"
  else
    "--#{name.tr("_", "-")}"
  end
end

#option_to_description(*names) ⇒ Object



122
123
124
# File 'brew/Library/Homebrew/cli/parser.rb', line 122

def option_to_description(*names)
  names.map { |name| name.to_s.sub(/\A--?/, "").tr("-", " ") }.max
end

#option_to_name(option) ⇒ Object



108
109
110
111
112
# File 'brew/Library/Homebrew/cli/parser.rb', line 108

def option_to_name(option)
  option.sub(/\A--?/, "")
        .tr("-", "_")
        .delete("=")
end

#parse(cmdline_args = ARGV) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'brew/Library/Homebrew/cli/parser.rb', line 130

def parse(cmdline_args = ARGV)
  raise "Arguments were already parsed!" if @args_parsed

  begin
    remaining_args = @parser.parse(cmdline_args)
  rescue OptionParser::InvalidOption => e
    $stderr.puts generate_help_text
    raise e
  end
  check_constraint_violations
  @args[:remaining] = remaining_args
  @args_parsed = true
  Homebrew.args = @args
  cmdline_args.freeze
  @parser
end

#post_initializeObject



40
41
42
43
44
45
# File 'brew/Library/Homebrew/cli/parser.rb', line 40

def post_initialize
  @parser.on_tail("-h", "--help", "Show this message.") do
    puts generate_help_text
    exit 0
  end
end

#summaryObject



126
127
128
# File 'brew/Library/Homebrew/cli/parser.rb', line 126

def summary
  @parser.to_s
end

#switch(*names, description: nil, env: nil, required_for: nil, depends_on: nil) ⇒ Object Also known as: switch_option



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'brew/Library/Homebrew/cli/parser.rb', line 47

def switch(*names, description: nil, env: nil, required_for: nil, depends_on: nil)
  global_switch = names.first.is_a?(Symbol)
  names, env, default_description = common_switch(*names) if global_switch
  if description.nil? && global_switch
    description = default_description
  elsif description.nil?
    description = option_to_description(*names)
  end
  process_option(*names, description)
  @parser.on(*names, *wrap_option_desc(description)) do
    enable_switch(*names, from: :args)
  end

  names.each do |name|
    set_constraints(name, required_for: required_for, depends_on: depends_on)
  end

  enable_switch(*names, from: :env) if !env.nil? && !ENV["HOMEBREW_#{env.to_s.upcase}"].nil?
end

#usage_banner(text) ⇒ Object



68
69
70
# File 'brew/Library/Homebrew/cli/parser.rb', line 68

def usage_banner(text)
  @parser.banner = Formatter.wrap("#{text}\n", COMMAND_DESC_WIDTH)
end

#usage_banner_textObject



72
73
74
# File 'brew/Library/Homebrew/cli/parser.rb', line 72

def usage_banner_text
  @parser.banner
end