frc-frecon/frecon

View on GitHub
bin/frecon

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby

# bin/frecon
#
# Copyright (C) 2014 Christopher Cooper, Sam Craig, Tiger Huang, Vincent Mai, Sam Mercier, and Kristofer Rye
#
# This file is part of FReCon, an API for scouting at FRC Competitions, which is
# licensed under the MIT license.  You should have received a copy of the MIT
# license with this program.  If not, please see
# <http://opensource.org/licenses/MIT>.

# Include rel-path ../lib/ in the $LOAD_PATH if it's not there already.
lib_directory = File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))
$LOAD_PATH.unshift(lib_directory) unless $LOAD_PATH.map { |directory| File.expand_path(directory) }.include?(lib_directory)

require "frecon"

require "optparse"

options = {
    mode: :server,
    environment: FReCon::ENVIRONMENT
}

optparse = OptionParser.new do |opts|
    opts.banner = <<EOF
Usage: #{$0} [OPTIONS] [MODE]

If MODE is not present, FReCon starts the Server.  If MODE is present, starts
with "c", and contains any of the letters of the word "console" in order, FReCon
instead starts the Console.
EOF

    opts.separator ""
    opts.separator "Server OPTIONS:"

    opts.on("-o", "--host HOST", "Bind to HOST (default: #{options[:environment].server["host"]})") do |host|
        options[:environment].server["host"] = host
    end

    opts.on("-p", "--port PORT", "Bind to port PORT (default: #{options[:environment].server["port"]})") do |port|
        options[:environment].server["port"] = port
    end

    opts.separator ""
    opts.separator "General OPTIONS:"

    opts.on("-E", "--env ENVIRONMENT", "Run in the ENVIRONMENT environment (default: #{options[:environment].variable})") do |environment|
        options[:environment].variable = environment.to_sym
    end

    opts.on("-h", "--help", "Print this usage message.") do
        puts opts
        exit
    end
end

optparse.parse!

ARGV.select do |arg|
    case arg
    when /^co?n?s?o?l?e?$/i
        options[:mode] = :console
    end
end

case options[:mode]
when :server
    FReCon::Server.start
when :console
    FReCon::Console.start
end