module Mixlib::CLI

def self.included(receiver)

def self.included(receiver)
  receiver.extend(Mixlib::CLI::ClassMethods)
  receiver.extend(Mixlib::CLI::InheritMethods)
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]
    description << " (included in ['#{opt_setting[:in].join("', '")}'])" if opt_setting[:in]
    arguments << description
  end
  arguments
end

def initialize(*args)

object:: Returns an instance of whatever you wanted :)
=== Returns

*args:: The array of arguments passed to the initializer
=== Parameters

Create a new Mixlib::CLI class. If you override this, make sure you call super!
def initialize(*args)
  @options = Hash.new
  @config  = Hash.new
  @default_config = Hash.new
  @opt_parser = nil
  # 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 }
  # If use_separate_defaults? is on, default values go in @default_config
  defaults_container = if self.class.use_separate_defaults?
                         @default_config
                       else
                         @config
                       end
  # Set the default configuration values for this instance
  @options.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
    config_opts[:in] ||= nil
    if config_opts.has_key?(:default)
      defaults_container[config_key] = config_opts[:default]
    end
  end
  super(*args)
end

def opt_parser

opt_parser:: The option parser object.
=== Returns
`puts opt_parser`.
used to print a help message including the banner and any CLI options via
The option parser generated from the mixlib-cli DSL. +opt_parser+ can be
def opt_parser
  @opt_parser ||= OptionParser.new do |opts|
    # Set the 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|
      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 =
        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
      opts.send(*full_opt)
    end
  end
end

def parse_options(argv = ARGV)

argv:: Returns any un-parsed elements.
=== Returns

argv:: The array of arguments to parse; defaults to ARGV
=== Parameters
the class level).
Parses an array, by default ARGV, for command line options (as configured at
def parse_options(argv = ARGV)
  argv = argv.dup
  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