Example #1
0
def parse_args():
    """Return host and port, or print usage and exit."""
    usage = "usage: %prog [options] host [port]"
    desc = ("Create a Minecraft proxy listening for a client connection," +
            "and forward that connection to <host>:<port>.")
    parser = OptionParser(usage=usage,
                          description=desc)
    parser.add_option("-l", "--log-level", dest="loglvl", metavar="LEVEL",
                      choices=["debug","info","warn","error"],
                      help="Override logging.conf root log level")
    parser.add_option("--log-file", dest='logfile', metavar="FILE", default=None,
                      help="logging configuration file (optional)")
    parser.add_option("-p", "--local-port", dest="locport", metavar="PORT",
                      default="34343", type="int", help="Listen on this port")
    parser.add_option("-c", "--check-authenticity", dest="check_auth",
                      action="store_true", default=False,
                      help="Check authenticity of connecting clients")
    parser.add_option("-a", "--auto-authenticate", dest="authenticate",
                      action="store_true", default=False,
                      help="Authenticate with the credentials stored in the game client")
    parser.add_option("-u", "--user", dest="user", metavar="USERNAME", default=None,
                      help="Authenticate with the given username and ask for the password")
    parser.add_option("-P", "--password-file", dest="password_file",
                      metavar="FILE", default=None,
                      help="Authenticate with the credentials stored in FILE" +
                      "in the form \"username:password\"")
    parser.add_option("--plugin", dest="plugins", metavar="ID:PLUGIN(ARGS)", type="string",
                      action="append", help="Configure a plugin", default=[])
    parser.add_option("--profile", dest="perf_data", metavar="FILE", default=None,
                      help="Enable profiling, save profiling data to FILE")
    (opts,args) = parser.parse_args()

    if not 1 <= len(args) <= 2:
        parser.error("Incorrect number of arguments.") # Calls sys.exit()

    host = args[0]
    port = 25565
    if len(args) > 1:
        try:
            port = int(args[1])
        except ValueError:
            parser.error("Invalid port %s" % args[1])

    pcfg = PluginConfig()
    pregex = re.compile('((?P<id>\\w+):)?(?P<plugin_name>[\\w\\.\\d_]+)(\\((?P<argstr>.*)\\))?$')
    for pstr in opts.plugins:
        m = pregex.match(pstr)
        if not m:
            logger.error('Invalid --plugin option: %s' % pstr)
            sys.exit(1)
        else:
            parts = {'argstr': ''}
            parts.update(m.groupdict())
            pcfg.add(**parts)

    return (host, port, opts, pcfg)