Module: Plist4r::Mixlib::CLI
- Included in:
- CLI
- Defined in:
- lib/plist4r/mixin/mixlib_cli.rb
Overview
Author:: Adam Jacob (<adam@opscode.com>)
Copyright:: Copyright (c) 2008 Opscode, Inc.
License:: Apache License, Version 2.0
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.
See the License for the specific language governing and
limitations under the License.
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#banner ⇒ Object
Returns the value of attribute banner.
-
#config ⇒ Object
Returns the value of attribute config.
-
#opt_parser ⇒ Object
Returns the value of attribute opt_parser.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #build_option_arguments(opt_setting) ⇒ Object
-
#initialize(*args) ⇒ Object
Create a new Mixlib::CLI class.
-
#parse_options(argv = ARGV) ⇒ Object
Parses an array, by default ARGV, for command line options (as configured at the class level).
Instance Attribute Details
#banner ⇒ Object
Returns the value of attribute banner.
77 78 79 |
# File 'lib/plist4r/mixin/mixlib_cli.rb', line 77 def end |
#config ⇒ Object
Returns the value of attribute config.
77 78 79 |
# File 'lib/plist4r/mixin/mixlib_cli.rb', line 77 def config @config end |
#opt_parser ⇒ Object
Returns the value of attribute opt_parser.
77 78 79 |
# File 'lib/plist4r/mixin/mixlib_cli.rb', line 77 def opt_parser @opt_parser end |
#options ⇒ Object
Returns the value of attribute options.
77 78 79 |
# File 'lib/plist4r/mixin/mixlib_cli.rb', line 77 def end |
Instance Method Details
#build_option_arguments(opt_setting) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/plist4r/mixin/mixlib_cli.rb', line 178 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 |
#initialize(*args) ⇒ Object
Create a new Mixlib::CLI class. If you override this, make sure you call super!
Parameters
- *args<Array>
-
The array of arguments passed to the initializer
Returns
- object<Mixlib::Config>
-
Returns an instance of whatever you wanted :)
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/plist4r/mixin/mixlib_cli.rb', line 86 def initialize(*args) = Hash.new @config = Hash.new # Set the banner = self.class. # Dupe the class options for this instance = self.class. .keys.inject() { |memo, key| memo[key] = [key].dup; memo } # Set the default configuration values for this instance .each do |config_key, config_opts| config_opts[:on] ||= :on config_opts[:boolean] ||= false config_opts[:required] ||= false 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 |
#parse_options(argv = ARGV) ⇒ Object
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
Returns
- argv<Array>
-
Returns any un-parsed elements.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/plist4r/mixin/mixlib_cli.rb', line 121 def (argv=ARGV) argv = argv.dup @opt_parser = OptionParser.new do |opts| # Set the banner opts. = # Create new 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 when :tail :on_tail when :head :on_head else raise ArgumentError, "You must pass :on, :tail, or :head to :on" end parse_block = case opt_val[:boolean] when true Proc.new() do config[opt_key] = (opt_val[:proc] && opt_val[:proc].call(true)) || true puts opts if opt_val[:show_options] exit opt_val[:exit] if opt_val[:exit] end when false 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 end full_opt = [ opt_method ] opt_args.inject(full_opt) { |memo, arg| memo << arg; memo } full_opt << parse_block opts.send(*full_opt) end end @opt_parser.parse!(argv) # Deal with any required values .each do |opt_key, opt_value| if opt_value[:required] && ! config[opt_key] reqarg = opt_value[:short] || opt_value[:long] puts "You must supply #{reqarg}!" puts @opt_parser exit 2 end end argv end |