123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Options Private

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: lib/rubocop/options.rb

Overview

This class handles command line options.

Constant Summary

Class Method Summary

Instance Method Summary

Instance Method Details

#add_additional_modes(opts) (private)

[ GitHub ]

  
# File 'lib/rubocop/options.rb', line 229

def add_additional_modes(opts)
  section(opts, 'Additional Modes') do
    option(opts, '-L', '--list-target-files')
    option(opts, '--show-cops [COP1,COP2,...]') do |list|
      @options[:show_cops] = list.nil? ? [] : list.split(',')
    end
    option(opts, '--show-docs-url [COP1,COP2,...]') do |list|
      @options[:show_docs_url] = list.nil? ? [] : list.split(',')
    end
  end
end

#add_autocorrection_options(opts) (private)

rubocop:todo Naming/InclusiveLanguage the autocorrect command-line arguments map to the autocorrect @options values like so: :fix_layout :autocorrect :safe_autocorrect :autocorrect_all -x, --fix-layout true true - - -a, --auto-correct - true true - --safe-auto-correct - true true - -A, --auto-correct-all - true - true

[ GitHub ]

  
# File 'lib/rubocop/options.rb', line 140

def add_autocorrection_options(opts) # rubocop:disable Metrics/MethodLength
  section(opts, 'Autocorrection') do
    option(opts, '-a', '--autocorrect') { @options[:safe_autocorrect] = true }
    option(opts, '--auto-correct') do
      handle_deprecated_option('--auto-correct', '--autocorrect')
      @options[:safe_autocorrect] = true
    end
    option(opts, '--safe-auto-correct') do
      handle_deprecated_option('--safe-auto-correct', '--autocorrect')
      @options[:safe_autocorrect] = true
    end

    option(opts, '-A', '--autocorrect-all') { @options[:autocorrect] = true }
    option(opts, '--auto-correct-all') do
      handle_deprecated_option('--auto-correct-all', '--autocorrect-all')
      @options[:autocorrect] = true
    end

    option(opts, '--disable-uncorrectable')
  end
end

#add_cache_options(opts) (private)

[ GitHub ]

  
# File 'lib/rubocop/options.rb', line 205

def add_cache_options(opts)
  section(opts, 'Caching') do
    option(opts, '-C', '--cache FLAG')
    option(opts, '--cache-root DIR') { @validator.validate_cache_enabled_for_cache_root }
  end
end

#add_check_options(opts) (private)

[ GitHub ]

  
# File 'lib/rubocop/options.rb', line 73

def add_check_options(opts) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  section(opts, 'Basic Options') do # rubocop:disable Metrics/BlockLength
    option(opts, '-l', '--lint') do
      @options[:only] ||= []
      @options[:only] << 'Lint'
    end
    option(opts, '-x', '--fix-layout') do
      @options[:only] ||= []
      @options[:only] << 'Layout'
      @options[:autocorrect] = true
    end
    option(opts, '--safe')
    add_cop_selection_csv_option('except', opts)
    add_cop_selection_csv_option('only', opts)
    option(opts, '--only-guide-cops')
    option(opts, '-F', '--fail-fast')
    option(opts, '--disable-pending-cops')
    option(opts, '--enable-pending-cops')
    option(opts, '--ignore-disable-comments')
    option(opts, '--force-exclusion')
    option(opts, '--only-recognized-file-types')
    option(opts, '--ignore-parent-exclusion')
    option(opts, '--ignore-unrecognized-cops')
    option(opts, '--force-default-config')
    option(opts, '-s', '--stdin FILE')
    option(opts, '--editor-mode')
    option(opts, '-P', '--[no-]parallel')
    option(opts, '--raise-cop-error')
    add_severity_option(opts)
  end
end

#add_config_generation_options(opts) (private)

[ GitHub ]

  
# File 'lib/rubocop/options.rb', line 163

def add_config_generation_options(opts)
  section(opts, 'Config Generation') do
    option(opts, '--auto-gen-config')

    option(opts, '--regenerate-todo') do
      @options.replace(ConfigRegeneration.new.options.merge(@options))
    end

    option(opts, '--exclude-limit COUNT') { @validator.validate_exclude_limit_option }
    option(opts, '--no-exclude-limit')

    option(opts, '--[no-]offense-counts')
    option(opts, '--[no-]auto-gen-only-exclude')
    option(opts, '--[no-]auto-gen-timestamp')
    option(opts, '--[no-]auto-gen-enforced-style')
  end
end

#add_cop_selection_csv_option(option, opts) (private)

[ GitHub ]

  
# File 'lib/rubocop/options.rb', line 181

def add_cop_selection_csv_option(option, opts)
  option(opts, "--#{option} [COP1,COP2,...]") do |list|
    unless list
      message = "--#{option} argument should be [COP1,COP2,...]."

      raise OptionArgumentError, message
    end

    cop_names = list.empty? ? [''] : list.split(',')
    cop_names.unshift('Lint/Syntax') if option == 'only' && !cop_names.include?('Lint/Syntax')

    @options[:"#{option}"] = cop_names
  end
end

#add_general_options(opts) (private)

[ GitHub ]

  
# File 'lib/rubocop/options.rb', line 241

def add_general_options(opts)
  section(opts, 'General Options') do
    option(opts, '--init')
    option(opts, '-c', '--config FILE')
    option(opts, '-d', '--debug')
    option(opts, '-r', '--require FILE') { |f| require_feature(f) }
    option(opts, '--[no-]color')
    option(opts, '-v', '--version')
    option(opts, '-V', '--verbose-version')
  end
end

#add_lsp_option(opts) (private)

[ GitHub ]

  
# File 'lib/rubocop/options.rb', line 212

def add_lsp_option(opts)
  section(opts, 'LSP Option') do
    option(opts, '--lsp')
  end
end

#add_output_options(opts) (private)

Metrics/MethodLength

[ GitHub ]

  
# File 'lib/rubocop/options.rb', line 105

def add_output_options(opts) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  section(opts, 'Output Options') do
    option(opts, '-f', '--format FORMATTER') do |key|
      @options[:formatters] ||= []
      @options[:formatters] << [key]
    end

    option(opts, '-D', '--[no-]display-cop-names')
    option(opts, '-E', '--extra-details')
    option(opts, '-S', '--display-style-guide')

    option(opts, '-o', '--out FILE') do |path|
      if @options[:formatters]
        @options[:formatters].last << path
      else
        @options[:output_path] = path
      end
    end

    option(opts, '--stderr')
    option(opts, '--display-time')
    option(opts, '--display-only-failed')
    option(opts, '--display-only-fail-level-offenses')
    option(opts, '--display-only-correctable')
    option(opts, '--display-only-safe-correctable')
  end
end

#add_profile_options(opts) (private)

[ GitHub ]

  
# File 'lib/rubocop/options.rb', line 253

def add_profile_options(opts)
  section(opts, 'Profiling Options') do
    option(opts, '--profile') do
      @options[:profile] = true
      @options[:cache] = 'false' unless @options.key?(:cache)
    end
    option(opts, '--memory')
  end
end

#add_server_options(opts) (private)

[ GitHub ]

  
# File 'lib/rubocop/options.rb', line 218

def add_server_options(opts)
  section(opts, 'Server Options') do
    option(opts, '--[no-]server')
    option(opts, '--restart-server')
    option(opts, '--start-server')
    option(opts, '--stop-server')
    option(opts, '--server-status')
    option(opts, '--no-detach')
  end
end

#add_severity_option(opts) (private)

[ GitHub ]

  
# File 'lib/rubocop/options.rb', line 196

def add_severity_option(opts)
  table = RuboCop::Cop::Severity::CODE_TABLE.merge(A: :autocorrect)
  option(opts, '--fail-level SEVERITY',
         RuboCop::Cop::Severity::NAMES + [:autocorrect],
         table) do |severity|
    @options[:fail_level] = severity
  end
end

#define_options (private)

[ GitHub ]

  
# File 'lib/rubocop/options.rb', line 53

def define_options
  OptionParser.new do |opts|
    opts.banner = rainbow.wrap('Usage: rubocop [options] [file1, file2, ...]').bright

    add_check_options(opts)
    add_cache_options(opts)
    add_lsp_option(opts)
    add_server_options(opts)
    add_output_options(opts)
    add_autocorrection_options(opts)
    add_config_generation_options(opts)
    add_additional_modes(opts)
    add_general_options(opts)

    # `stackprof` is not supported on JRuby and Windows.
    add_profile_options(opts) if RUBY_ENGINE == 'ruby' && !Platform.windows?
  end
end

#handle_deprecated_option(old_option, new_option) (private)

[ GitHub ]

  
# File 'lib/rubocop/options.rb', line 263

def handle_deprecated_option(old_option, new_option)
  warn rainbow.wrap("#{old_option} is deprecated; use #{new_option} instead.").yellow
  @options[long_opt_symbol([new_option])] = @options.delete(long_opt_symbol([old_option]))
end

#long_opt_symbol(args) (private)

Finds the option in args starting with — and converts it to a symbol, e.g. […​, '--autocorrect', …​] to :autocorrect.

[ GitHub ]

  
# File 'lib/rubocop/options.rb', line 297

def long_opt_symbol(args)
  long_opt = args.find { |arg| arg.start_with?('--') }
  long_opt[2..].sub('[no-]', '').sub(/ .*/, '').tr('-', '_').gsub(/[\[\]]/, '').to_sym
end

#option(opts, *args) (private)

Sets a value in the @options hash, based on the given long option and its value, in addition to calling the block if a block is given.

[ GitHub ]

  
# File 'lib/rubocop/options.rb', line 286

def option(opts, *args)
  long_opt_symbol = long_opt_symbol(args)
  args += Array(OptionsHelp::TEXT[long_opt_symbol])
  opts.on(*args) do |arg|
    @options[long_opt_symbol] = arg
    yield arg if block_given?
  end
end

#parse(command_line_args)

[ GitHub ]

  
# File 'lib/rubocop/options.rb', line 27

def parse(command_line_args)
  args_from_file = ArgumentsFile.read_as_arguments
  args_from_env = ArgumentsEnv.read_as_arguments
  args = args_from_file.concat(args_from_env).concat(command_line_args)

  define_options.parse!(args)

  @validator.validate_compatibility

  if @options[:stdin]
    # The parser will put the file name given after --stdin into
    # @options[:stdin]. If it did, then the args array should be empty.
    raise OptionArgumentError, E_STDIN_NO_PATH if args.any?

    # We want the STDIN contents in @options[:stdin] and the file name in
    # args to simplify the rest of the processing.
    args = [@options[:stdin]]
    @options[:stdin] = $stdin.binmode.read
  end

  [@options, args]
end

#rainbow (private)

[ GitHub ]

  
# File 'lib/rubocop/options.rb', line 268

def rainbow
  @rainbow ||= begin
    rainbow = Rainbow.new
    rainbow.enabled = false if ARGV.include?('--no-color')
    rainbow
  end
end

#require_feature(file) (private)

[ GitHub ]

  
# File 'lib/rubocop/options.rb', line 302

def require_feature(file)
  # If any features were added on the CLI from `--require`,
  # add them to the config.
  ConfigLoader.add_loaded_features(file)
  require file
end

#section(opts, heading, &_block) (private)

Creates a section of options in order to separate them visually when using --help.

[ GitHub ]

  
# File 'lib/rubocop/options.rb', line 278

def section(opts, heading, &_block)
  heading = rainbow.wrap(heading).bright
  opts.separator("\n#{heading}:\n")
  yield
end