This repository was archived by the owner on Sep 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_parameters.py
More file actions
117 lines (86 loc) · 3.15 KB
/
cli_parameters.py
File metadata and controls
117 lines (86 loc) · 3.15 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import getopt
import random
import sys
''' This file handles all cli parameters. '''
# Default param values
default_host = 'localhost'
default_port = 4242
default_bots = ['andrea', 'steven', 'arthur']
# Initial alias
alias = random.choice(default_bots)
client_help_docs = """
-h \t --help \t shows all command options
-l \t --list_bots \t shows all bots that can be used
Default Connection Data:
- Default Host: \t localhost
- Default Port: \t 4242
- Default Bot: \t Random bot from the bot array
Change connection data by passing in parameters such as:
-i \t --ip \t requires an IP address (-i 127.0.0.1)
-p \t --port \t requires a Port number (-p 4242)
-b \t --bot \t requires a bot name as (-b edward)
Alternatively: Parameters can be passed in with no options like so:
- client.py [host] [port] [bot]
Bot commands:
[quit] \t Terminate connection to server
[reply] \t Make the bot reply to previous message
[debug on/off] \t enables and disables bot debug mode
"""
server_help_docs = """
-h \t --help \t shows all command options
-p \t --port \t requires a Port number [-p 4242]
Alternatively: Parameters can be passed in with no [options] given like so:
- server.py [port]
- For example: \t server.py 4242
"""
def client_parameters():
global default_host, default_port, alias
argument_list = sys.argv[1:]
if len(argument_list) < 3:
opt_short = "hli:p:b:"
opt_long = ["help", "list_bots", "ip=", "port=", "bot="]
try:
arguments, values = getopt.getopt(argument_list, opt_short, opt_long)
except getopt.error as e:
print(str(e))
sys.exit(2)
for curr_arg, curr_val in arguments:
if curr_arg in ("-h", "--help"):
print(client_help_docs)
sys.exit(2)
elif curr_arg in ("-l", "--list_bots"):
print(f"Bots: {default_bots}")
sys.exit(2)
elif curr_arg in ("-i", "--ip"):
default_host = str(curr_val)
elif curr_arg in ("-p", "--port"):
default_port = int(curr_val)
elif curr_arg in ("-b", "--bot"):
alias = str(curr_val)
return {"host": default_host, "port": default_port, "alias": alias}
elif len(argument_list) == 3:
return {"host": sys.argv[1], "port": sys.argv[2], "alias": sys.argv[3]}
else:
print(f"\nParameters not recognized.\n"
f"{client_help_docs}")
sys.exit(2)
def server_parameters():
global default_port
argument_list = sys.argv[1:]
opt_short = "hp:"
opt_long = ["help", "port="]
try:
default_port = int(sys.argv[1])
except:
try:
arguments, values = getopt.getopt(argument_list, opt_short, opt_long)
except getopt.error as e:
print(str(e))
sys.exit(2)
for curr_arg, curr_val in arguments:
if curr_arg in ("-h", "--help"):
print(server_help_docs)
sys.exit(2)
elif curr_arg in ("-p", "--port"):
default_port = int(curr_val)
return {"host": default_host, "port": default_port}