class RuboCop::OptionsValidator
Validates option arguments and the options’ compatibility with each other.
def self.validate_cop_list(names)
Cop name validation must be done later than option parsing, so it's not
def self.validate_cop_list(names) return unless names namespaces = Cop::Cop.all.types.map { |t| t.to_s.capitalize } names.each do |name| next if Cop::Cop.all.any? { |c| c.cop_name == name } next if namespaces.include?(name) next if %w(Syntax Lint/Syntax).include?(name) raise ArgumentError, "Unrecognized cop or namespace: #{name}." end end
def boolean_or_empty_cache?
def boolean_or_empty_cache? !@options.key?(:cache) || %w(true false).include?(@options[:cache]) end
def except_syntax?
def except_syntax? @options.key?(:except) && (@options[:except] & %w(Lint/Syntax Syntax)).any? end
def incompatible_options
def incompatible_options @incompatible_options ||= @options.keys & Options::EXITING_OPTIONS end
def initialize(options)
def initialize(options) @options = options end
def no_offense_counts_without_auto_gen_config?
def no_offense_counts_without_auto_gen_config? @options.key?(:no_offense_counts) && !@options.key?(:auto_gen_config) end
def only_includes_unneeded_disable?
def only_includes_unneeded_disable? @options.key?(:only) && (@options[:only] & %w(Lint/UnneededDisable UnneededDisable)).any? end
def validate_compatibility
def validate_compatibility if only_includes_unneeded_disable? raise ArgumentError, 'Lint/UnneededDisable can not be used with --only.' end if except_syntax? raise ArgumentError, 'Syntax checking can not be turned off.' end unless boolean_or_empty_cache? raise ArgumentError, '-C/--cache argument must be true or false' end if no_offense_counts_without_auto_gen_config? raise ArgumentError, '--no-offense-counts can only be used together ' \ 'with --auto-gen-config.' end return if incompatible_options.size <= 1 raise ArgumentError, 'Incompatible cli options: ' \ "#{incompatible_options.inspect}" end
def validate_exclude_limit_option(args)
def validate_exclude_limit_option(args) if @options[:exclude_limit] !~ /^\d+$/ # Emulate OptionParser's behavior to make failures consistent regardless # of option order. raise OptionParser::MissingArgument end # --exclude-limit is valid if there's a parsed or yet unparsed # --auto-gen-config. return if @options[:auto_gen_config] || args.include?('--auto-gen-config') raise ArgumentError, '--exclude-limit can only be used with --auto-gen-config.' end