Class: RuboCop::Options Private
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/rubocop/options.rb |
Overview
This class handles command line options.
Constant Summary
-
DEFAULT_MAXIMUM_EXCLUSION_ITEMS =
# File 'lib/rubocop/options.rb', line 2015
-
EXITING_OPTIONS =
# File 'lib/rubocop/options.rb', line 19%i[version verbose_version show_cops show_docs_url lsp].freeze
-
E_STDIN_NO_PATH =
# File 'lib/rubocop/options.rb', line 15'-s/--stdin requires exactly one path, relative to the ' \ 'root of the project. RuboCop will use this path to determine which ' \ 'cops are enabled (via eg. Include/Exclude), and so that certain cops ' \ 'like Naming/FileName can be checked.'
Class Method Summary
- .new ⇒ Options constructor Internal use only
Instance Method Summary
- #parse(command_line_args) Internal use only
- #add_additional_modes(opts) private Internal use only
-
#add_autocorrection_options(opts)
private
Internal use only
rubocop:todo Naming/InclusiveLanguage the autocorrect command-line arguments map to the autocorrect @options values like so:
- #add_cache_options(opts) private Internal use only
- #add_check_options(opts) private Internal use only
- #add_config_generation_options(opts) private Internal use only
- #add_cop_selection_csv_option(option, opts) private Internal use only
- #add_general_options(opts) private Internal use only
- #add_lsp_option(opts) private Internal use only
-
#add_output_options(opts)
private
Internal use only
Metrics/MethodLength.
- #add_profile_options(opts) private Internal use only
- #add_server_options(opts) private Internal use only
- #add_severity_option(opts) private Internal use only
- #define_options private Internal use only
- #handle_deprecated_option(old_option, new_option) private Internal use only
-
#long_opt_symbol(args)
private
Internal use only
Finds the option in
args
starting with — and converts it to a symbol, e.g. -
#option(opts, *args)
private
Internal use only
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.
- #rainbow private Internal use only
- #require_feature(file) private Internal use only
-
#section(opts, heading, &_block)
private
Internal use only
Creates a section of options in order to separate them visually when using
--help
.
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
# File 'lib/rubocop/options.rb', line 140
def (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 ]#add_check_options(opts) (private)
[ GitHub ]# File 'lib/rubocop/options.rb', line 73
def (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 (opts) section(opts, 'Config Generation') do option(opts, '--auto-gen-config') option(opts, '--regenerate-todo') do @options.replace(ConfigRegeneration.new. .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 = "--#{option} argument should be [COP1,COP2,...]." raise OptionArgumentError, 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 (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 ]#add_output_options(opts) (private)
Metrics/MethodLength
# File 'lib/rubocop/options.rb', line 105
def (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 ]#add_server_options(opts) (private)
[ GitHub ]#add_severity_option(opts) (private)
[ GitHub ]#define_options (private)
[ GitHub ]# File 'lib/rubocop/options.rb', line 53
def OptionParser.new do |opts| opts. = rainbow.wrap('Usage: rubocop [options] [file1, file2, ...]').bright (opts) (opts) add_lsp_option(opts) (opts) (opts) (opts) (opts) add_additional_modes(opts) (opts) # `stackprof` is not supported on JRuby and Windows. (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.
# 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.
# 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) .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
.
# File 'lib/rubocop/options.rb', line 278
def section(opts, heading, &_block) heading = rainbow.wrap(heading).bright opts.separator("\n#{heading}:\n") yield end