import sys
import json

from lbrynet.conf import API_CONNECTION_STRING
from jsonrpc.proxy import JSONRPCProxy

help_msg = "Usage: lbrynet-cli method json-args\n" \
             + "Examples: " \
             + "lbrynet-cli resolve_name '{\"name\": \"what\"}'\n" \
             + "lbrynet-cli get_balance\n" \
             + "lbrynet-cli help '{\"function\": \"resolve_name\"}'\n" \
             + "\n******lbrynet-cli functions******\n"


def main():
    api = JSONRPCProxy.from_url(API_CONNECTION_STRING)

    try:
        s = api.is_running()
    except:
        print "lbrynet-daemon isn't running"
        sys.exit(1)

    args = sys.argv[1:]
    meth = args[0]

    msg = help_msg
    for f in api.help():
        msg += f + "\n"

    if meth in ['--help', '-h', 'help']:
        print msg
        sys.exit(1)

    if len(args) > 1:
        if isinstance(args[1], dict):
            params = args[1]
        elif isinstance(args[1], basestring):
            params = json.loads(args[1])
    else:
        params = None

    if meth in api.help():
        try:
            if params:
                r = api.call(meth, params)
            else:
                r = api.call(meth)
            print json.dumps(r, sort_keys=True)
        except:
            print "Something went wrong, here's the usage for %s:" % meth
            print api.help({'function': meth})
    else:
        print "Unknown function"
        print msg


if __name__ == '__main__':
    main()

@@ -15,16 +15,32 @@
 ort json
+%0Aimport argparse
 %0A%0Afrom l
@@ -447,16 +447,235 @@
 **%5Cn%22%0A%0A%0A
+def guess_type(x):%0A    if '.' in x:%0A        try:%0A            return float(x)%0A        except ValueError:%0A            # not a float%0A            pass%0A    try:%0A        return int(x)%0A    except ValueError:%0A        return x%0A%0A
 def main
@@ -858,27 +858,171 @@
     
-args = sys.argv%5B1:%5D
+parser = argparse.ArgumentParser()%0A    parser.add_argument('method', nargs=1, type=str)%0A    parser.add_argument('params', nargs=%22+%22)%0A    args = parser.parse_args()
 %0A   
@@ -1037,249 +1037,508 @@
 args
+.method
 %5B0%5D%0A
-%0A
     
+para
 ms
-g
  = 
-help_msg
+%7B%7D%0A    if args.params:
 %0A    
-for f in api.help():%0A        msg += f + %22%5Cn%22%0A%0A    if meth in %5B'--help', '-h', 'help'%5D:%0A        print msg%0A        sys.exit(1)%0A%0A    if len(args) %3E 1:%0A        if isinstance(args%5B1%5D, dict):%0A            params = args
+    if len(args.params) != 1:%0A            for i in args.params:%0A                k, v = i.split('=')%5B0%5D, i.split('=')%5B1:%5D%0A                if isinstance(v, list):%0A                    v = ''.join(v)%0A                params%5Bk%5D = guess_type(v)%0A        else:%0A            try:%0A                params = json.loads(args.params%5B0%5D)%0A            except ValueError:%0A                for i in args.params:%0A                    k, v = i.split('=')%5B0%5D, i.split('=')
 %5B1
+:
 %5D%0A  
@@ -1543,18 +1543,28 @@
         
-el
+            
 if isins
@@ -1573,102 +1573,251 @@
 nce(
-args%5B1%5D, basestring):%0A            params = json.loads(args%5B1%5D)%0A    else:%0A        params = None
+v, list):%0A                        v = ''.join(v)%0A                    params%5Bk%5D = guess_type(v)%0A%0A    msg = help_msg%0A    for f in api.help():%0A        msg += f + %22%5Cn%22%0A%0A    if meth in %5B'--help', '-h', 'help'%5D:%0A        print msg%0A        sys.exit(1)
 %0A%0A  

fix windows cli