-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcli.py
68 lines (51 loc) · 1.83 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
This is a command line script for the Chantek server
Usage:
python cli.py <cmdname> -m <method> -q <query> -ak <argument key> -av <argument value>
"""
import json, argparse, logging, sys, shutil
from commandsmanager import CommandsManager
commands = CommandsManager()
parser = argparse.ArgumentParser(description = "A command line script for the Chantek server.")
def run(args):
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)
if args.list:
return "\n".join(list(commands.listall().keys()))
if not args.cmdname:
parser.print_help()
return
cmd, result = commands.run(
cmdname = args.cmdname,
cmdmethod = args.method,
params = {
"q" : args.query
}
)
if result["error"]:
return result["error"]
if not args.verbose:
result = result["response"]
return result
def init_command(name):
print("Creating new command '%s'" % name)
shutil.copytree("etc/command-template", "commands/%s" % name)
def main():
parser.add_argument('cmdname', nargs = '?', help="Name of the command")
parser.add_argument('-m', '--method', help="Command method")
parser.add_argument('-q', '--query', help="Default query")
parser.add_argument('-l', '--list', help="List all available commands", action="store_true")
parser.add_argument('-d', '--debug', help="Show debug data", action="store_true")
parser.add_argument('-v', '--verbose', help="Show verbose results", action="store_true")
parser.add_argument('--init', help="Create new command")
args = parser.parse_args()
if args.init:
init_command(args.init)
sys.exit()
result = run(args)
if isinstance(result, str):
print(result)
else:
print(json.dumps(result, indent = 4))
if __name__ == "__main__":
main()