From fc851a6305f997f67b9ae79ed0297ebe5346baeb Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Fri, 12 Jul 2019 15:56:00 +0530 Subject: Fix chefstyle Signed-off-by: Vivek Singh --- Rakefile | 2 +- lib/mixlib/cli.rb | 53 ++++++++++++++++++++++++--------------------- lib/mixlib/cli/formatter.rb | 1 + spec/mixlib/cli_spec.rb | 50 ++++++++++++++++++++---------------------- 4 files changed, 54 insertions(+), 52 deletions(-) diff --git a/Rakefile b/Rakefile index 199e0ce..bcbe224 100644 --- a/Rakefile +++ b/Rakefile @@ -38,4 +38,4 @@ task :console do IRB.start end -task default: [:spec, :style] +task default: %i{spec style} diff --git a/lib/mixlib/cli.rb b/lib/mixlib/cli.rb index d0c1c67..c426b06 100644 --- a/lib/mixlib/cli.rb +++ b/lib/mixlib/cli.rb @@ -52,8 +52,8 @@ module Mixlib # contents will be iterated and cloned as well. def deep_dup(object) cloned_object = object.respond_to?(:dup) ? object.dup : object - if cloned_object.kind_of?(Enumerable) - if cloned_object.kind_of?(Hash) + if cloned_object.is_a?(Enumerable) + if cloned_object.is_a?(Hash) new_hash = cloned_object.class.new cloned_object.each do |key, value| cloned_key = deep_dup(key) @@ -122,7 +122,8 @@ module Mixlib # i def option(name, args) @options ||= {} - raise(ArgumentError, "Option name must be a symbol") unless name.kind_of?(Symbol) + raise(ArgumentError, "Option name must be a symbol") unless name.is_a?(Symbol) + @options[name.to_sym] = args end @@ -148,12 +149,12 @@ module Mixlib # === Returns # :: The config hash for the created option. def deprecated_option(name, - replacement: nil, - long: nil, - short: nil, - boolean: false, - value_mapper: nil, - keep: true) + replacement: nil, + long: nil, + short: nil, + boolean: false, + value_mapper: nil, + keep: true) description = if replacement replacement_cfg = options[replacement] @@ -165,15 +166,15 @@ module Mixlib value_mapper ||= Proc.new { |v| v } option(name, - long: long, - short: short, - boolean: boolean, - description: description, - on: :tail, - deprecated: true, - keep: keep, - replacement: replacement, - value_mapper: value_mapper) + long: long, + short: short, + boolean: boolean, + description: description, + on: :tail, + deprecated: true, + keep: keep, + replacement: replacement, + value_mapper: value_mapper) end # Get the hash of current options. @@ -193,7 +194,8 @@ module Mixlib # === Returns # @options:: The current options hash. def options=(val) - raise(ArgumentError, "Options must recieve a hash") unless val.kind_of?(Hash) + raise(ArgumentError, "Options must recieve a hash") unless val.is_a?(Hash) + @options = val end @@ -256,9 +258,9 @@ module Mixlib # === Returns # object:: Returns an instance of whatever you wanted :) def initialize(*args) - @options = Hash.new - @config = Hash.new - @default_config = Hash.new + @options = {} + @config = {} + @default_config = {} @opt_parser = nil # Set the banner @@ -316,9 +318,10 @@ module Mixlib exit 2 end if opt_config[:in] - unless opt_config[:in].kind_of?(Array) + unless opt_config[:in].is_a?(Array) raise(ArgumentError, "Options config key :in must receive an Array") end + if config[opt_key] && !opt_config[:in].include?(config[opt_key]) reqarg = Formatter.combined_option_display_name(opt_config[:short], opt_config[:long]) puts "#{reqarg}: #{config[opt_key]} is not one of the allowed values: #{Formatter.friendly_opt_list(opt_config[:in])}" @@ -358,7 +361,7 @@ module Mixlib end parse_block = - Proc.new() do |c| + Proc.new do |c| config[opt_key] = if opt_val[:proc] if opt_val[:proc].arity == 2 # New hotness to allow for reducer-style procs. @@ -427,7 +430,7 @@ module Mixlib end def build_option_arguments(opt_setting) - arguments = Array.new + arguments = [] arguments << opt_setting[:short] if opt_setting[:short] arguments << opt_setting[:long] if opt_setting[:long] diff --git a/lib/mixlib/cli/formatter.rb b/lib/mixlib/cli/formatter.rb index fef8503..59f98a4 100644 --- a/lib/mixlib/cli/formatter.rb +++ b/lib/mixlib/cli/formatter.rb @@ -25,6 +25,7 @@ module Mixlib def self.friendly_opt_list(opt_array) opts = opt_array.map { |x| "'#{x}'" } return opts.join(" or ") if opts.size < 3 + opts[0..-2].join(", ") + ", or " + opts[-1] end end diff --git a/spec/mixlib/cli_spec.rb b/spec/mixlib/cli_spec.rb index 46795aa..91a4a4d 100644 --- a/spec/mixlib/cli_spec.rb +++ b/spec/mixlib/cli_spec.rb @@ -103,19 +103,19 @@ describe Mixlib::CLI do }) expect(cli.options[:option_file]).to include( - boolean: false, - deprecated: true, - description: "This flag is deprecated. Use -l instead.", - exit: nil, - in: nil, - long: nil, - keep: true, - proc: nil, - replacement: :config_file, - required: false, - short: "-o FILE", - on: :tail, - show_options: false + boolean: false, + deprecated: true, + description: "This flag is deprecated. Use -l instead.", + exit: nil, + in: nil, + long: nil, + keep: true, + proc: nil, + replacement: :config_file, + required: false, + short: "-o FILE", + on: :tail, + show_options: false ) expect(cli.options[:option_file][:value_mapper].class).to eql(Proc) end @@ -177,8 +177,7 @@ describe Mixlib::CLI do it "sets the corresponding config value according to a supplied proc" do TestCLI.option(:number, short: "-n NUMBER", - proc: Proc.new { |config| config.to_i + 2 } - ) + proc: Proc.new { |config| config.to_i + 2 }) @cli = TestCLI.new @cli.parse_options([ "-n", "2" ]) expect(@cli.config[:number]).to eql(4) @@ -187,8 +186,7 @@ describe Mixlib::CLI do it "passes the existing value to two-argument procs" do TestCLI.option(:number, short: "-n NUMBER", - proc: Proc.new { |value, existing| existing ||= []; existing << value; existing } - ) + proc: Proc.new { |value, existing| existing ||= []; existing << value; existing }) @cli = TestCLI.new @cli.parse_options([ "-n", "2", "-n", "3" ]) expect(@cli.config[:number]).to eql(%w{2 3}) @@ -306,7 +304,7 @@ describe Mixlib::CLI do @cli = TestCLI.new argv_old = ARGV.dup ARGV.replace ["-c", "foo.rb"] - @cli.parse_options() + @cli.parse_options expect(ARGV).to eql(["-c", "foo.rb"]) ARGV.replace argv_old end @@ -343,9 +341,9 @@ describe Mixlib::CLI do context "and a value_mapper is provided" do before do TestCLI.deprecated_option(:option_x, - long: "--option-x ARG", - replacement: :option_b, - value_mapper: Proc.new { |val| val == "valid" ? "a" : "xxx" } ) + long: "--option-x ARG", + replacement: :option_b, + value_mapper: Proc.new { |val| val == "valid" ? "a" : "xxx" } ) end it "still checks the replacement's 'in' validation list" do @@ -363,9 +361,9 @@ describe Mixlib::CLI do context "and keep is set to false in the deprecated option" do before do TestCLI.deprecated_option(:option_x, - long: "--option-x ARG", - replacement: :option_c, - keep: false) + long: "--option-x ARG", + replacement: :option_c, + keep: false) end it "captures the replacement value, but does not set the deprecated value" do cli.parse_options %w{--option-x blah} @@ -395,8 +393,8 @@ describe Mixlib::CLI do context "when the replacement does not accept a value" do before do TestCLI.deprecated_option(:option_x, - long: "--option-x ARG", - replacement: :option_c) + long: "--option-x ARG", + replacement: :option_c) end it "will still set the value because you haven't given a custom value mapper to set a true/false value" do -- cgit v1.2.1