#!/usr/bin/env ruby require 'gyoza-languages' def usage binary_name = GyozaLanguages::SPEC_NAME env_var_name = GyozaLanguages::REPOSITORIES_DIRECTORY_ENV_NAME puts( "#{binary_name} #{GyozaLanguages::VERSION} #{GyozaLanguages::SPEC_DESCRIPTION}. Usage: #{binary_name} [options] -p, --port PORT Starts the server with the specified port. -d, --directory DIRECTORY Manually specifies the repositories directory. -h, --help Show this message If the -d argument is not specified, a #{env_var_name} environment variable will be necessary.") end def parse_arguments(parsed, args) if args.length == 0 return end arg = args[0] if arg == '-h' || arg == '--help' usage exit 0 end if args.length == 1 puts("You did not specify enough arguments for argument #{arg}") usage exit 1 end arg = arg.downcase if arg == '-p' arg = '--port' elsif arg == '-d' arg = '--directory' else unless arg == '--port' || arg == '--directory' puts("Invalid argument: #{arg}") exit 2 end end arg = arg[2..-1] unless parsed.has_key?(arg) parsed[arg] = args[1] end parse_arguments(parsed, args[2..-1]) end def main(args) parsed = {} parse_arguments(parsed, args) port = nil if parsed.include?('port') begin port = parsed['port'] port = Integer(port) if port < 1 || port > 65535 raise ArgumentError end rescue ArgumentError puts("Invalid port \"#{port}\". Expected an integer between 1 and 65535") exit 2 end end if parsed.include?('directory') repo_directory = parsed['directory'] else env_var_name = GyozaLanguages::REPOSITORIES_DIRECTORY_ENV_NAME repo_directory = ENV[env_var_name] if repo_directory.nil? puts("No repositories directory specified.") puts("You can specify one by using --directory or by defining the environment variable: #{env_var_name}") exit 1 end end app = GyozaLanguageApp.new(repo_directory) if port.nil? app.start else app.start(port) end end main(ARGV)