module RHC::Commands

Public Class Methods

add(opts) click to toggle source
# File lib/rhc/commands.rb, line 184
def self.add(opts)
  commands[opts[:name]] = opts
end
deprecated!() click to toggle source
# File lib/rhc/commands.rb, line 191
def self.deprecated!
  instance = Commander::Runner.instance
  command_name = instance.command_name_from_args
  command = instance.active_command

  if new_cmd = command.deprecated(command_name)
    new_cmd = "rhc #{command.name}" if new_cmd == true
    RHC::Helpers.deprecated_command new_cmd
  end
end
global_option(*args, &block) click to toggle source
# File lib/rhc/commands.rb, line 187
def self.global_option(*args, &block)
  global_options << [args.freeze, block]
end
load() click to toggle source
# File lib/rhc/commands.rb, line 178
def self.load
  Dir[File.join(File.dirname(__FILE__), "commands", "*.rb")].each do |file|
    require file
  end
  self
end
needs_configuration!(cmd, options, config) click to toggle source
# File lib/rhc/commands.rb, line 202
def self.needs_configuration!(cmd, options, config)
  if not (cmd.class.suppress_wizard? or
          options.noprompt or
          options.help or
          config.has_local_config? or
          config.has_opts_config?)

    $stderr.puts RHC::Helpers.color("You have not yet configured the OpenShift client tools. Please run 'rhc setup'.", :yellow)
  end
end
to_commander(instance=Commander::Runner.instance) click to toggle source
# File lib/rhc/commands.rb, line 213
def self.to_commander(instance=Commander::Runner.instance)
  global_options.each do |args, block|
    args = args.dup
    opts = (args.pop if Hash === args.last) || {}
    option = instance.global_option(*args, &block).last
    option.merge!(opts)
  end
  commands.each_pair do |name, opts|
    name = Array(name)
    names = [name.reverse.join('-'), name.join(' ')] if name.length > 1
    name = name.join('-')

    instance.command name do |c|
      c.description = opts[:description]
      c.summary = opts[:summary]
      c.syntax = opts[:syntax]
      c.default_action = opts[:default]

      c.info = opts

      (options_metadata = Array(opts[:options])).each do |o|
        option_data = [o[:switches], o[:type], o[:description], o.slice(:optional, :default, :hide, :covered_by)].compact.flatten(1)
        c.option *option_data
        o[:arg] = Commander::Runner.switch_to_sym(Array(o[:switches]).last)
      end

      (args_metadata = Array(opts[:args])).each do |meta|
        switches = meta[:switches]
        unless switches.blank?
          switches = switches.dup
          switches << meta[:description]
          switches << meta.slice(:optional, :default, :hide, :covered_by, :allow_nil)
          c.option *switches
        end
      end

      Array(opts[:aliases]).each do |a|
        action = Array(a[:action])
        [' ', '-'].each do |s|
          cmd = action.join(s)
          instance.alias_command cmd, name
        end
      end

      if names
        names.each{ |alt| instance.alias_command alt, name }
      else
        c.root = true
      end

      c.when_called do |args, options|
        deprecated!

        config = c.instance_variable_get(:@config)

        cmd = opts[:class].new
        cmd.options = options
        cmd.config = config

        args = fill_arguments(cmd, options, args_metadata, options_metadata, args)
        needs_configuration!(cmd, options, config)

        return execute(cmd, :help, args) unless opts[:method]
        execute(cmd, opts[:method], args)
      end
    end
  end
  self
end

Protected Class Methods

argument_to_slot(options, available, arg) click to toggle source
# File lib/rhc/commands.rb, line 346
def self.argument_to_slot(options, available, arg)
  if Array(arg[:covered_by]).any?{ |k| !options.__explicit__[k].nil? }
    return nil
  end

  option = arg[:option_symbol]
  value = options.__explicit__[option] if option
  if value.nil?
    value =
      if arg[:type] == :list
        take_leading_list(available)
      else
        v = available.shift
        if v == '--'
          v = nil
        else
          available.shift if available.first == '--'
        end
        v
      end
  end

  value = options[option] if option && (value.nil? || (value.is_a?(Array) && value.blank?))
  if arg[:type] == :list
    value = Array(value)
  end
  options[option] = value if option && !value.nil?

  value
end
commands() click to toggle source
# File lib/rhc/commands.rb, line 389
def self.commands
  @commands ||= {}
end
execute(cmd, method, args) click to toggle source
# File lib/rhc/commands.rb, line 284
def self.execute(cmd, method, args)
  cmd.send(method, *args)
end
fill_arguments(cmd, options, args, opts, arguments) click to toggle source
# File lib/rhc/commands.rb, line 288
def self.fill_arguments(cmd, options, args, opts, arguments)
  # process defaults
  defaults = {}
  covers = {}
  (opts + args).each do |option_meta|
    arg = option_meta[:option_symbol] || option_meta[:name] || option_meta[:arg] or next
    if arg && option_meta[:type] != :list && options[arg].is_a?(Array)
      options[arg] = options[arg].last
    end
    Array(option_meta[:covered_by]).each{ |sym| (covers[sym] ||= []) << arg }

    case v = option_meta[:default]
    when Symbol
      cmd.send(v, defaults, arg)
    when Proc
      v.call(defaults, arg)
    when nil
    else
      defaults[arg] = v
    end
  end
  options.default(defaults)

  # process required options
  opts.each do |option_meta|
    raise ArgumentError.new("Missing required option '#{option_meta[:arg]}'.") if option_meta[:required] && options[option_meta[:arg]].nil?
  end

  slots = Array.new(args.count)
  available = arguments.dup

  args.each_with_index do |arg, i|
    value = argument_to_slot(options, available, arg)

    if value.nil?
      if arg[:allow_nil] != true && !arg[:optional]
        raise ArgumentError, "Missing required argument '#{arg[:name]}'."
      end
    end

    slots[i] = value
  end

  raise ArgumentError, "Too many arguments passed in: #{available.reverse.join(" ")}" unless available.empty?

  # reset covered arguments
  options.__explicit__.keys.each do |k|
    if covered = covers[k]
      covered.each do |sym|
        raise ArgumentError, "The options '#{sym}' and '#{k}' cannot both be provided" unless options.__explicit__[sym].nil?
        options[sym] = nil
      end
    end
  end

  slots
end
global_options() click to toggle source
# File lib/rhc/commands.rb, line 392
def self.global_options
  @options ||= []
end
take_leading_list(available) click to toggle source
# File lib/rhc/commands.rb, line 377
def self.take_leading_list(available)
  if i = available.index('--')
    left = available.shift(i)
    available.shift
    left
  else
    left = available.dup
    available.clear
    left
  end
end