diff options
-rw-r--r-- | .travis.yml | 7 | ||||
-rw-r--r-- | CHANGELOG.md | 10 | ||||
-rw-r--r-- | Gemfile | 3 | ||||
-rw-r--r-- | README.rdoc | 7 | ||||
-rw-r--r-- | Rakefile | 27 | ||||
-rw-r--r-- | lib/mixlib/cli.rb | 71 | ||||
-rw-r--r-- | lib/mixlib/cli/version.rb | 2 | ||||
-rw-r--r-- | mixlib-cli.gemspec | 3 | ||||
-rw-r--r-- | spec/mixlib/cli_spec.rb | 16 |
9 files changed, 82 insertions, 64 deletions
diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f019556 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,7 @@ +rvm: + - 1.8.7 + - 1.9.3 + - 2.0.0 + +script: bundle exec rake spec + diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..db855dd --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,10 @@ +# mixlib-cli Changelog + +## Unreleased + +* [**C Chamblin**:](https://github.com/chamblin) + Documented CLI arguments. +* [**Kenichi Kamiya**:](https://github.com/kachick) + Fixed ruby-warning "instance variable @{ivar} not initialized". + +## Last Release: 1.4.0 (12/05/2013) @@ -0,0 +1,3 @@ +source "https://rubygems.org" + +gemspec diff --git a/README.rdoc b/README.rdoc index f5441d1..26fe85c 100644 --- a/README.rdoc +++ b/README.rdoc @@ -74,6 +74,13 @@ Available arguments to 'option': :proc:: If set, the configuration value will be set to the return value of this proc. :in:: An array contain 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). + + # ARGV = [ '-c', 'foo.rb', '-l', 'debug', 'file1', 'file2', 'file3' ] + cli = MyCLI.new + cli.parse_options + cli.cli_arguments # [ 'file1', 'file2', 'file3' ] + === New in 1.2.2 :required works, and we now support Ruby-style boolean option negation @@ -1,7 +1,10 @@ +require 'bundler' require 'rubygems' require 'rubygems/package_task' -require 'rspec/core/rake_task' require 'rdoc/task' +require 'rspec/core/rake_task' + +Bundler::GemHelper.install_tasks task :default => :spec @@ -12,31 +15,9 @@ end gem_spec = eval(File.read("mixlib-cli.gemspec")) -Gem::PackageTask.new(gem_spec) do |pkg| - pkg.gem_spec = gem_spec -end - -desc "install the gem locally" -task :install => [:package] do - sh %{gem install pkg/#{gem_spec.name}-#{gem_spec.version}} -end - -desc "create a gemspec file" -task :make_spec do - File.open("#{gem_spec.name}.gemspec", "w") do |file| - file.puts spec.to_ruby - end -end - -desc "remove build files" -task :clean do - sh %Q{ rm -f pkg/*.gem } -end - RDoc::Task.new do |rdoc| rdoc.rdoc_dir = 'rdoc' rdoc.title = "mixlib-cli #{gem_spec.version}" rdoc.rdoc_files.include('README*') rdoc.rdoc_files.include('lib/**/*.rb') end - 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 diff --git a/mixlib-cli.gemspec b/mixlib-cli.gemspec index 60cfd5d..fd63ea3 100644 --- a/mixlib-cli.gemspec +++ b/mixlib-cli.gemspec @@ -15,6 +15,9 @@ Gem::Specification.new do |s| # Uncomment this to add a dependency #s.add_dependency "mixlib-log" + s.add_development_dependency 'rake' + s.add_development_dependency 'rspec' + s.add_development_dependency 'rdoc' s.require_path = 'lib' s.files = %w(LICENSE README.rdoc Rakefile NOTICE) + Dir.glob("{lib,spec}/**/*") diff --git a/spec/mixlib/cli_spec.rb b/spec/mixlib/cli_spec.rb index 550b26f..3399369 100644 --- a/spec/mixlib/cli_spec.rb +++ b/spec/mixlib/cli_spec.rb @@ -92,16 +92,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 @@ -109,7 +108,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 @@ -117,7 +115,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 @@ -126,13 +123,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 @@ -233,10 +232,11 @@ describe Mixlib::CLI do ARGV.replace argv_old end - it "should return any un-parsed elements" do + it "should preserve and return any un-parsed elements" do TestCLI.option(:party, :short => "-p LOCATION") @cli = TestCLI.new - @cli.parse_options([ '-p', 'opscode', 'hard' ]).should == ['hard'] + @cli.parse_options([ 'easy', '-p', 'opscode', 'hard' ]).should == ['easy', 'hard'] + @cli.cli_arguments.should == ['easy', 'hard'] end end end |