<commit_before># frozen_string_literal: true

require_relative '../cli'
require_relative '../../facts'
require_relative '../../version'

require 'optparse'

module OctocatalogDiff
  module CatalogDiff
    class Cli
      # This class contains the option parser. 'parse_options' is the external entry point.
      class Options
        # The usage banner.
        BANNER = 'Usage: catalog-diff -n <hostname> [-f <from environment>] [-t <to environment>]'.freeze

        # List of classes
        def self.classes
          @classes ||= []
        end

        # Define the Option class and newoption() method for use by catalog-diff/cli/options/*.rb files
        class Option
          DEFAULT_WEIGHT = 999
          def self.has_weight(w) # rubocop:disable Style/PredicateName
            @weight = w
          end

          def self.weight
            @weight || DEFAULT_WEIGHT
          end

          def self.name
            self::NAME
          end

          def self.newoption(name, &block)
            klass = Class.new(OctocatalogDiff::CatalogDiff::Cli::Options::Option)
            klass.const_set('NAME', name)
            klass.class_exec(&block)
            Options.classes.push(klass)
          end
        end

        # Method to call all of the other methods in this class. Except in very specific circumstances,
        # this should be the method called from outside of this class.
        # @param argv [Array] Array of command line arguments
        # @param defaults [Hash] Default values
        # @return [Hash] Parsed options
        def self.parse_options(argv, defaults = {})
          options = defaults.dup
          Options.classes.clear
          ::OptionParser.new do |parser|
            parser.banner = "#{BANNER}\n\n"
            option_classes.each do |klass|
              obj = klass.new
              obj.parse(parser, options)
            end
            parser.on_tail('-v', '--version', 'Show version information about this program and quit.') do
              puts "octocatalog-diff #{OctocatalogDiff::Version::VERSION}"
              exit
            end
          end.parse! argv
          options
        end

        # Read in *.rb files in the 'options' directory and create classes from them.
        # Sort the classes according to weight and return the list of sorted classes.
        # @return [Array<Class>] Sorted classes
        def self.option_classes
          files = Dir.glob(File.join(File.dirname(__FILE__), 'options', '*.rb'))
          files.each { |file| load file } # Populates self.classes
          classes.sort do |a, b|
            [
              a.weight <=> b.weight,
              a.name.downcase <=> b.name.downcase,
              a.object_id <=> b.object_id
            ].find(&:nonzero?)
          end
        end

        # Sets up options that can be defined globally or for just one branch. For example, with a
        # CLI name of 'puppet-binary' this will acknowledge 3 options: --puppet-binary (global),
        # --from-puppet-binary (for the from branch only), and --to-puppet-binary (for the to branch
        # only). The only options that will be created are the 'to' and 'from' variants, but the global
        # option will populate any of the 'to' and 'from' variants that are missing.
        # @param :datatype [?] Expected data type
        def self.option_globally_or_per_branch(opts = {})<commit_msg>Update comment to reflect sorting by name too<commit_after># frozen_string_literal: true

require_relative '../cli'
require_relative '../../facts'
require_relative '../../version'

require 'optparse'

module OctocatalogDiff
  module CatalogDiff
    class Cli
      # This class contains the option parser. 'parse_options' is the external entry point.
      class Options
        # The usage banner.
        BANNER = 'Usage: catalog-diff -n <hostname> [-f <from environment>] [-t <to environment>]'.freeze

        # List of classes
        def self.classes
          @classes ||= []
        end

        # Define the Option class and newoption() method for use by catalog-diff/cli/options/*.rb files
        class Option
          DEFAULT_WEIGHT = 999
          def self.has_weight(w) # rubocop:disable Style/PredicateName
            @weight = w
          end

          def self.weight
            @weight || DEFAULT_WEIGHT
          end

          def self.name
            self::NAME
          end

          def self.newoption(name, &block)
            klass = Class.new(OctocatalogDiff::CatalogDiff::Cli::Options::Option)
            klass.const_set('NAME', name)
            klass.class_exec(&block)
            Options.classes.push(klass)
          end
        end

        # Method to call all of the other methods in this class. Except in very specific circumstances,
        # this should be the method called from outside of this class.
        # @param argv [Array] Array of command line arguments
        # @param defaults [Hash] Default values
        # @return [Hash] Parsed options
        def self.parse_options(argv, defaults = {})
          options = defaults.dup
          Options.classes.clear
          ::OptionParser.new do |parser|
            parser.banner = "#{BANNER}\n\n"
            option_classes.each do |klass|
              obj = klass.new
              obj.parse(parser, options)
            end
            parser.on_tail('-v', '--version', 'Show version information about this program and quit.') do
              puts "octocatalog-diff #{OctocatalogDiff::Version::VERSION}"
              exit
            end
          end.parse! argv
          options
        end

        # Read in *.rb files in the 'options' directory and create classes from them.
        # Sort the classes according to weight and name and return the list of sorted classes.
        # @return [Array<Class>] Sorted classes
        def self.option_classes
          files = Dir.glob(File.join(File.dirname(__FILE__), 'options', '*.rb'))
          files.each { |file| load file } # Populates self.classes
          classes.sort do |a, b|
            [
              a.weight <=> b.weight,
              a.name.downcase <=> b.name.downcase,
              a.object_id <=> b.object_id
            ].find(&:nonzero?)
          end
        end

        # Sets up options that can be defined globally or for just one branch. For example, with a
        # CLI name of 'puppet-binary' this will acknowledge 3 options: --puppet-binary (global),
        # --from-puppet-binary (for the from branch only), and --to-puppet-binary (for the to branch
        # only). The only options that will be created are the 'to' and 'from' variants, but the global
        # option will populate any of the 'to' and 'from' variants that are missing.
        # @param :datatype [?] Expected data type
        def self.option_globally_or_per_branch(opts = {})
{'fraction_non_alphanumeric': 0.0935535740388832, 'fraction_numerical': 0.00116941967548604, 'mean_word_length': 1.9696180555555556, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '7052051', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1805, 'n_words': 629}