summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordanielsdeleo <dan@opscode.com>2013-01-09 11:22:44 -0800
committerdanielsdeleo <dan@opscode.com>2013-01-09 11:22:44 -0800
commit6a2890e2df4a547b778c51362e7d66a2c6c56919 (patch)
treec51ede25efcb8fe914f15f1ff3c04a2a3ca089a9
parentdefe2e868519cf8471e4df0082241ffbdc87fb45 (diff)
downloadmixlib-cli-6a2890e2df4a547b778c51362e7d66a2c6c56919.tar.gz
cleanup whitespace
-rw-r--r--lib/mixlib/cli.rb64
1 files changed, 32 insertions, 32 deletions
diff --git a/lib/mixlib/cli.rb b/lib/mixlib/cli.rb
index 39edd7f..4b8275f 100644
--- a/lib/mixlib/cli.rb
+++ b/lib/mixlib/cli.rb
@@ -6,9 +6,9 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
-#
+#
# http://www.apache.org/licenses/LICENSE-2.0
-#
+#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,7 +20,7 @@ require 'optparse'
module Mixlib
module CLI
- module ClassMethods
+ module ClassMethods
# Add a command line option.
#
# === Parameters
@@ -33,7 +33,7 @@ module Mixlib
raise(ArgumentError, "Option name must be a symbol") unless name.kind_of?(Symbol)
@options[name.to_sym] = args
end
-
+
# Get the hash of current options.
#
# === Returns
@@ -42,7 +42,7 @@ module Mixlib
@options ||= {}
@options
end
-
+
# Set the current options hash
#
# === Parameters
@@ -54,13 +54,13 @@ module Mixlib
raise(ArgumentError, "Options must recieve a hash") unless val.kind_of?(Hash)
@options = val
end
-
+
# Change the banner. Defaults to:
# Usage: #{0} (options)
#
# === Parameters
# bstring<String>:: The string to set the banner to
- #
+ #
# === Returns
# @banner<String>:: The current banner
def banner(bstring=nil)
@@ -72,9 +72,9 @@ module Mixlib
end
end
end
-
+
attr_accessor :options, :config, :banner, :opt_parser
-
+
# Create a new Mixlib::CLI class. If you override this, make sure you call super!
#
# === Parameters
@@ -85,14 +85,14 @@ module Mixlib
def initialize(*args)
@options = Hash.new
@config = Hash.new
-
+
# Set the banner
@banner = self.class.banner
-
+
# Dupe the class options for this instance
klass_options = self.class.options
klass_options.keys.inject(@options) { |memo, key| memo[key] = klass_options[key].dup; memo }
-
+
# Set the default configuration values for this instance
@options.each do |config_key, config_opts|
config_opts[:on] ||= :on
@@ -101,16 +101,16 @@ module Mixlib
config_opts[:proc] ||= nil
config_opts[:show_options] ||= false
config_opts[:exit] ||= nil
-
+
if config_opts.has_key?(:default)
@config[config_key] = config_opts[:default]
end
end
-
+
super(*args)
end
-
- # Parses an array, by default ARGV, for command line options (as configured at
+
+ # Parses an array, by default ARGV, for command line options (as configured at
# the class level).
# === Parameters
# argv<Array>:: The array of arguments to parse; defaults to ARGV
@@ -119,14 +119,14 @@ 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 = OptionParser.new do |opts|
# Set the banner
- opts.banner = banner
-
+ opts.banner = banner
+
# Create new options
- options.sort { |a, b| a[0].to_s <=> b[0].to_s }.each do |opt_key, opt_val|
+ options.sort { |a, b| a[0].to_s <=> b[0].to_s }.each do |opt_key, opt_val|
opt_args = build_option_arguments(opt_val)
-
+
opt_method = case opt_val[:on]
when :on
:on
@@ -137,14 +137,14 @@ module Mixlib
else
raise ArgumentError, "You must pass :on, :tail, or :head to :on"
end
-
+
parse_block =
Proc.new() do |c|
config[opt_key] = (opt_val[:proc] && opt_val[:proc].call(c)) || c
puts opts if opt_val[:show_options]
exit opt_val[:exit] if opt_val[:exit]
end
-
+
full_opt = [ opt_method ]
opt_args.inject(full_opt) { |memo, arg| memo << arg; memo }
full_opt << parse_block
@@ -152,7 +152,7 @@ module Mixlib
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)
@@ -162,28 +162,28 @@ module Mixlib
exit 2
end
end
-
+
argv
end
-
- def build_option_arguments(opt_setting)
+
+ def build_option_arguments(opt_setting)
arguments = Array.new
-
+
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]
arguments << description
end
-
+
arguments
end
-
+
def self.included(receiver)
receiver.extend(Mixlib::CLI::ClassMethods)
end
-
+
end
end