module Anyplayer::CommandLine

Constants

COMMANDS

Public Instance Methods

parse(argv) click to toggle source
# File lib/anyplayer/command_line.rb, line 26
def parse(argv)
  # Create a selector
  selector = Anyplayer::Selector.new
  if argv.first == "-v"
    selector.verbose = true
    argv.shift

    $stderr.puts "anyplayer v#{Anyplayer::VERSION}"
  end

  # Find the current player
  player = selector.player
  abort "Error: no player connected.\n" + selector.errors.join("\n") if !player

  # Call a command
  if argv.empty?
    usage(player)
  else
    command(player, argv.first)
  end
end

Private Instance Methods

call(player, command) click to toggle source

Call the method on the player Tries the boolean method as well

# File lib/anyplayer/command_line.rb, line 64
def call(player, command)
  [command, "#{command}?"].each do |method|
    method = method.to_sym
    return player.send(method) if player.respond_to?(method)
  end

  :undefined_command
end
command(player, command) click to toggle source

Call

# File lib/anyplayer/command_line.rb, line 51
def command(player, command)
  response = call(player, command)

  case response
  when :undefined_command then usage(player)
  when true then exit(0)
  when false then exit(1)
  when String, Numeric then puts response
  end
end
usage(player) click to toggle source
# File lib/anyplayer/command_line.rb, line 73
  def usage(player)
    $stderr.puts <<USAGE
Usage: anyplayer [-v] [command]

Where command is one of: #{COMMANDS.join(', ')}.

Player connected: #{player.name}.
USAGE
    exit(1)
  end