diff options
author | Thom May <thom@may.lt> | 2016-07-06 14:55:24 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-06 14:55:24 +0100 |
commit | 8077a095781b2ce9298f8d58e695f8e4bc69f38e (patch) | |
tree | db6aee780c0382c69f0b6aa83a51a7217abc732b | |
parent | 2e2670b7664acda8b5c48da7a7b71f5b775ff07d (diff) | |
parent | f789c3f9055f2bf3a5e37c683dbf92c0293032fc (diff) | |
download | mixlib-cli-8077a095781b2ce9298f8d58e695f8e4bc69f38e.tar.gz |
Merge pull request #28 from coderanger/master
Support two-argument procs for reducer style.
-rw-r--r-- | lib/mixlib/cli.rb | 13 | ||||
-rw-r--r-- | spec/mixlib/cli_spec.rb | 10 |
2 files changed, 22 insertions, 1 deletions
diff --git a/lib/mixlib/cli.rb b/lib/mixlib/cli.rb index 31e44be..736ec76 100644 --- a/lib/mixlib/cli.rb +++ b/lib/mixlib/cli.rb @@ -281,7 +281,18 @@ module Mixlib parse_block = Proc.new() do |c| - config[opt_key] = (opt_val[:proc] && opt_val[:proc].call(c)) || 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 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 39b1eb1..f42f9ce 100644 --- a/spec/mixlib/cli_spec.rb +++ b/spec/mixlib/cli_spec.rb @@ -149,6 +149,16 @@ describe Mixlib::CLI do @cli.config[:number].should == 4 end + it "should pass the existing value to two-argument procs" do + TestCLI.option(:number, + :short => "-n NUMBER", + :proc => Proc.new { |value, existing| existing ||= []; existing << value; existing } + ) + @cli = TestCLI.new + @cli.parse_options([ "-n", "2", "-n", "3" ]) + @cli.config[:number].should == %w{2 3} + end + it "should set the corresponding config value to true for boolean arguments" do TestCLI.option(:i_am_boolean, :short => "-i", :boolean => true) @cli = TestCLI.new |