-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
60 lines (41 loc) · 1.17 KB
/
main.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
import sys
import getopt
import os
from game import run_game
HELP_MESSAGE = 'main.py -f <wordfile> -c <wordcount>'
def error(msg: str):
print (msg)
sys.exit(2)
def get_arguments():
file_arg = ""
count_arg = ""
if (len(sys.argv) < 2):
error (HELP_MESSAGE)
try:
opts, _ = getopt.getopt(sys.argv[1:],"hf:c:",["file=","count="])
except getopt.GetoptError:
error (HELP_MESSAGE)
for opt, arg in opts:
if opt == '-h':
print (HELP_MESSAGE)
sys.exit()
elif opt in ("-f", "--file"):
file_arg = arg
elif opt in ("-c", "--count"):
count_arg = arg
return file_arg, count_arg
def process_arguments(file_arg, count_arg):
if not os.path.isfile(file_arg):
error ("File not found")
count = 0
try:
count = int(count_arg)
if count <= 0:
raise ValueError
except ValueError:
error ("Word count must be a positive integer")
return file_arg, count
if __name__ == "__main__":
file_arg, count_arg = get_arguments()
file, count = process_arguments(file_arg, count_arg)
run_game(file, count)