Skip to content

Commit

Permalink
Merge pull request mdub#106 from AlexWayfer/update_rubocop
Browse files Browse the repository at this point in the history
Update RuboCop
  • Loading branch information
mdub committed Sep 9, 2020
2 parents 9aeed06 + ea83ba5 commit 651271b
Show file tree
Hide file tree
Showing 18 changed files with 45 additions and 22 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 120
11 changes: 4 additions & 7 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
AllCops:
TargetRubyVersion: 2.1
TargetRubyVersion: 2.4
NewCops: enable

Eval:
Exclude:
- "Rakefile"
Layout/LineLength:
Max: 120

Layout/EmptyLinesAroundBlockBody:
Enabled: false
Expand All @@ -21,9 +21,6 @@ Metrics/BlockLength:
Exclude:
- "spec/**/*"

Metrics/LineLength:
Max: 120

Metrics/MethodLength:
Max: 30

Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: ruby
rvm:
- 2.3
- 2.4
- 2.5
- 2.6
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ group :development do
gem "highline"
gem "listen", "~> 3.0"
gem "rake", "~> 12.3"
gem "rubocop", "~> 0.57.2", "<= 0.58", require: false
gem "rubocop", "~> 0.89.0", require: false
end

group :test do
Expand Down
1 change: 1 addition & 0 deletions clamp.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ Gem::Specification.new do |s|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.require_paths = ["lib"]

s.required_ruby_version = "~> 2.4"
end
1 change: 1 addition & 0 deletions lib/clamp/attribute/declaration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def define_reader_for(attribute)

def define_default_for(attribute)
return false if attribute.default_value.nil?

define_method(attribute.default_method) do
attribute.default_value
end
Expand Down
2 changes: 2 additions & 0 deletions lib/clamp/attribute/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ def default_description
("$#{@environment_variable}" if defined?(@environment_variable)),
(@default_value.inspect if defined?(@default_value))
].compact

return nil if default_sources.empty?

"#{Clamp.message(:default)}: " + default_sources.join(", #{Clamp.message(:or)} ")
end

Expand Down
1 change: 1 addition & 0 deletions lib/clamp/attribute/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def default_from_environment
return if self.defined?
return if attribute.environment_variable.nil?
return unless ENV.key?(attribute.environment_variable)

# Set the parameter value if it's environment variable is present
value = ENV[attribute.environment_variable]
begin
Expand Down
9 changes: 4 additions & 5 deletions lib/clamp/help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def usage(usage)
@declared_usage_descriptions << usage
end

attr_reader :declared_usage_descriptions
attr_reader :declared_usage_descriptions, :description

def description=(description)
@description = description.dup
Expand All @@ -29,8 +29,6 @@ def banner(description)
self.description = description
end

attr_reader :description

def derived_usage_description
parts = ["[OPTIONS]"]
parts += parameters.map(&:name)
Expand Down Expand Up @@ -84,19 +82,20 @@ def row(lhs, rhs)
end

def add_usage(invocation_path, usage_descriptions)
line Clamp.message(:usage_heading) + ":"
line "#{Clamp.message(:usage_heading)}:"
usage_descriptions.each do |usage|
line " #{invocation_path} #{usage}".rstrip
end
end

def add_description(description)
return unless description

line
line description.gsub(/^/, " ")
end

DETAIL_FORMAT = " %-29s %s".freeze
DETAIL_FORMAT = " %-29s %s"

def add_list(heading, items)
line
Expand Down
2 changes: 2 additions & 0 deletions lib/clamp/option/declaration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def recognised_options

def declare_implicit_help_option
return false if effective_options.find { |o| o.handles?("--help") }

help_switches = ["--help"]
help_switches.unshift("-h") unless effective_options.find { |o| o.handles?("-h") }
option help_switches, :flag, "print help" do
Expand All @@ -55,6 +56,7 @@ def effective_options

def options_declared_on(ancestor)
return [] unless ancestor.is_a?(Clamp::Option::Declaration)

ancestor.declared_options
end

Expand Down
7 changes: 5 additions & 2 deletions lib/clamp/option/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def initialize(switches, type, description, options = {})
@description = description
super(options)
@multivalued = options[:multivalued]

return unless options.key?(:required)

@required = options[:required]
# Do some light validation for conflicting settings.
raise ArgumentError, "Specifying a :default value with :required doesn't make sense" if options.key?(:default)
Expand All @@ -43,7 +45,7 @@ def flag_value(switch)

def read_method
if flag?
super + "?"
"#{super}?"
else
super
end
Expand All @@ -63,7 +65,7 @@ def default_conversion_block

def help_lhs
lhs = switches.join(", ")
lhs += " " + type unless flag?
lhs += " #{type}" unless flag?
lhs
end

Expand All @@ -81,6 +83,7 @@ def recognised_switches

def infer_attribute_name
raise Clamp::DeclarationError, "You must specify either a long-switch or an :attribute_value" unless long_switch

inferred_name = long_switch.sub(/^--(\[no-\])?/, "").tr("-", "_")
inferred_name += "_list" if multivalued?
inferred_name
Expand Down
7 changes: 6 additions & 1 deletion lib/clamp/option/parsing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ def set_options_from_command_line
until remaining_arguments.empty?
unless remaining_arguments.first.start_with?("-")
break unless argument_buffer.size < argument_buffer_limit

argument_buffer << remaining_arguments.shift

next
end

switch = remaining_arguments.shift

break if switch == "--"

handle_switch(switch)
end
remaining_arguments.unshift(*argument_buffer)
Expand All @@ -55,7 +60,7 @@ def split_trailing_switches(switch)
when /\A(-\w)(.+)\z/m # combined short options
switch = Regexp.last_match(1)
if find_option(switch).flag?
remaining_arguments.unshift("-" + Regexp.last_match(2))
remaining_arguments.unshift("-#{Regexp.last_match(2)}")
else
remaining_arguments.unshift(Regexp.last_match(2))
end
Expand Down
3 changes: 3 additions & 0 deletions lib/clamp/parameter/declaration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ def inheritable_parameters

def parameter_buffer_limit
return 0 unless Clamp.allow_options_after_parameters

return Float::INFINITY if inheritable_parameters.any?(&:multivalued?)

inheritable_parameters.size
end

private

def superclass_inheritable_parameters
return [] unless superclass.respond_to?(:inheritable_parameters, true)

superclass.inheritable_parameters
end

Expand Down
9 changes: 6 additions & 3 deletions lib/clamp/parameter/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,22 @@ def help_lhs

def consume(arguments)
raise ArgumentError, Clamp.message(:no_value_provided) if required? && arguments.empty?

arguments.shift(multivalued? ? arguments.length : 1)
end

private

ELLIPSIS_SUFFIX = / \.\.\.$/
OPTIONAL = /^\[(.*)\]/
ELLIPSIS_SUFFIX = / \.\.\.$/.freeze
OPTIONAL = /^\[(.*)\]/.freeze

VALID_ATTRIBUTE_NAME = /^[a-z0-9_]+$/
VALID_ATTRIBUTE_NAME = /^[a-z0-9_]+$/.freeze

def infer_attribute_name
inferred_name = name.downcase.tr("-", "_").sub(ELLIPSIS_SUFFIX, "").sub(OPTIONAL) { Regexp.last_match(1) }

raise "cannot infer attribute_name from #{name.inspect}" unless inferred_name =~ VALID_ATTRIBUTE_NAME

inferred_name += "_list" if multivalued?
inferred_name
end
Expand Down
4 changes: 3 additions & 1 deletion lib/clamp/subcommand/declaration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ def find_subcommand(name)
def find_subcommand_class(*names)
names.inject(self) do |command_class, name|
return nil unless command_class

subcommand = command_class.find_subcommand(name)
subcommand.subcommand_class if subcommand
subcommand&.subcommand_class if subcommand
end
end

Expand All @@ -42,6 +43,7 @@ def inheritable_attributes

def default_subcommand=(name)
raise Clamp::DeclarationError, "default_subcommand must be defined before subcommands" if has_subcommands?

@default_subcommand = name
end

Expand Down
2 changes: 2 additions & 0 deletions lib/clamp/subcommand/execution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def instantiate_subcommand(name)
subcommand = subcommand_class.new(invocation_path_for(name), context)
self.class.inheritable_attributes.each do |attribute|
next unless attribute.of(self).defined?

attribute.of(subcommand).set(attribute.of(self).get)
end
subcommand
Expand All @@ -38,6 +39,7 @@ def invocation_path_for(name)
def find_subcommand_class(name)
subcommand_def = self.class.find_subcommand(name)
return subcommand_def.subcommand_class if subcommand_def

subcommand_missing(name)
end

Expand Down
1 change: 1 addition & 0 deletions lib/clamp/subcommand/parsing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Parsing

def parse_subcommand
return false unless self.class.has_subcommands?

extend(Subcommand::Execution)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/clamp/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Clamp
VERSION = "1.3.2".freeze
VERSION = "1.3.2"
end

0 comments on commit 651271b

Please sign in to comment.