diff options
author | Mehrez Alachheb <mehrez.alachheb@inria.fr> | 2013-06-15 22:55:44 +0200 |
---|---|---|
committer | Mehrez Alachheb <mehrez.alachheb@inria.fr> | 2013-06-15 22:55:44 +0200 |
commit | 94e5b7f860d5acacb75653a0ec2f876a197ebdf7 (patch) | |
tree | 1d954f5ea3753f84983da005f691abfcb3ba3398 | |
parent | 83e161811cbe483759790d737229efbca2f93021 (diff) | |
download | mixlib-cli-94e5b7f860d5acacb75653a0ec2f876a197ebdf7.tar.gz |
Add option key ':in' to specifie that option value should be included in the given list
-rw-r--r-- | lib/mixlib/cli.rb | 16 | ||||
-rw-r--r-- | spec/mixlib/cli_spec.rb | 29 |
2 files changed, 43 insertions, 2 deletions
diff --git a/lib/mixlib/cli.rb b/lib/mixlib/cli.rb index bdaabdb..f0b62d2 100644 --- a/lib/mixlib/cli.rb +++ b/lib/mixlib/cli.rb @@ -173,6 +173,7 @@ module Mixlib config_opts[:proc] ||= nil config_opts[:show_options] ||= false config_opts[:exit] ||= nil + config_opts[:in] ||= nil if config_opts.has_key?(:default) defaults_container[config_key] = config_opts[:default] @@ -233,6 +234,19 @@ module Mixlib puts @opt_parser exit 2 end + if opt_value[:in] + unless opt_value[:in].kind_of?(Array) + raise(ArgumentError, "Options config key :in must recieve an Array") + puts @opt_parser + exit 2 + end + if !opt_value[:in].include?(config[opt_key]) + reqarg = opt_value[:short] || opt_value[:long] + puts "#{reqarg}: #{config[opt_key]} is not included in the list #{opt_value[:in]}" + puts @opt_parser + exit 2 + end + end end argv @@ -243,10 +257,10 @@ module Mixlib arguments << opt_setting[:short] if opt_setting.has_key?(:short) arguments << opt_setting[:long] if opt_setting.has_key?(:long) - if opt_setting.has_key?(:description) description = opt_setting[:description] description << " (required)" if opt_setting[:required] + description << " (included in #{opt_setting[:in]})" if opt_setting[:in] arguments << description end diff --git a/spec/mixlib/cli_spec.rb b/spec/mixlib/cli_spec.rb index 39740b4..e2fa647 100644 --- a/spec/mixlib/cli_spec.rb +++ b/spec/mixlib/cli_spec.rb @@ -79,7 +79,8 @@ describe Mixlib::CLI do :required => false, :proc => nil, :show_options => false, - :exit => nil + :exit => nil, + :in => nil } } end @@ -174,6 +175,32 @@ describe Mixlib::CLI do @cli = TestCLI.new lambda { @cli.parse_options([]) }.should raise_error(SystemExit) end + + it "should exit if option is not included in the list" do + TestCLI.option(:inclusion, :short => "-i val", :in => ['one', 'two']) + @cli = TestCLI.new + lambda { @cli.parse_options(['-i', 'three']) }.should raise_error(SystemExit) + end + + it "should exit if options key :in is not an array" do + TestCLI.option(:inclusion, :short => "-i val", :in => 'foo') + @cli = TestCLI.new + lambda { @cli.parse_options(['-i', 'three']) }.should raise_error(ArgumentError) + end + + it "should not exit if option is included in the list" do + TestCLI.option(:inclusion, :short => "-i val", :in => ['one', 'two']) + @cli = TestCLI.new + @cli.parse_options(['-i', 'one']) + @cli.config[:inclusion].should == 'one' + end + + it "should change description if :in key is specified" do + TestCLI.option(:inclusion, :short => "-i val", :in => ['one', 'two'], :description => 'desc') + @cli = TestCLI.new + @cli.parse_options(['-i', 'one']) + @cli.options[:inclusion][:description].should == "desc (included in [\"one\", \"two\"])" + end it "should not exit if a required option is specified" do TestCLI.option(:require_me, :short => "-r", :boolean => true, :required => true) |