summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/mixlib/cli.rb45
-rw-r--r--spec/mixlib/cli_spec.rb11
2 files changed, 29 insertions, 27 deletions
diff --git a/lib/mixlib/cli.rb b/lib/mixlib/cli.rb
index aa0b70b..e51e558 100644
--- a/lib/mixlib/cli.rb
+++ b/lib/mixlib/cli.rb
@@ -135,12 +135,6 @@ module Mixlib
# `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
@@ -194,7 +188,30 @@ 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
+ 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
@@ -226,20 +243,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
- end
-
- @cli_arguments = argv
- argv
end
def build_option_arguments(opt_setting)
diff --git a/spec/mixlib/cli_spec.rb b/spec/mixlib/cli_spec.rb
index 685af4e..fba376c 100644
--- a/spec/mixlib/cli_spec.rb
+++ b/spec/mixlib/cli_spec.rb
@@ -91,16 +91,15 @@ describe Mixlib::CLI do
end
end
- describe "parse_options" do
+ describe "opt_parser" do
+
it "should set the banner in opt_parse" do
- @cli.parse_options([])
@cli.opt_parser.banner.should == @cli.banner
end
it "should present the arguments in the banner" do
TestCLI.option(:config_file, :short => "-l LOG")
@cli = TestCLI.new
- @cli.parse_options([])
@cli.opt_parser.to_s.should =~ /-l LOG/
end
@@ -108,7 +107,6 @@ describe Mixlib::CLI do
TestCLI.option(:config_file, :short => "-l LOG")
TestCLI.option(:help, :short => "-h", :boolean => true, :on => :tail)
@cli = TestCLI.new
- @cli.parse_options([])
@cli.opt_parser.to_s.split("\n").last.should =~ /-h/
end
@@ -116,7 +114,6 @@ describe Mixlib::CLI do
TestCLI.option(:config_file, :short => "-l LOG")
TestCLI.option(:help, :short => "-h", :boolean => true, :on => :head)
@cli = TestCLI.new
- @cli.parse_options([])
@cli.opt_parser.to_s.split("\n")[1].should =~ /-h/
end
@@ -125,13 +122,15 @@ describe Mixlib::CLI do
TestCLI.option(:beta, :short => "-b BETA")
TestCLI.option(:zeta, :short => "-z ZETA")
@cli = TestCLI.new
- @cli.parse_options([])
output_lines = @cli.opt_parser.to_s.split("\n")
output_lines[1].should =~ /-a ALPHA/
output_lines[2].should =~ /-b BETA/
output_lines[3].should =~ /-z ZETA/
end
+ end
+
+ describe "parse_options" do
it "should set the corresponding config value for non-boolean arguments" do
TestCLI.option(:config_file, :short => "-c CONFIG")
@cli = TestCLI.new