def main():
    """ function to manage restkit command line """
    parser = op.OptionParser(usage=__usage__, option_list=options(),
                    version="%prog " + __version__)

    opts, args = parser.parse_args()
    args_len = len(args)

    if opts.shell:
        try:
            from restkit.contrib import ipython_shell as shell
            shell.main(options=opts, *args)
        except Exception, e:
            print >>sys.stderr, str(e)
            sys.exit(1)
        return

    if args_len < 1:
        return parser.error('incorrect number of arguments')

    if opts.log_level is not None:
        set_logging(opts.log_level)

    body = None
    headers = []
    if opts.input:
        if opts.input == '-':
            body = sys.stdin.read()
            headers.append(("Content-Length", str(len(body))))
        else:
            fname = os.path.normpath(os.path.join(os.getcwd(),opts.input))
            body = open(fname, 'r')
    
    if opts.headers:
        for header in opts.headers:
            try:
                k, v = header.split(':')
                headers.append((k, v))
            except ValueError:
                pass


    try:
        if len(args) == 2:
            if args[1] == "-" and not opts.input:
                body = sys.stdin.read()
                headers.append(("Content-Length", str(len(body))))

        if not opts.method and opts.input:
            method = 'POST'
        else:
            method=opts.method.upper()
            
        resp = request(args[0], method=method, body=body,
                    headers=headers, follow_redirect=opts.follow_redirect)
                        
        if opts.output and opts.output != '-':
            with open(opts.output, 'wb') as f:
                if opts.server_response:
                    f.write("Server response from %s:\n" % resp.final_url)
                    for k, v in resp.headerslist:
                        f.write( "%s: %s" % (k, v))
                else:
                    with resp.body_stream() as body:
                        for block in body:
                            f.write(block)
        else:
            if opts.server_response:
                if opts.prettify:
                    print "\n\033[0m\033[95mServer response from %s:\n\033[0m" % (
                                                                    resp.final_url)
                    for k, v in resp.headerslist:
                        print "\033[94m%s\033[0m: %s" % (k, v)
                    print "\033[0m"
                else:
                    print "Server response from %s:\n" % (resp.final_url)
                    for k, v in resp.headerslist:
                        print "%s: %s" % (k, v)
                    print ""

                if opts.output == '-':
                    if opts.prettify:
                        print prettify(resp)
                    else:
                        print resp.body_string()
            else:
                if opts.prettify:
                    print prettify(resp)
                else:
                    print resp.body_string()
        
    except Exception, e:
        sys.stderr.write("An error happened: %s" % str(e))
        sys.stderr.flush()
        sys.exit(1)

    sys.exit(0)
dataset/ETHPy150Open benoitc/restkit/restkit/contrib/console.py/main/original