Skip to content

Commit 43a8b97

Browse files
committed
cmd_prompt
1 parent 1bafc35 commit 43a8b97

File tree

5 files changed

+68
-21
lines changed

5 files changed

+68
-21
lines changed

cfg.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def parse_args():
88
help = 'game server ip address')
99
parser.add_argument('-p', '--port', type=int, dest = 'port',
1010
help = 'game server tcp port')
11+
parser.add_argument('-u', '--uid', dest = 'uid', help="login as UID")
1112
parser.add_argument("-m", '--mode', dest = "mode", choices = ["simulator", "client"])
1213
parser.add_argument(dest="config", help = "config file")
1314
args = parser.parse_args()
@@ -20,5 +21,4 @@ def parse_args():
2021
Config[k] = v
2122

2223
sys.path.insert(0, Config["proto_path"])
23-
24-
CLIENT_PROMPT = "> "
24+

cmdclient.py

+21-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
11
import sys,traceback
22
from game import Game
3-
from command import do_cmdstr,has_cmd
3+
from command import do_cmdstr,find_cmd
4+
5+
import pprint
6+
7+
from cfg import Config
48

5-
import cfg
69

710
class Client(object):
811
def __init__(self, srv_addr):
912
self.game = Game(srv_addr)
1013

14+
def docmd(self, tokens):
15+
cmdname = tokens[0]
16+
ok,similars = find_cmd(cmdname)
17+
if not ok:
18+
print "cmd not found:", cmdname
19+
print "Did you mean this?"
20+
print "\t", ", ".join(similars)
21+
return
22+
result = do_cmdstr(self.game, cmdname, tokens[1] if len(tokens) > 1 else "")
23+
if result != None:
24+
pprint.pprint(result)
25+
1126
def interact(self):
27+
if "uid" in Config:
28+
self.docmd(["login", Config["uid"]])
1229
while True:
1330
try:
14-
sys.stdout.write(cfg.CLIENT_PROMPT)
31+
sys.stdout.write(Config["client_prompt"])
1532
sys.stdout.flush()
1633
l = sys.stdin.read(1)
1734
if not l: # eof
@@ -24,13 +41,7 @@ def interact(self):
2441
tokens = l.split(None, 1)
2542
if not tokens:
2643
continue
27-
cmdname = tokens[0]
28-
if not has_cmd(cmdname):
29-
print("cmd not found:", cmdname)
30-
continue
31-
result = do_cmdstr(self.game, cmdname, tokens[1] if len(tokens) > 1 else "")
32-
if result != None:
33-
print(result)
44+
self.docmd(tokens)
3445

3546
except Exception as e:
3647
print "error occured", e, traceback.format_exc()

command.py

+35-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys, os, importlib
2+
import inspect
23

34
cmddir = "cmd"
45

@@ -41,9 +42,36 @@ def _parseargs(argstr):
4142
for i in xrange(len(args)):
4243
args[i] = eval(args[i], {}, {})
4344
return args
44-
45-
def has_cmd(cmdname):
46-
return cmdname in commands
45+
46+
def find_cmd(cmdname):
47+
if cmdname in commands:
48+
return True,[]
49+
50+
similars = []
51+
for n in commands:
52+
if n.startswith(cmdname):
53+
similars.append(n)
54+
55+
if not similars:
56+
mindiff = 100000
57+
for n in commands:
58+
d = abs(len(n) - len(cmdname))
59+
for i in range(min(len(n), len(cmdname))):
60+
if n[i] != cmdname[i]:
61+
d += 1
62+
if d < mindiff:
63+
similars = [n]
64+
mindiff = d
65+
elif d == mindiff:
66+
similars.append(n)
67+
68+
return False, similars
69+
70+
def inspect_cmd(cmdname):
71+
cmd = commands[cmdname]
72+
args = inspect.getargspec(cmd["handle"]).args
73+
del args[0] # remove `self' arg
74+
return cmdname, args
4775

4876
def do_cmd(player, cmdname, args):
4977
cmd = commands[cmdname]
@@ -82,3 +110,7 @@ def _decorator(f):
82110
return f
83111

84112
return _decorator
113+
114+
def listcmd():
115+
return [cmdname for cmdname in commands]
116+

game.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import gevent
88

99
from command import *
10-
import cfg
10+
from cfg import Config
1111

1212
def _get_secret(uid, token, salt):
1313
m = md5.new()
@@ -35,16 +35,19 @@ def send(self, protoname, msg = {}):
3535
return self.srv.invoke(protoname, msg)
3636

3737
################################ Test #########################################
38-
@addcmd("create")
39-
def create_user(self):
40-
resp = self.call("client.create", {"platform" : 1})
41-
print resp["uid"], resp["token"]
38+
@addcmd()
39+
def help(self, cmdname = ""):
40+
ok,similars = find_cmd(cmdname)
41+
cmds = [cmdname] if ok else similars
42+
for cmdname in cmds:
43+
cmdname,args = inspect_cmd(cmdname)
44+
print cmdname,args
4245

4346
@addcmd()
4447
def login(self, user):
4548
'''for sproto test login '''
4649
resp = self.call("login", {"account": user})
47-
cfg.CLIENT_PROMPT = "[%s] > " % user
50+
Config["client_prompt"] = "[%s] > " % user
4851
print(resp["prompt"])
4952

5053
@addcmd()

test/config.py

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
## client mode
2828
mode = "client"
29+
client_prompt = "> "
2930

3031
# mode = "simulator" # client / simulator
3132
# run = [

0 commit comments

Comments
 (0)