<commit_before># frozen_string_literal: true

require "cli/args"
require "optparse"
require "set"
require "formula"

COMMAND_DESC_WIDTH = 80
OPTION_DESC_WIDTH = 43

module Homebrew
  module CLI
    class Parser
      attr_reader :processed_options, :hide_from_man_page

      def self.from_cmd_path(cmd_path)
        cmd_args_method_name = Commands.args_method_name(cmd_path)

        begin
          Homebrew.send(cmd_args_method_name) if require?(cmd_path)
        rescue NoMethodError => e
          raise if e.name != cmd_args_method_name

          nil
        end
      end

      def self.global_options
        [
          ["-q", "--quiet", "Suppress any warnings."],
          ["-v", "--verbose", "Make some output more verbose."],
          ["-d", "--debug", "Display any debugging information."],
        ]
      end

      def initialize(&block)
        @parser = OptionParser.new

        @args = Homebrew::CLI::Args.new

        @constraints = []
        @conflicts = []
        @switch_sources = {}
        @processed_options = []
        @max_named_args = nil
        @min_named_args = nil
        @min_named_type = nil
        @hide_from_man_page = false
        @formula_options = false

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

        instance_eval(&block) if block_given?

        post_initialize
      end

      def post_initialize
        # Disable default handling of `--version` switch.
        @parser.base.long.delete("version")

        # Disable default handling of `--help` switch.
        @parser.on_tail("-h", "--help", "Show this message.") do
          # Handled in `brew.rb`.
        end
      end

      def switch(*names, description: nil, env: nil, required_for: nil, depends_on: nil)
        global_switch = names.first.is_a?(Symbol)
        return if global_switch

        description = option_to_description(*names) if description.nil?
        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?(env)
      end
      alias switch_option switch

      def env?(env)
        return false if env.blank?

        Homebrew::EnvConfig.send("#{env}?")
      rescue NoMethodError
        false
      end

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

      def usage_banner_text
        @parser.banner
      end

      def comma_array(name, description: nil)
        name = name.chomp "="
        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

      def flag(*names, description: nil, required_for: nil, depends_on: nil)
        required = if names.any? { |name| name.end_with? "=" }
          OptionParser::REQUIRED_ARGUMENT
        else
          OptionParser::OPTIONAL_ARGUMENT
        end
        names.map! { |name| name.chomp "=" }<commit_msg>Use `try` instead of `send`.<commit_after># frozen_string_literal: true

require "cli/args"
require "optparse"
require "set"
require "formula"

COMMAND_DESC_WIDTH = 80
OPTION_DESC_WIDTH = 43

module Homebrew
  module CLI
    class Parser
      attr_reader :processed_options, :hide_from_man_page

      def self.from_cmd_path(cmd_path)
        cmd_args_method_name = Commands.args_method_name(cmd_path)

        begin
          Homebrew.send(cmd_args_method_name) if require?(cmd_path)
        rescue NoMethodError => e
          raise if e.name != cmd_args_method_name

          nil
        end
      end

      def self.global_options
        [
          ["-q", "--quiet", "Suppress any warnings."],
          ["-v", "--verbose", "Make some output more verbose."],
          ["-d", "--debug", "Display any debugging information."],
        ]
      end

      def initialize(&block)
        @parser = OptionParser.new

        @args = Homebrew::CLI::Args.new

        @constraints = []
        @conflicts = []
        @switch_sources = {}
        @processed_options = []
        @max_named_args = nil
        @min_named_args = nil
        @min_named_type = nil
        @hide_from_man_page = false
        @formula_options = false

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

        instance_eval(&block) if block_given?

        post_initialize
      end

      def post_initialize
        # Disable default handling of `--version` switch.
        @parser.base.long.delete("version")

        # Disable default handling of `--help` switch.
        @parser.on_tail("-h", "--help", "Show this message.") do
          # Handled in `brew.rb`.
        end
      end

      def switch(*names, description: nil, env: nil, required_for: nil, depends_on: nil)
        global_switch = names.first.is_a?(Symbol)
        return if global_switch

        description = option_to_description(*names) if description.nil?
        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?(env)
      end
      alias switch_option switch

      def env?(env)
        return false if env.blank?

        Homebrew::EnvConfig.try(:"#{env}?")
      end

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

      def usage_banner_text
        @parser.banner
      end

      def comma_array(name, description: nil)
        name = name.chomp "="
        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

      def flag(*names, description: nil, required_for: nil, depends_on: nil)
        required = if names.any? { |name| name.end_with? "=" }
          OptionParser::REQUIRED_ARGUMENT
        else
          OptionParser::OPTIONAL_ARGUMENT
        end
        names.map! { |name| name.chomp "=" }
{'fraction_non_alphanumeric': 0.10449538553140816, 'fraction_numerical': 0.0011908306043465317, 'mean_word_length': 2.3245917862444334, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4913430', 'n_tokens_mistral': 2029, 'n_tokens_neox': 1976, 'n_words': 473}