diff options
author | Mehrez Alachheb <lachheb.mehrez@gmail.com> | 2014-07-08 14:14:52 +0200 |
---|---|---|
committer | Mehrez Alachheb <lachheb.mehrez@gmail.com> | 2014-07-08 14:14:52 +0200 |
commit | 3a3ba3b332b853bf9c628992dae8494121988de8 (patch) | |
tree | 878b4f2a55ac17bdc2c960718975025cb6102673 /lib | |
parent | 0c00fdb5775cd2e5cd0d6d70ec73fc2133b72956 (diff) | |
parent | 3058c02fa5534bd1f15f0602b78ed77a4b48f9f6 (diff) | |
download | mixlib-cli-3a3ba3b332b853bf9c628992dae8494121988de8.tar.gz |
Merge branch 'master' into malachheb-1.4.0
Conflicts:
lib/mixlib/cli.rb
Diffstat (limited to 'lib')
-rw-r--r-- | lib/mixlib/cli.rb | 71 | ||||
-rw-r--r-- | lib/mixlib/cli/version.rb | 2 |
2 files changed, 40 insertions, 33 deletions
diff --git a/lib/mixlib/cli.rb b/lib/mixlib/cli.rb index 90adc49..5e1fa7c 100644 --- a/lib/mixlib/cli.rb +++ b/lib/mixlib/cli.rb @@ -47,7 +47,7 @@ module Mixlib end def use_separate_defaults? - @separate_default_options || false + @separate_default_options ||= false end # Add a command line option. @@ -128,16 +128,13 @@ module Mixlib # hash. attr_accessor :default_config + # Any arguments which were not parsed and placed in "config"--the leftovers. + attr_accessor :cli_arguments + # Banner for the option parser. If the option parser is printed, e.g., by # `puts opt_parser`, this string will be used as the first line. attr_accessor :banner - # The option parser generated from the mixlib-cli DSL. Set to nil on - # initialize; when #parse_options is called +opt_parser+ is set to an - # instance of OptionParser. +opt_parser+ can be used to print a help - # message including the banner and any CLI options via `puts opt_parser`. - attr_accessor :opt_parser - # Create a new Mixlib::CLI class. If you override this, make sure you call super! # # === Parameters @@ -192,7 +189,41 @@ module Mixlib # argv<Array>:: Returns any un-parsed elements. def parse_options(argv=ARGV) argv = argv.dup - @opt_parser = OptionParser.new do |opts| + opt_parser.parse!(argv) + + # Deal with any required values + options.each do |opt_key, opt_value| + if opt_value[:required] && !config.has_key?(opt_key) + reqarg = opt_value[:short] || opt_value[:long] + puts "You must supply #{reqarg}!" + puts @opt_parser + exit 2 + end + if opt_value[:in] + unless opt_value[:in].kind_of?(Array) + raise(ArgumentError, "Options config key :in must receive an Array") + 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].join("', '")}'] " + puts @opt_parser + exit 2 + end + end + end + + @cli_arguments = argv + argv + end + + + # The option parser generated from the mixlib-cli DSL. +opt_parser+ can be + # used to print a help message including the banner and any CLI options via + # `puts opt_parser`. + # === Returns + # opt_parser<OptionParser>:: The option parser object. + def opt_parser + @opt_parser ||= OptionParser.new do |opts| # Set the banner opts.banner = banner @@ -224,30 +255,6 @@ module Mixlib opts.send(*full_opt) end end - @opt_parser.parse!(argv) - - # Deal with any required values - options.each do |opt_key, opt_value| - if opt_value[:required] && !config.has_key?(opt_key) - reqarg = opt_value[:short] || opt_value[:long] - puts "You must supply #{reqarg}!" - puts @opt_parser - exit 2 - end - if opt_value[:in] - unless opt_value[:in].kind_of?(Array) - raise(ArgumentError, "Options config key :in must receive an Array") - 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].join("', '")}'] " - puts @opt_parser - exit 2 - end - end - end - - argv end def build_option_arguments(opt_setting) diff --git a/lib/mixlib/cli/version.rb b/lib/mixlib/cli/version.rb index 63fd6f9..21cac2e 100644 --- a/lib/mixlib/cli/version.rb +++ b/lib/mixlib/cli/version.rb @@ -1,6 +1,6 @@ module Mixlib module CLI - VERSION = "1.3.0" + VERSION = "1.5.0" end end |