class RXCode::Command

Attributes

arguments[R]
ARGUMENTS ==================================================================================================
err[RW]
input[RW]
options[R]
OPTIONS ====================================================================================================
output[RW]
STREAMS ====================================================================================================

Public Class Methods

command_class_for_name(command_name) click to toggle source
# File lib/rxcode/command.rb, line 80
def self.command_class_for_name(command_name)
  self.commands[command_name]
end
command_names() click to toggle source
# File lib/rxcode/command.rb, line 76
def self.command_names
  self.commands.keys
end
commands() click to toggle source
# File lib/rxcode/command.rb, line 72
def self.commands
  @commands ||= {}
end
display_name() click to toggle source
# File lib/rxcode/command.rb, line 84
def self.display_name
  self.name.split('::').last.
    gsub(/([A-Z]+)/) { |uppercase| '_' + uppercase.downcase }.
    gsub(/^_/, '').
    gsub(/[^a-z]_/) { |str| str.gsub(/_$/, '') }
end
inherited(subclass) click to toggle source
COMMAND REGISTRATION =======================================================================================

Commands are automatically registered when they subclass Command.

# File lib/rxcode/command.rb, line 68
def self.inherited(subclass)
  self.commands[subclass.display_name] = subclass
end
new(*args) { |self| ... } click to toggle source

Initializes the command with a set of options and/or arguments. The formal way to initialize a command looks like this, with options followed by arguments:

Command.new({ :option => 'value' }, [ 'first arg', 'second arg' ])

This is the way it’s done using parsed command-line arguments. However, this isn’t as intuitive when creating commands programmatically, so the more rubyish constructor signature is also supported:

Command.new('first arg', 'second arg', :option => 'value')
Command.new('first arg', 'second arg')
# File lib/rxcode/command.rb, line 17
def initialize(*args)
  if args.first.is_a?(Hash)
    @options = args.shift
    @arguments = args.shift
  else
    if args.last.is_a?(Hash)
      @options = args.pop
    else
      @options = {}
    end
    
    @arguments = args
  end
  
  yield self if block_given?
end
new_command_option_parser() click to toggle source
# File lib/rxcode/command.rb, line 173
def self.new_command_option_parser
  Trollop::Parser.new
end
new_global_option_parser() click to toggle source

—– COMMAND LINE PARSER —————————————————————————————-

# File lib/rxcode/command.rb, line 154
    def self.new_global_option_parser
      
      Trollop::Parser.new do
        version "rxcode #{RXCode::VERSION}"
        banner <<-END
A utility for manipulating XCode projects.

Usage:
#{$0} [global options] command [command options]

Available Commands: #{::RXCode::Command.command_names.sort.join(', ')}

Global Options:
END
        stop_on_unknown
      end
      
    end
run!(args = ARGV) click to toggle source
# File lib/rxcode/command.rb, line 101
def self.run!(args = ARGV)
  require 'trollop'
  
  global_options = {}
  command_name = nil
  command_arguments = []
  
  parser = self.new_global_option_parser
  Trollop::with_standard_exception_handling parser do
    global_options = parser.parse(args)
    command_arguments = parser.leftovers
    command_name = command_arguments.shift
    
    if command_name.nil?
      
      if (global_options.keys - [:version, :help]).empty?
        raise Trollop::HelpNeeded
      else
        parser.die("No command given", nil)
      end
      
    elsif !RXCode::Command.command_names.include?(command_name)
      
      parser.die("Unknown command (#{command_name.inspect})", nil)
    
    end
    
  end
  
  run_command(command_name, command_arguments, global_options)
end
run_command(command_name, command_args = [], global_options = {}) click to toggle source
# File lib/rxcode/command.rb, line 133
def self.run_command(command_name, command_args = [], global_options = {})
  if command_class = self.command_class_for_name(command_name)
    parser = command_class.new_command_option_parser
    
    Trollop::with_standard_exception_handling parser do
      
      command_options = parser.parse(command_args)
      command_arguments = parser.leftovers
      
      command = command_class.new(command_options, command_arguments)
      command.run!
    end
    
  else
    raise "Invalid Command: #{command_name.inspect}"
  end
  
end

Public Instance Methods

env() click to toggle source
XCODE ENVIRONMENT ==========================================================================================
# File lib/rxcode/command.rb, line 61
def env
  @env ||= RXCode::Environment.new(Dir.pwd)
end
run!() click to toggle source
COMMAND RUNNING ============================================================================================
# File lib/rxcode/command.rb, line 93
def run!
  if self.class == Command
    raise "#{self.class.name} is an abstract class."
  else
    raise "#{Command.name}#run! is abstract and should be overridden"
  end
end