From efdfbfeee765dbfff485a4adf76fd88f0db175cf Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 11 Feb 2025 11:10:46 +0100 Subject: [PATCH 1/3] Fix all Rubocop offenses --- .rubocop.yml | 8 ++++++-- lib/consoler/application.rb | 10 +++++++--- lib/consoler/matcher.rb | 29 +++++++++++++++++------------ lib/consoler/option.rb | 2 +- lib/consoler/options.rb | 8 +++----- lib/consoler/version.rb | 2 +- 6 files changed, 35 insertions(+), 24 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 59842f0..f82e2b4 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,5 +1,5 @@ AllCops: - TargetRubyVersion: 2.2 + TargetRubyVersion: 2.4 Exclude: - 'test/**/*' @@ -37,9 +37,13 @@ Style/GuardClause: # `super` makes no sense in this case # `#respond_to_missing?` is implemented -Style/MethodMissingSuper: +Lint/MissingSuper: Enabled: false # I like the comma Style/TrailingCommaInArguments: Enabled: false + +# Grouping `attr_reader` is not VCS friendly +Style/AccessorGrouping: + Enabled: false diff --git a/lib/consoler/application.rb b/lib/consoler/application.rb index d18ed03..06d361c 100644 --- a/lib/consoler/application.rb +++ b/lib/consoler/application.rb @@ -30,8 +30,8 @@ class Application # @option options [bool] :rescue_errors Should the application catch errors (optional) def initialize(options = {}) @description = options[:description] - @rescue_errors = if !options[:rescue_errors].nil? then - options[:rescue_errors] + @rescue_errors = if !options[:rescue_errors].nil? + options[:rescue_errors] else true end @@ -83,6 +83,9 @@ def method_missing(command_name, input = nil, &block) nil end + # It would be a breaking change + # rubocop:disable Style/OptionalBooleanParameter + # Run the application with a list of arguments # # @param args [Array] Arguments @@ -100,12 +103,13 @@ def run(args = ARGV, disable_usage_message = false) result rescue RuntimeError => e if @rescue_errors - $stderr.puts "A runtime error occured: #{e.message.strip}" + warn "A runtime error occured: #{e.message.strip}" nil else raise e end end + # rubocop:enable Style/OptionalBooleanParameter # Show the usage message # diff --git a/lib/consoler/matcher.rb b/lib/consoler/matcher.rb index 8becdf5..6d0aa1a 100644 --- a/lib/consoler/matcher.rb +++ b/lib/consoler/matcher.rb @@ -215,24 +215,24 @@ def _match_arguments @optionals_before.each do |mandatory_arg_name, optionals| # fill the optional argument option with a value if there are enough # arguments supplied (info available from optionals map) - optionals.each do |_, optional| + optionals.each_value do |optional| optional.each do |before| - if before[:included] - return nil if argument_values_index >= total_argument_values + next unless before[:included] - @matched_options[before[:name]] = @argument_values[argument_values_index] - argument_values_index += 1 - end + return nil if argument_values_index >= total_argument_values + + @matched_options[before[:name]] = @argument_values[argument_values_index] + argument_values_index += 1 end end # only fill mandatory argument if its not the :REMAINING key - if mandatory_arg_name != :REMAINING - return nil if argument_values_index >= total_argument_values + next if mandatory_arg_name == :REMAINING - @matched_options[mandatory_arg_name] = @argument_values[argument_values_index] - argument_values_index += 1 - end + return nil if argument_values_index >= total_argument_values + + @matched_options[mandatory_arg_name] = @argument_values[argument_values_index] + argument_values_index += 1 end remaining = [] @@ -253,6 +253,9 @@ def _match_arguments_optionals_before @optionals_before = {} tracker = {} + # `@options` is not a hash + # rubocop:disable Style/HashEachMethods + @options.each do |option, _key| next unless option.is_argument @@ -271,6 +274,8 @@ def _match_arguments_optionals_before end end + # rubocop:enable Style/HashEachMethods + # make sure all optionals are accounted for in the map if tracker != {} # use a special key so we can handle it differently in the filling process @@ -333,7 +338,7 @@ def _fill_defaults # # @return [Consoler::Matcher] def _each_optional_before_sorted - @optionals_before.each do |_, optionals| + @optionals_before.each_value do |optionals| tmp = [] optionals.each do |optional_index, before| tmp.push( diff --git a/lib/consoler/option.rb b/lib/consoler/option.rb index 5421282..8ca48da 100644 --- a/lib/consoler/option.rb +++ b/lib/consoler/option.rb @@ -117,7 +117,7 @@ def initialize(option_def, tracker) option, @is_optional = _is_optional option, tracker option, @is_long = _is_long option option, @is_short = _is_short option - @is_argument = (!@is_long && !@is_short) + @is_argument = !@is_long && !@is_short option, @is_value = _value option, @is_argument option, @aliases = _aliases option, alias_defs, tracker diff --git a/lib/consoler/options.rb b/lib/consoler/options.rb index a01d393..2b4a1bc 100644 --- a/lib/consoler/options.rb +++ b/lib/consoler/options.rb @@ -20,7 +20,7 @@ def initialize(options_def) return if options_def.nil? # strip the description - if (match = /(^|\s+)-- (?.*)$/.match(options_def)) + if (match = /(?^|\s+)-- (?.*)$/.match(options_def)) @description = match[:description] options_def = options_def[0...-match[0].size] end @@ -80,10 +80,8 @@ def get_with_alias(name) # # @yield [Consoler::Option, Integer] An option # @return [Consoler::Options] - def each - @options.each_with_index do |option, i| - yield option, i - end + def each(&block) + @options.each_with_index(&block) self end diff --git a/lib/consoler/version.rb b/lib/consoler/version.rb index 77a6f3f..358a525 100644 --- a/lib/consoler/version.rb +++ b/lib/consoler/version.rb @@ -2,5 +2,5 @@ module Consoler # Current version number - VERSION = '1.3.0'.freeze + VERSION = '1.3.0' end From 93f02b7ee7d195131bd4155874fd794787b1f4ea Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 11 Feb 2025 11:12:49 +0100 Subject: [PATCH 2/3] Add newer Ruby versions to test matrix --- .github/workflows/tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index c9bfa1f..31433e4 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -13,12 +13,12 @@ jobs: strategy: fail-fast: false matrix: - ruby: ['2.4', '2.5', '2.6', '2.7', '3.0'] + ruby: ['2.4', '2.5', '2.6', '2.7', '3.0', '3.1', '3.2', '3.3', '3.4'] name: Ruby ${{ matrix.ruby }} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Set up Ruby # change this to (see https://github.com/ruby/setup-ruby#versioning): From 1cecddbb78fc9479ef831856f24faa6a9cd1b284 Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 11 Feb 2025 11:26:20 +0100 Subject: [PATCH 3/3] Add dev dependencies that are no longer default in Ruby Use the latest version that are installable in Ruby 2.4, our lowest version in the test matrix. --- consoler.gemspec | 2 ++ 1 file changed, 2 insertions(+) diff --git a/consoler.gemspec b/consoler.gemspec index 5f185c6..09ac82e 100644 --- a/consoler.gemspec +++ b/consoler.gemspec @@ -25,6 +25,8 @@ Gem::Specification.new do |spec| spec.required_ruby_version = '>= 2.4' spec.add_development_dependency 'bundler', '~> 2.0' spec.add_development_dependency 'minitest', '~> 5.11.3' + spec.add_development_dependency 'mutex_m', '~> 0.1.2' + spec.add_development_dependency 'ostruct', '~> 0.1.0' spec.add_development_dependency 'rake', '~> 12.3.0' spec.add_development_dependency 'simplecov', '~> 0.16.1' spec.add_development_dependency 'yard', '~> 0.9.12'