Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 6 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AllCops:
TargetRubyVersion: 2.2
TargetRubyVersion: 2.4

Exclude:
- 'test/**/*'
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions consoler.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
10 changes: 7 additions & 3 deletions lib/consoler/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
#
Expand Down
29 changes: 17 additions & 12 deletions lib/consoler/matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion lib/consoler/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 3 additions & 5 deletions lib/consoler/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def initialize(options_def)
return if options_def.nil?

# strip the description
if (match = /(^|\s+)-- (?<description>.*)$/.match(options_def))
if (match = /(?<whitespace>^|\s+)-- (?<description>.*)$/.match(options_def))
@description = match[:description]
options_def = options_def[0...-match[0].size]
end
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/consoler/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

module Consoler
# Current version number
VERSION = '1.3.0'.freeze
VERSION = '1.3.0'
end