diff options
-rw-r--r-- | README.md | 8 | ||||
-rw-r--r-- | Rakefile | 2 | ||||
-rw-r--r-- | lib/mixlib/cli.rb | 69 | ||||
-rw-r--r-- | spec/mixlib/cli_spec.rb | 57 |
4 files changed, 80 insertions, 56 deletions
@@ -38,8 +38,8 @@ end # ARGV = [ '-c', 'foo.rb', '-l', 'debug' ] cli = MyCLI.new cli.parse_options -cli.config[:config_file] # 'foo.rb' -cli.config[:log_level] # :debug +cli.parsed_options[:config_file] # 'foo.rb' +cli.parsed_options[:log_level] # :debug ``` If you are using this in conjunction with Mixlib::Config, you can do something like this (building on the above definition): @@ -55,7 +55,7 @@ end class MyCLI def run(argv=ARGV) parse_options(argv) - MyConfig.merge!(config) + MyConfig.merge!(parsed_options) end end @@ -79,7 +79,7 @@ Available arguments to 'option': - `:proc`: If set, the configuration value will be set to the return value of this proc. - `:in`: An array containing the list of accepted values -If you need access to the leftover options that aren't captured in the config, you can get at them through +cli_arguments+ (referring to the above definition of MyCLI). +If you need access to the leftover options that aren't captured in the `parsed_options`, you can get at them through +cli_arguments+ (referring to the above definition of MyCLI). ```ruby # ARGV = [ '-c', 'foo.rb', '-l', 'debug', 'file1', 'file2', 'file3' ] @@ -14,7 +14,7 @@ begin require "chefstyle" require "rubocop/rake_task" RuboCop::RakeTask.new(:style) do |task| - task.options += ["--display-cop-names", "--no-color"] + task.options += ["--display-cop-names", "--no-color", "--auto-correct"] end rescue LoadError puts "chefstyle/rubocop is not available. bundle install first to make sure all dependencies are installed." diff --git a/lib/mixlib/cli.rb b/lib/mixlib/cli.rb index 0516926..178a3e0 100644 --- a/lib/mixlib/cli.rb +++ b/lib/mixlib/cli.rb @@ -35,7 +35,7 @@ module Mixlib # # === Parsing # Command line options are parsed by calling the instance method - # #parse_options. After calling this method, the attribute #config will + # #parse_options. After calling this method, the attribute #parsed_options will # contain a hash of `:option_name => value` pairs. module CLI @@ -142,7 +142,7 @@ module Mixlib # Gives the command line options definition as configured in the DSL. These # are used by #parse_options to generate the option parsing code. To get - # the values supplied by the user, see #config. + # the values supplied by the user, see #parsed_options. attr_accessor :options # A Hash containing the values supplied by command line options. @@ -150,13 +150,20 @@ module Mixlib # The behavior and contents of this Hash vary depending on whether # ClassMethods#use_separate_default_options is enabled. # ==== use_separate_default_options *disabled* - # After initialization, +config+ will contain any default values defined + # After initialization, +parsed_options+ will contain any default values defined # via the mixlib-config DSL. When #parse_options is called, user-supplied # values (from ARGV) will be merged in. # ==== use_separate_default_options *enabled* # After initialization, this will be an empty hash. When #parse_options is - # called, +config+ is populated *only* with user-supplied values. - attr_accessor :config + # called, +parsed_options+ is populated *only* with user-supplied values. + attr_accessor :parsed_options + + # This is the historical method used to access the parsed options. It is + # too widely used to deprecate so we will leave it around. But new users + # of mixlib-cli are encouraged to use #parsed_options since it is more clear. + def config + parsed_options + end # If ClassMethods#use_separate_default_options is enabled, this will be a # Hash containing key value pairs of `:option_name => default_value` @@ -164,9 +171,15 @@ module Mixlib # # If use_separate_default_options is disabled, it will always be an empty # hash. - attr_accessor :default_config + attr_accessor :default_options + + # See #config + def default_config + default_options + end - # Any arguments which were not parsed and placed in "config"--the leftovers. + # Any arguments which were not parsed and placed in #parsed_options - + # the leftovers. attr_accessor :cli_arguments # Banner for the option parser. If the option parser is printed, e.g., by @@ -182,8 +195,8 @@ module Mixlib # object<Mixlib::Config>:: Returns an instance of whatever you wanted :) def initialize(*args) @options = Hash.new - @config = Hash.new - @default_config = Hash.new + @parsed_options = Hash.new + @default_options = Hash.new @opt_parser = nil # Set the banner @@ -193,11 +206,11 @@ module Mixlib klass_options = self.class.options klass_options.keys.inject(@options) { |memo, key| memo[key] = klass_options[key].dup; memo } - # If use_separate_defaults? is on, default values go in @default_config + # If use_separate_defaults? is on, default values go in @default_options defaults_container = if self.class.use_separate_defaults? - @default_config + @default_options else - @config + @parsed_options end # Set the default configuration values for this instance @@ -231,7 +244,7 @@ module Mixlib # Deal with any required values options.each do |opt_key, opt_value| - if opt_value[:required] && !config.key?(opt_key) + if opt_value[:required] && !parsed_options.key?(opt_key) reqarg = opt_value[:short] || opt_value[:long] puts "You must supply #{reqarg}!" puts @opt_parser @@ -239,11 +252,11 @@ module Mixlib end if opt_value[:in] unless opt_value[:in].kind_of?(Array) - raise(ArgumentError, "Options config key :in must receive an Array") + raise(ArgumentError, "Options configuration key :in must receive an Array") end - if config[opt_key] && !opt_value[:in].include?(config[opt_key]) + if parsed_options[opt_key] && !opt_value[:in].include?(parsed_options[opt_key]) reqarg = opt_value[:short] || opt_value[:long] - puts "#{reqarg}: #{config[opt_key]} is not included in the list ['#{opt_value[:in].join("', '")}'] " + puts "#{reqarg}: #{parsed_options[opt_key]} is not included in the list ['#{opt_value[:in].join("', '")}'] " puts @opt_parser exit 2 end @@ -281,18 +294,18 @@ module Mixlib parse_block = 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. - opt_val[:proc].call(c, config[opt_key]) - else - # Older single-argument proc. - opt_val[:proc].call(c) - end - else - # No proc. - c - end + parsed_options[opt_key] = if opt_val[:proc] + if opt_val[:proc].arity == 2 + # New hotness to allow for reducer-style procs. + opt_val[:proc].call(c, parsed_options[opt_key]) + else + # Older single-argument proc. + opt_val[:proc].call(c) + end + else + # No proc. + c + end puts opts if opt_val[:show_options] exit opt_val[:exit] if opt_val[:exit] end diff --git a/spec/mixlib/cli_spec.rb b/spec/mixlib/cli_spec.rb index ed890b5..3629dd2 100644 --- a/spec/mixlib/cli_spec.rb +++ b/spec/mixlib/cli_spec.rb @@ -57,7 +57,7 @@ describe Mixlib::CLI do end end - context "when configured with default single-config-hash behavior" do + context "when configured with default single-parsed-options-hash behavior" do before(:each) do @cli = TestCLI.new @@ -85,10 +85,16 @@ describe Mixlib::CLI do }) end - it "sets the default config value for any options that include it" do + it "sets the default parsed_options value for any options that include it" do TestCLI.option(:config_file, short: "-l LOG", default: :debug) @cli = TestCLI.new - expect(@cli.config[:config_file]).to eql(:debug) + expect(@cli.parsed_options[:config_file]).to eql(:debug) + end + + it "sets the historical value config" do + TestCLI.option(:config_file, short: "-l LOG", default: :debug) + @cli = TestCLI.new + expect(@cli.parsed_options).to eql(@cli.config) end end @@ -132,21 +138,21 @@ describe Mixlib::CLI do end describe "parse_options" do - it "sets the corresponding config value for non-boolean arguments" do + it "sets the corresponding parsed_options value for non-boolean arguments" do TestCLI.option(:config_file, short: "-c CONFIG") @cli = TestCLI.new @cli.parse_options([ "-c", "foo.rb" ]) - expect(@cli.config[:config_file]).to eql("foo.rb") + expect(@cli.parsed_options[:config_file]).to eql("foo.rb") end - it "sets the corresponding config value according to a supplied proc" do + it "sets the corresponding parsed_options value according to a supplied proc" do TestCLI.option(:number, short: "-n NUMBER", - proc: Proc.new { |config| config.to_i + 2 } + proc: Proc.new { |c| c.to_i + 2 } ) @cli = TestCLI.new @cli.parse_options([ "-n", "2" ]) - expect(@cli.config[:number]).to eql(4) + expect(@cli.parsed_options[:number]).to eql(4) end it "passes the existing value to two-argument procs" do @@ -156,24 +162,24 @@ describe Mixlib::CLI do ) @cli = TestCLI.new @cli.parse_options([ "-n", "2", "-n", "3" ]) - expect(@cli.config[:number]).to eql(%w{2 3}) + expect(@cli.parsed_options[:number]).to eql(%w{2 3}) end - it "sets the corresponding config value to true for boolean arguments" do + it "sets the corresponding parsed_options value to true for boolean arguments" do TestCLI.option(:i_am_boolean, short: "-i", boolean: true) @cli = TestCLI.new @cli.parse_options([ "-i" ]) - expect(@cli.config[:i_am_boolean]).to be true + expect(@cli.parsed_options[:i_am_boolean]).to be true end - it "sets the corresponding config value to false when a boolean is prefixed with --no" do + it "sets the corresponding parsed_options value to false when a boolean is prefixed with --no" do TestCLI.option(:i_am_boolean, long: "--[no-]bool", boolean: true) @cli = TestCLI.new @cli.parse_options([ "--no-bool" ]) - expect(@cli.config[:i_am_boolean]).to be false + expect(@cli.parsed_options[:i_am_boolean]).to be false end - it "exits if a config option has :exit set" do + it "exits if a parsed_options option has :exit set" do TestCLI.option(:i_am_exit, short: "-x", boolean: true, exit: 0) @cli = TestCLI.new expect(lambda { @cli.parse_options(["-x"]) }).to raise_error(SystemExit) @@ -221,7 +227,7 @@ describe Mixlib::CLI do TestCLI.option(:inclusion, short: "-i val", in: %w{one two}, required: true) @cli = TestCLI.new @cli.parse_options(["-i", "one"]) - expect(@cli.config[:inclusion]).to eql("one") + expect(@cli.parsed_options[:inclusion]).to eql("one") end it "changes description if :in key is specified" do @@ -235,21 +241,21 @@ describe Mixlib::CLI do TestCLI.option(:require_me, short: "-r", boolean: true, required: true) @cli = TestCLI.new @cli.parse_options(["-r"]) - expect(@cli.config[:require_me]).to be true + expect(@cli.parsed_options[:require_me]).to be true end it "doesn't exit if a required boolean option is specified and false" do TestCLI.option(:require_me, long: "--[no-]req", boolean: true, required: true) @cli = TestCLI.new @cli.parse_options(["--no-req"]) - expect(@cli.config[:require_me]).to be false + expect(@cli.parsed_options[:require_me]).to be false end it "doesn't exit if a required option is specified and empty" do TestCLI.option(:require_me, short: "-r VALUE", required: true) @cli = TestCLI.new @cli.parse_options(["-r", ""]) - expect(@cli.config[:require_me]).to eql("") + expect(@cli.parsed_options[:require_me]).to eql("") end it "preserves all of the commandline arguments, ARGV" do @@ -280,14 +286,19 @@ describe Mixlib::CLI do it "sets default values on the `default` hash" do @cli.parse_options([]) - expect(@cli.default_config[:defaulter]).to eql("this is the default") - expect(@cli.config[:defaulter]).to be_nil + expect(@cli.default_options[:defaulter]).to eql("this is the default") + expect(@cli.parsed_options[:defaulter]).to be_nil end - it "sets parsed values on the `config` hash" do + it "sets parsed values on the `parsed_options` hash" do @cli.parse_options(%w{-D not-default}) - expect(@cli.default_config[:defaulter]).to eql("this is the default") - expect(@cli.config[:defaulter]).to eql("not-default") + expect(@cli.default_options[:defaulter]).to eql("this is the default") + expect(@cli.parsed_options[:defaulter]).to eql("not-default") + end + + it "sets the historical value default_config" do + @cli.parse_options([]) + expect(@cli.default_options).to eql(@cli.default_config) end end |