Class: Homebrew::CLI::Parser Private

Inherits:
Object
  • Object
show all
Includes:
Utils::Output::Mixin
Defined in:
cli/parser.rb

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Defined Under Namespace

Classes: Subcommand

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Output::Mixin

#issue_reporting_message, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #opoo_outside_github_actions, #opoo_without_github_actions_annotation, #pretty_deprecated, #pretty_disabled, #pretty_duration, #pretty_install_status, #pretty_installed, #pretty_outdated, #pretty_uninstalled, #pretty_upgradable

Constructor Details

#initialize(cmd, &block) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'cli/parser.rb', line 170

def initialize(cmd, &block)
  @parser = T.let(OptionParser.new, OptionParser)
  @parser.summary_indent = "  "
  # Disable default handling of `--version` switch.
  @parser.base.long.delete("version")
  # Disable default handling of `--help` switch.
  @parser.base.long.delete("help")

  @args = T.let((cmd.args_class || Args).new, Args)

  @command_name = T.let(cmd.command_name, String)
  @is_dev_cmd = T.let(cmd.dev_cmd?, T::Boolean)

  @constraints = T.let([], T::Array[[String, String, T.nilable(T::Array[String])]])
  @conflicts = T.let([], T::Array[T::Array[String]])
  @switch_sources = T.let({}, T::Hash[String, Symbol])
  @option_sources = T.let({}, T::Hash[String, Symbol])
  @option_subcommands = T.let({}, T::Hash[String, T::Array[String]])
  @option_types = T.let({}, T::Hash[String, Symbol])
  @processed_options = T.let([], Args::OptionsType)
  @processed_option_summaries = T.let([], T::Array[[T.untyped, T::Boolean]])
  @processed_options_by_subcommand = T.let({}, T::Hash[T.nilable(String), Args::OptionsType])
  @processed_option_summaries_by_subcommand =
    T.let({}, T::Hash[T.nilable(String), T::Array[[T.untyped, T::Boolean]]])
  @non_global_processed_options = T.let([], T::Array[[String, ArgType]])
  @named_args_type = T.let(nil, T.nilable(ArgType))
  @max_named_args = T.let(nil, T.nilable(Integer))
  @min_named_args = T.let(nil, T.nilable(Integer))
  @named_args_without_api = T.let(false, T::Boolean)
  @description = T.let(nil, T.nilable(String))
  @usage_banner = T.let(nil, T.nilable(String))
  @hide_from_man_page = T.let(false, T::Boolean)
  @formula_options = T.let(false, T::Boolean)
  @cask_options = T.let(false, T::Boolean)
  @subcommands = T.let([], T::Array[Subcommand])
  @current_subcommands = T.let(nil, T.nilable(T::Array[String]))

  self.class.global_options.each do |short, long, desc|
    switch short, long, description: desc, env: option_to_name(long), method: :on_tail
  end

  instance_eval(&block) if block

  generate_banner
end

Instance Attribute Details

#argsArgs (readonly)

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



44
45
46
# File 'cli/parser.rb', line 44

def args
  @args
end

#hide_from_man_pageBoolean (readonly)

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


50
51
52
# File 'cli/parser.rb', line 50

def hide_from_man_page
  @hide_from_man_page
end

#processed_optionsArgs::OptionsType (readonly)

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



47
48
49
# File 'cli/parser.rb', line 47

def processed_options
  @processed_options
end

#subcommandsArray<Subcommand> (readonly)

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



53
54
55
# File 'cli/parser.rb', line 53

def subcommands
  @subcommands
end

Class Method Details

.from_cmd_path(cmd_path) ⇒ CLI::Parser?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'cli/parser.rb', line 56

def self.from_cmd_path(cmd_path)
  cmd_args_method_name = Commands.args_method_name(cmd_path)
  cmd_name = cmd_args_method_name.to_s.delete_suffix("_args").tr("_", "-")

  begin
    if ENV.clear_sensitive_environment! { Homebrew.require?(cmd_path) }
      cmd = Homebrew::AbstractCommand.command(cmd_name)
      if cmd
        cmd.parser
      else
        # FIXME: remove once commands are all subclasses of `AbstractCommand`:
        Homebrew.send(cmd_args_method_name)
      end
    end
  rescue NoMethodError => e
    raise if e.name.to_sym != cmd_args_method_name

    nil
  end
end

.global_cask_optionsArray<Array<(Symbol, String, Hash)>>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'cli/parser.rb', line 78

def self.global_cask_options
  [
    [:flag, "--appdir=", {
      description: "Target location for Applications " \
                   "(default: `#{Cask::Config::DEFAULT_DIRS[:appdir]}`).",
    }],
    [:flag, "--appimagedir=", {
      description: "Target location for AppImages " \
                   "(default: `#{Cask::Config::DEFAULT_DIRS[:appimagedir]}`).",
    }],
    [:flag, "--keyboard-layoutdir=", {
      description: "Target location for Keyboard Layouts " \
                   "(default: `#{Cask::Config::DEFAULT_DIRS[:keyboard_layoutdir]}`).",
    }],
    [:flag, "--colorpickerdir=", {
      description: "Target location for Color Pickers " \
                   "(default: `#{Cask::Config::DEFAULT_DIRS[:colorpickerdir]}`).",
    }],
    [:flag, "--prefpanedir=", {
      description: "Target location for Preference Panes " \
                   "(default: `#{Cask::Config::DEFAULT_DIRS[:prefpanedir]}`).",
    }],
    [:flag, "--qlplugindir=", {
      description: "Target location for Quick Look Plugins " \
                   "(default: `#{Cask::Config::DEFAULT_DIRS[:qlplugindir]}`).",
    }],
    [:flag, "--mdimporterdir=", {
      description: "Target location for Spotlight Plugins " \
                   "(default: `#{Cask::Config::DEFAULT_DIRS[:mdimporterdir]}`).",
    }],
    [:flag, "--dictionarydir=", {
      description: "Target location for Dictionaries " \
                   "(default: `#{Cask::Config::DEFAULT_DIRS[:dictionarydir]}`).",
    }],
    [:flag, "--fontdir=", {
      description: "Target location for Fonts " \
                   "(default: `#{Cask::Config::DEFAULT_DIRS[:fontdir]}`).",
    }],
    [:flag, "--servicedir=", {
      description: "Target location for Services " \
                   "(default: `#{Cask::Config::DEFAULT_DIRS[:servicedir]}`).",
    }],
    [:flag, "--input-methoddir=", {
      description: "Target location for Input Methods " \
                   "(default: `#{Cask::Config::DEFAULT_DIRS[:input_methoddir]}`).",
    }],
    [:flag, "--internet-plugindir=", {
      description: "Target location for Internet Plugins " \
                   "(default: `#{Cask::Config::DEFAULT_DIRS[:internet_plugindir]}`).",
    }],
    [:flag, "--audio-unit-plugindir=", {
      description: "Target location for Audio Unit Plugins " \
                   "(default: `#{Cask::Config::DEFAULT_DIRS[:audio_unit_plugindir]}`).",
    }],
    [:flag, "--vst-plugindir=", {
      description: "Target location for VST Plugins " \
                   "(default: `#{Cask::Config::DEFAULT_DIRS[:vst_plugindir]}`).",
    }],
    [:flag, "--vst3-plugindir=", {
      description: "Target location for VST3 Plugins " \
                   "(default: `#{Cask::Config::DEFAULT_DIRS[:vst3_plugindir]}`).",
    }],
    [:flag, "--screen-saverdir=", {
      description: "Target location for Screen Savers " \
                   "(default: `#{Cask::Config::DEFAULT_DIRS[:screen_saverdir]}`).",
    }],
    [:comma_array, "--language", {
      description: "Comma-separated list of language codes to prefer for cask installation. " \
                   "The first matching language is used, otherwise it reverts to the cask's " \
                   "default language. The default value is the language of your system.",
    }],
  ]
end

.global_optionsArray<Array<(String, String, String)>>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



153
154
155
156
157
158
159
160
# File 'cli/parser.rb', line 153

def self.global_options
  [
    ["-d", "--debug",   "Display any debugging information."],
    ["-q", "--quiet",   "Make some output more quiet."],
    ["-v", "--verbose", "Make some output more verbose."],
    ["-h", "--help",    "Show this message."],
  ]
end

.option_to_name(option) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



163
164
165
# File 'cli/parser.rb', line 163

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

Instance Method Details

#cask_optionsvoid

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



593
594
595
596
597
598
599
600
# File 'cli/parser.rb', line 593

def cask_options
  self.class.global_cask_options.each do |args|
    options = T.cast(args.pop, T::Hash[Symbol, String])
    send(*args, **options)
    conflicts "--formula", args[1]
  end
  @cask_options = true
end

#comma_array(name, description: nil, hidden: false, subcommands: nil) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • name (String)
  • description (String, nil) (defaults to: nil)
  • hidden (Boolean) (defaults to: false)
  • subcommands (String, Array<String>, nil) (defaults to: nil)


329
330
331
332
333
334
335
336
337
338
339
# File 'cli/parser.rb', line 329

def comma_array(name, description: nil, hidden: false, subcommands: nil)
  name = name.chomp "="
  description = option_description(description, name, hidden:)
  process_option(name, description, type: :comma_array, hidden:, subcommands:)
  @parser.on(name, OptionParser::REQUIRED_ARGUMENT, Array, *wrap_option_desc(description)) do |list|
    option_name = option_to_name(name)
    @option_sources[option_name] = :args
    @option_types[option_name] ||= :comma_array
    set_args_method(option_name.to_sym, list)
  end
end

#conflicts(*options) ⇒ Array<Array<String>>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



387
388
389
390
391
# File 'cli/parser.rb', line 387

def conflicts(*options)
  return @conflicts if options.empty?

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

#default_subcommandSubcommand?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



692
# File 'cli/parser.rb', line 692

def default_subcommand = @subcommands.find(&:default)

#description(text = nil) ⇒ String?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • text (String, nil) (defaults to: nil)

Returns:



279
280
281
282
283
# File 'cli/parser.rb', line 279

def description(text = nil)
  return @description if text.blank?

  @description = text.chomp
end

#flag(*names, description: nil, replacement: nil, depends_on: nil, hidden: false, subcommands: nil) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • names (String)
  • description (String, nil) (defaults to: nil)
  • replacement (Symbol, String, nil) (defaults to: nil)
  • depends_on (String, nil) (defaults to: nil)
  • hidden (Boolean) (defaults to: false)
  • subcommands (String, Array<String>, nil) (defaults to: nil)


346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'cli/parser.rb', line 346

def flag(*names, description: nil, replacement: nil, depends_on: nil, hidden: false, subcommands: nil)
  required, flag_type = if names.any? { |name| name.end_with? "=" }
    [OptionParser::REQUIRED_ARGUMENT, :required_flag]
  else
    [OptionParser::OPTIONAL_ARGUMENT, :optional_flag]
  end
  names.map! { |name| name.chomp "=" }
  description = option_description(description, *names, hidden:)
  if replacement.nil?
    process_option(*names, description, type: flag_type, hidden:, subcommands:)
  else
    description += " (disabled#{"; replaced by #{replacement}" if replacement.present?})"
  end
  @parser.on(*names, *wrap_option_desc(description), required) do |option_value|
    # This odisabled should stick around indefinitely.
    odisabled "the `#{names.first}` flag", replacement unless replacement.nil?
    names.each do |name|
      option_name = option_to_name(name)
      @option_sources[option_name] = :args
      @option_types[option_name] ||= flag_type
      set_args_method(option_name.to_sym, option_value)
    end
  end

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

#formula_optionsvoid

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



603
604
605
# File 'cli/parser.rb', line 603

def formula_options
  @formula_options = true
end

#generate_help_text(remaining_args: nil) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • remaining_args (Array<String>, nil) (defaults to: nil)

Returns:



546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
# File 'cli/parser.rb', line 546

def generate_help_text(remaining_args: nil)
  help_text = if remaining_args.nil? || @subcommands.empty?
    @parser.to_s
  elsif (subcommand = remaining_args.filter_map do |arg|
    subcommand_for_name(arg) unless arg.start_with?("-")
  end.first)
    parts = T.let([], T::Array[String])
    parts << (subcommand.usage_banner || "`#{@command_name} #{subcommand.name}`").sub(/\A`brew /, "`")
    option_summaries = option_summaries_text(subcommand.name)
    parts << option_summaries if option_summaries.present?
    parts.join("\n\n")
  else
    parts = T.let([], T::Array[String])
    usage_banner = @usage_banner
    description = @description
    parts << usage_banner if usage_banner.present?
    parts << description if description.present?

    subcommand_lines = @subcommands.map do |subcommand|
      subcommand_summary = if (usage_banner = subcommand.usage_banner)
        usage_banner.lines.drop(1).map(&:strip).find(&:present?)
      end
      subcommand_summary ||= subcommand.description
      if subcommand_summary.present?
        "  `#{subcommand.name}`: #{subcommand_summary}"
      else
        "  `#{subcommand.name}`"
      end
    end
    parts << "Subcommands:\n#{subcommand_lines.join("\n")}"

    option_summaries = option_summaries_text(nil)
    parts << option_summaries if option_summaries.present?
    parts.join("\n\n")
  end

  Formatter.format_help_text(help_text, width: Formatter::COMMAND_DESC_WIDTH)
           .gsub(/\n.*?@@HIDDEN@@.*?(?=\n)/, "")
           .sub(/^/, "#{Tty.bold}Usage: brew#{Tty.reset} ")
           .gsub(/`(.*?)`/m, "#{Tty.bold}\\1#{Tty.reset}")
           .gsub(%r{<([^\s]+?://[^\s]+?)>}) { |url| Formatter.url(url) }
           .gsub(/\*(.*?)\*|<(.*?)>/m) do |underlined|
             underlined[1...-1].to_s.gsub(/^(\s*)(.*?)$/, "\\1#{Tty.underline}\\2#{Tty.reset}")
           end
end

#hide_from_man_page!void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



657
658
659
# File 'cli/parser.rb', line 657

def hide_from_man_page!
  @hide_from_man_page = true
end

#name_to_option(name) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



397
398
399
400
401
402
403
# File 'cli/parser.rb', line 397

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

#named_args(type = nil, number: nil, min: nil, max: nil, without_api: false) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • type (ArgType) (defaults to: nil)
  • number (Integer, nil) (defaults to: nil)
  • min (Integer, nil) (defaults to: nil)
  • max (Integer, nil) (defaults to: nil)
  • without_api (Boolean) (defaults to: false)

Raises:

  • (ArgumentError)


616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
# File 'cli/parser.rb', line 616

def named_args(type = nil, number: nil, min: nil, max: nil, without_api: false)
  raise ArgumentError, "Do not specify both `number` and `min` or `max`" if number && (min || max)

  if type == :none && (number || min || max)
    raise ArgumentError, "Do not specify both `number`, `min` or `max` with `named_args :none`"
  end

  if @current_subcommands.present?
    @current_subcommands.each do |subcommand_name|
      subcommand = subcommand_for_name(subcommand_name)
      raise ArgumentError, "unknown subcommand: #{subcommand_name}" if subcommand.nil?

      subcommand.named_args_type = type
      if type == :none
        subcommand.max_named_args = 0
      elsif number
        subcommand.min_named_args = subcommand.max_named_args = number
      elsif min || max
        subcommand.min_named_args = min
        subcommand.max_named_args = max
      end
      subcommand.named_args_without_api = without_api
    end
    return
  end

  @named_args_type = type

  if type == :none
    @max_named_args = 0
  elsif number
    @min_named_args = @max_named_args = number
  elsif min || max
    @min_named_args = min
    @max_named_args = max
  end

  @named_args_without_api = without_api
end

#named_args_typeArgType

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (ArgType)


319
320
321
322
323
# File 'cli/parser.rb', line 319

def named_args_type
  return @named_args_type if @subcommands.empty?

  subcommand_names
end

#named_args_type_for_subcommand(subcommand_name) ⇒ ArgType

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (ArgType)


720
721
722
# File 'cli/parser.rb', line 720

def named_args_type_for_subcommand(subcommand_name)
  subcommand_for_name(subcommand_name)&.named_args_type
end

#option_description(description, *names, hidden: false) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • description (String, nil)
  • names (String)
  • hidden (Boolean) (defaults to: false)

Returns:



411
412
413
414
415
416
# File 'cli/parser.rb', line 411

def option_description(description, *names, hidden: false)
  return HIDDEN_DESC_PLACEHOLDER if hidden
  return description if description.present?

  option_to_description(*names)
end

#option_to_description(*names) ⇒ String?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



406
407
408
# File 'cli/parser.rb', line 406

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

#option_to_name(option) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



394
# File 'cli/parser.rb', line 394

def option_to_name(option) = self.class.option_to_name(option)

#parse(argv = ARGV.freeze, ignore_invalid_options: false) ⇒ Args

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • argv (Array<String>) (defaults to: ARGV.freeze)
  • ignore_invalid_options (Boolean) (defaults to: false)

Returns:



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'cli/parser.rb', line 458

def parse(argv = ARGV.freeze, ignore_invalid_options: false)
  raise "Arguments were already parsed!" if @args_parsed

  # If we accept formula options, but the command isn't scoped only
  # to casks, parse once allowing invalid options so we can get the
  # remaining list containing formula names.
  if @formula_options && !only_casks?(argv)
    remaining, non_options = parse_remaining(argv, ignore_invalid_options: true)

    argv = [*remaining, "--", *non_options]

    formulae(argv).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:)
        else
          switch name, description:
        end

        conflicts "--cask", name
      end
    end
  end

  remaining, non_options = parse_remaining(argv, ignore_invalid_options:)

  named_args = if ignore_invalid_options
    []
  else
    remaining + non_options
  end

  unless ignore_invalid_options
    if @subcommands.present? && named_args.present?
      subcommand_arg = named_args.fetch(0)
      if (subcommand = subcommand_for_name(subcommand_arg)) && subcommand.alias_options.key?(subcommand_arg)
        set_switch(subcommand.alias_options.fetch(subcommand_arg), value: true, from: :args)
      end
    end
    unless @is_dev_cmd
      set_default_options
      validate_options
    end
    check_constraint_violations(named_args)
    check_named_args(named_args)
    check_subcommand_violations(named_args)
  end

  if @subcommands.present?
    parsed_subcommand = subcommand_name(named_args)
    set_args_method(:subcommand, parsed_subcommand)
    named_args = if parsed_subcommand && named_args.present?
      named_args.drop(1)
    else
      []
    end
  end
  @args.freeze_named_args!(named_args, cask_options: @cask_options, without_api: @named_args_without_api)
  remaining_args = if non_options.empty?
    remaining
  else
    [*remaining, "--", non_options]
  end
  @args.freeze_remaining_args!(remaining_args)
  @args.freeze_processed_options!(@processed_options)
  @args.freeze

  @args_parsed = T.let(true, T.nilable(TrueClass))

  if !ignore_invalid_options && @args.help?
    puts generate_help_text(remaining_args: @subcommands.present? ? remaining : nil)
    exit
  end

  @args
end

#parse_remaining(argv, ignore_invalid_options: false) ⇒ Array<(Array<String>, Array<String>)>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • argv (Array<String>)
  • ignore_invalid_options (Boolean) (defaults to: false)

Returns:



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'cli/parser.rb', line 422

def parse_remaining(argv, ignore_invalid_options: false)
  i = 0
  remaining = []

  argv, non_options = split_non_options(argv)
  allow_commands = Array(@named_args_type).include?(:command)

  while i < argv.count
    begin
      begin
        arg = argv[i]

        remaining << arg unless @parser.parse([arg]).empty?
      rescue OptionParser::MissingArgument
        raise if i + 1 >= argv.count

        args = argv[i..(i + 1)]
        @parser.parse(args)
        i += 1
      end
    rescue OptionParser::InvalidOption
      if ignore_invalid_options || (allow_commands && arg && Commands.path(arg))
        remaining << arg
      else
        $stderr.puts generate_help_text
        raise
      end
    end

    i += 1
  end

  [remaining, non_options]
end

#processed_options_for_root_commandArgs::OptionsType

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



715
716
717
# File 'cli/parser.rb', line 715

def processed_options_for_root_command
  @processed_options_by_subcommand.fetch(nil, [])
end

#processed_options_for_subcommand(subcommand_name) ⇒ Args::OptionsType

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • subcommand_name (String, nil)

Returns:



700
701
702
703
704
705
706
707
708
709
710
711
712
# File 'cli/parser.rb', line 700

def processed_options_for_subcommand(subcommand_name)
  subcommand = if subcommand_name
    subcommand_for_name(subcommand_name)
  else
    default_subcommand
  end
  canonical_subcommand = subcommand&.name

  root_options = @processed_options_by_subcommand.fetch(nil, [])
  return root_options if canonical_subcommand.nil?

  root_options + @processed_options_by_subcommand.fetch(canonical_subcommand, [])
end

#root_usage_banner_textString?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



316
# File 'cli/parser.rb', line 316

def root_usage_banner_text = @usage_banner

#set_args_method(name, value) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



376
377
378
379
380
381
382
383
384
# File 'cli/parser.rb', line 376

def set_args_method(name, value)
  @args.set_arg(name, value)
  return if @args.respond_to?(name)

  @args.define_singleton_method(name) do
    # We cannot reference the ivar directly due to https://github.com/sorbet/sorbet/issues/8106
    instance_variable_get(:@table).fetch(name)
  end
end

#set_default_optionsvoid

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



540
# File 'cli/parser.rb', line 540

def set_default_options; end

#subcommand(name, aliases: [], alias_options: {}, description: nil, default: false, &block) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • name (String)
  • aliases (Array<String>) (defaults to: [])
  • alias_options (Hash{String => String}) (defaults to: {})
  • description (String, nil) (defaults to: nil)
  • default (Boolean) (defaults to: false)
  • block (T.proc.bind(Parser).void, nil)


671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
# File 'cli/parser.rb', line 671

def subcommand(name, aliases: [], alias_options: {}, description: nil, default: false, &block)
  previous_subcommands = @current_subcommands

  @subcommands << Subcommand.new(
    name:,
    aliases:       aliases | alias_options.keys,
    alias_options:,
    description:,
    default:,
  )

  @current_subcommands = [name]
  instance_eval(&block) if block
ensure
  @current_subcommands = previous_subcommands
end

#subcommand_for_name(name) ⇒ Subcommand?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



695
696
697
# File 'cli/parser.rb', line 695

def subcommand_for_name(name)
  @subcommands.find { |subcommand| subcommand.name == name || subcommand.aliases.include?(name) }
end

#subcommand_name(named_args) ⇒ String?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



725
726
727
728
729
730
731
732
733
# File 'cli/parser.rb', line 725

def subcommand_name(named_args)
  subcommand = if named_args.empty?
    default_subcommand
  else
    subcommand_for_name(named_args.fetch(0))
  end

  subcommand&.name
end

#subcommand_namesArray<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



689
# File 'cli/parser.rb', line 689

def subcommand_names = @subcommands.map(&:name)

#subcommands_for_option(option) ⇒ Array<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



736
737
738
# File 'cli/parser.rb', line 736

def subcommands_for_option(option)
  @option_subcommands.fetch(option_to_name(option), [])
end

#switch(*names, description: nil, env: nil, depends_on: nil, method: :on, hidden: false, replacement: nil, odeprecated: false, odisabled: false, disable: false, subcommands: nil) ⇒ void Also known as: switch_option

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • names (String)
  • description (String, nil) (defaults to: nil)
  • env (T.untyped) (defaults to: nil)
  • depends_on (String, nil) (defaults to: nil)
  • method (Symbol) (defaults to: :on)
  • hidden (Boolean) (defaults to: false)
  • replacement (String, false, nil) (defaults to: nil)
  • odeprecated (Boolean) (defaults to: false)
  • odisabled (Boolean) (defaults to: false)
  • disable (Boolean) (defaults to: false)
  • subcommands (String, Array<String>, nil) (defaults to: nil)


223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'cli/parser.rb', line 223

def switch(*names, description: nil, env: nil,
           depends_on: nil, method: :on,
           hidden: false, replacement: nil,
           odeprecated: false, odisabled: false, disable: false,
           subcommands: nil)
  global_switch = names.first.is_a?(Symbol)
  return if global_switch

  if disable
    # this odisabled should be removed in 5.2.0
    odisabled "disable:", "odisabled:"
    odisabled = disable
  end
  if !odeprecated && !odisabled && replacement
    # this odisabled should be removed in 5.2.0
    odisabled "replacement: without :odeprecated or :odisabled",
              "replacement: with :odeprecated or :odisabled"
    odeprecated = true
  end
  hidden = true if odisabled || odeprecated

  description = option_description(description, *names, hidden:)
  env, counterpart = env
  env_hidden = Homebrew::EnvConfig::ENVS.fetch(:"HOMEBREW_#{env.upcase}", {}).fetch(:hidden, false) if env
  if env && @non_global_processed_options.any? && !hidden && !env_hidden
    affix = if counterpart
      " and `#{counterpart}` is passed."
    else
      "."
    end
    description += " Enabled by default if `$HOMEBREW_#{env.upcase}` is set#{affix}"
  end
  process_option(*names, description, type: :switch, hidden:, subcommands:) unless odisabled

  @parser.public_send(method, *names, *wrap_option_desc(description)) do |value|
    # This odeprecated should stick around indefinitely.
    replacement_string = replacement if replacement
    if odeprecated || odisabled
      odeprecated "the `#{names.first}` switch", replacement_string, disable: odisabled
    end
    value = true if names.none? { |name| name.start_with?("--[no-]") }

    set_switch(*names, value:, from: :args)
  end

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

  env_value = value_for_env(env)
  value = env_value&.present?
  set_switch(*names, value:, from: :env) unless value.nil?
end

#usage_banner(text) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'cli/parser.rb', line 286

def usage_banner(text)
  banner, description = text.chomp.split("\n\n", 2)

  if @current_subcommands.present?
    subcommand_description = description
    if subcommand_description.blank?
      usage_banner_lines = text.chomp.lines
      subcommand_description = usage_banner_lines.drop(1).join if usage_banner_lines.size > 1
    end

    @current_subcommands.each do |subcommand_name|
      subcommand = subcommand_for_name(subcommand_name)
      raise ArgumentError, "unknown subcommand: #{subcommand_name}" if subcommand.nil?

      subcommand.usage_banner = text.chomp
      next if subcommand.description.present? || subcommand_description.blank?

      subcommand.description = subcommand_description.lines.first&.chomp
    end
    return
  end

  @usage_banner = banner
  @description = description
end

#usage_banner_textString?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



313
# File 'cli/parser.rb', line 313

def usage_banner_text = @parser.banner

#validate_optionsvoid

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



543
# File 'cli/parser.rb', line 543

def validate_options; end