<commit_before>require 'optparse'

module VagrantPlugins
  module DevCommands
    # Definition of an executable command
    class CommandDef
      attr_reader :name
      attr_reader :parameters
      attr_reader :script

      attr_reader :box
      attr_reader :desc
      attr_reader :help
      attr_reader :usage

      def initialize(spec)
        @name       = spec[:name]
        @parameters = spec[:parameters]
        @script     = spec[:script]

        @box   = spec[:box]
        @desc  = spec[:desc]
        @help  = spec[:help]
        @usage = spec[:usage]
      end

      def run_script(argv)
        script = @script
        script = script.call if script.is_a?(Proc)

        opts = {}
        opts = parse_argv(argv) if @parameters

        (script % opts).strip
      end

      private

      def escape_option_values(options)
        @parameters.each do |key, conf|
          next if conf[:escape].nil?

          conf[:escape].each do |char, with|
            options[key] = options[key].sub(char, "#{with}#{char}")
          end
        end

        options
      end

      def options_with_defaults
        options = {}

        @parameters.each do |key, conf|
          options[key] = '' if conf[:optional]
          options[key] = conf[:default] unless conf[:default].nil?
        end

        options
      end

      def parse_argv(argv)
        options = options_with_defaults<commit_msg>ensures escaping keys are used as strings<commit_after>require 'optparse'

module VagrantPlugins
  module DevCommands
    # Definition of an executable command
    class CommandDef
      attr_reader :name
      attr_reader :parameters
      attr_reader :script

      attr_reader :box
      attr_reader :desc
      attr_reader :help
      attr_reader :usage

      def initialize(spec)
        @name       = spec[:name]
        @parameters = spec[:parameters]
        @script     = spec[:script]

        @box   = spec[:box]
        @desc  = spec[:desc]
        @help  = spec[:help]
        @usage = spec[:usage]
      end

      def run_script(argv)
        script = @script
        script = script.call if script.is_a?(Proc)

        opts = {}
        opts = parse_argv(argv) if @parameters

        (script % opts).strip
      end

      private

      def escape_option_values(options)
        @parameters.each do |key, conf|
          next if conf[:escape].nil?

          conf[:escape].each do |char, with|
            char         = char.to_s unless char.is_a?(String)
            options[key] = options[key].sub(char, "#{with}#{char}")
          end
        end

        options
      end

      def options_with_defaults
        options = {}

        @parameters.each do |key, conf|
          options[key] = '' if conf[:optional]
          options[key] = conf[:default] unless conf[:default].nil?
        end

        options
      end

      def parse_argv(argv)
        options = options_with_defaults
{'fraction_non_alphanumeric': 0.0954653937947494, 'fraction_numerical': 0.0, 'mean_word_length': 1.8708414872798433, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3504384', 'n_tokens_mistral': 842, 'n_tokens_neox': 819, 'n_words': 213}