#!/usr/bin/env python
import argparse

from greenrpc import DEFAULT_PORT
from greenrpc.client import TCPClient, HTTPClient

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Start a new GreenRPC TCP Server")
    parser.add_argument("method", metavar="<method>", type=str,
                        help="The remote method to call")
    parser.add_argument("args", metavar="<arg>", nargs="*", type=str,
                        help="Arguments to send for the remote method call")

    default_connect = "127.0.0.1:%s" % (DEFAULT_PORT, )
    parser.add_argument("--connect", dest="connect", type=str, default=default_connect,
                        help="<address>:<port> of the server to connect to(default: %s)" % (default_connect, ))

    parser.add_argument("--debug", dest="debug", action="store_true", default=False,
                        help="whether or not to show the full result")

    parser.add_argument("--http", dest="http", action="store_true", default=False,
                        help="whether the server is http or tcp")

    args = parser.parse_args()
    address, _, port = args.connect.partition(":")
    connect = (address, int(port))
    if args.http:
        client = HTTPClient(connect=connect)
    else:
        client = TCPClient(connect=connect)
    result = client.call(args.method, args.args, debug=args.debug)
    print result
