class RuboCop::Options

@api private
This class handles command line options.

def add_aliases(opts)

def add_aliases(opts)
  option(opts, '-l', '--lint') do
    @options[:only] ||= []
    @options[:only] << 'Lint'
  end
  option(opts, '-x', '--fix-layout') do
    @options[:only] ||= []
    @options[:only] << 'Layout'
    @options[:auto_correct] = true
  end
end

def add_auto_gen_options(opts)

def add_auto_gen_options(opts)
  option(opts, '--auto-gen-config')
  option(opts, '--regenerate-todo') do
    @options.replace(ConfigRegeneration.new.options.merge(@options))
  end
  option(opts, '--exclude-limit COUNT') do
    @validator.validate_exclude_limit_option
  end
  option(opts, '--disable-uncorrectable')
  option(opts, '--[no-]offense-counts')
  option(opts, '--[no-]auto-gen-only-exclude')
  option(opts, '--[no-]auto-gen-timestamp')
  option(opts, '--init')
end

def add_boolean_flags(opts)

rubocop:disable Metrics/MethodLength, Metrics/AbcSize
def add_boolean_flags(opts)
  option(opts, '-F', '--fail-fast')
  option(opts, '-d', '--debug')
  option(opts, '-D', '--[no-]display-cop-names')
  option(opts, '-E', '--extra-details')
  option(opts, '-S', '--display-style-guide')
  option(opts, '-a', '--auto-correct') do
    @options[:safe_auto_correct] = true
  end
  option(opts, '--safe-auto-correct') do
    warn '--safe-auto-correct is deprecated; use --auto-correct'
    @options[:safe_auto_correct] = @options[:auto_correct] = true
  end
  option(opts, '-A', '--auto-correct-all') do
    @options[:auto_correct] = true
  end
  option(opts, '--disable-pending-cops')
  option(opts, '--enable-pending-cops')
  option(opts, '--ignore-disable-comments')
  option(opts, '--safe')
  option(opts, '--stderr')
  option(opts, '--[no-]color')
  option(opts, '-v', '--version')
  option(opts, '-V', '--verbose-version')
  option(opts, '-P', '--parallel')
end

def add_cache_options(opts)

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

def add_configuration_options(opts)

def add_configuration_options(opts)
  option(opts, '-c', '--config FILE')
  option(opts, '--force-exclusion')
  option(opts, '--only-recognized-file-types')
  option(opts, '--ignore-parent-exclusion')
  option(opts, '--force-default-config')
  add_auto_gen_options(opts)
end

def add_cop_selection_csv_option(option, opts)

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
    @options[:"#{option}"] = list.empty? ? [''] : list.split(',')
  end
end

def add_flags_with_optional_args(opts)

def add_flags_with_optional_args(opts)
  option(opts, '--show-cops [COP1,COP2,...]') do |list|
    @options[:show_cops] = list.nil? ? [] : list.split(',')
  end
end

def add_formatting_options(opts)

def add_formatting_options(opts)
  option(opts, '-f', '--format FORMATTER') do |key|
    @options[:formatters] ||= []
    @options[:formatters] << [key]
  end
  option(opts, '-o', '--out FILE') do |path|
    if @options[:formatters]
      @options[:formatters].last << path
    else
      @options[:output_path] = path
    end
  end
  option(opts, '--display-time')
  option(opts, '--display-only-failed')
end

def add_list_options(opts)

def add_list_options(opts)
  option(opts, '-L', '--list-target-files')
end

def add_only_options(opts)

def add_only_options(opts)
  add_cop_selection_csv_option('except', opts)
  add_cop_selection_csv_option('only', opts)
  option(opts, '--only-guide-cops')
end

def add_severity_option(opts)

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
  option(opts, '--display-only-fail-level-offenses')
end

def args_from_env

def args_from_env
  Shellwords.split(ENV.fetch('RUBOCOP_OPTS', ''))
end

def args_from_file

def args_from_file
  if File.exist?('.rubocop') && !File.directory?('.rubocop')
    File.read('.rubocop').shellsplit
  else
    []
  end
end

def define_options

def define_options
  OptionParser.new do |opts|
    opts.banner = 'Usage: rubocop [options] [file1, file2, ...]'
    add_list_options(opts)
    add_only_options(opts)
    add_configuration_options(opts)
    add_formatting_options(opts)
    option(opts, '-r', '--require FILE') { |f| require_feature(f) }
    add_severity_option(opts)
    add_flags_with_optional_args(opts)
    add_cache_options(opts)
    add_boolean_flags(opts)
    add_aliases(opts)
    option(opts, '-s', '--stdin FILE')
  end
end

def initialize

def initialize
  @options = {}
  @validator = OptionsValidator.new(@options)
end

def long_opt_symbol(args)

e.g. [..., '--auto-correct', ...] to :auto_correct.
Finds the option in `args` starting with -- and converts it to a symbol,
def long_opt_symbol(args)
  long_opt = args.find { |arg| arg.start_with?('--') }
  long_opt[2..-1].sub('[no-]', '').sub(/ .*/, '')
                 .tr('-', '_').gsub(/[\[\]]/, '').to_sym
end

def option(opts, *args)

value, in addition to calling the block if a block is given.
Sets a value in the @options hash, based on the given long option and its
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

def parse(command_line_args)

def parse(command_line_args)
  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

def require_feature(file)

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