Skip to content

Commit 5e142cd

Browse files
committed
Add "freegames" entry-point and "play" subcommand
1 parent d4f6a9d commit 5e142cd

File tree

2 files changed

+52
-39
lines changed

2 files changed

+52
-39
lines changed

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ classifiers = [
3030
]
3131
dynamic = ["version"]
3232

33+
[project.scripts]
34+
freegames = "freegames.__main__:main"
35+
3336
[project.urls]
3437
Documentation = "https://grantjenks.com/docs/freegames/"
3538
Funding = "https://gum.co/freegames"

src/freegames/__main__.py

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33

44
import argparse
55
import os
6-
7-
directory = os.path.dirname(os.path.realpath(__file__))
8-
contents = os.listdir(directory)
6+
import runpy
97

108

119
def game_file(name):
@@ -17,49 +15,61 @@ def game_file(name):
1715
)
1816

1917

20-
games = sorted(name[:-3] for name in contents if game_file(name))
18+
def main():
19+
directory = os.path.dirname(os.path.realpath(__file__))
20+
contents = os.listdir(directory)
21+
games = sorted(name[:-3] for name in contents if game_file(name))
2122

22-
parser = argparse.ArgumentParser(
23-
prog='freegames',
24-
description='Free Python Games',
25-
epilog='Copyright 2023 Grant Jenks',
26-
)
27-
subparsers = parser.add_subparsers(dest='command', help='sub-command help')
23+
parser = argparse.ArgumentParser(
24+
prog='freegames',
25+
description='Free Python Games',
26+
epilog='Copyright 2023 Grant Jenks',
27+
)
28+
subparsers = parser.add_subparsers(dest='command', help='sub-command help')
29+
subparsers.required = True
2830

29-
parser_list = subparsers.add_parser('list', help='list games')
31+
parser_list = subparsers.add_parser('list', help='list games')
3032

31-
parser_copy = subparsers.add_parser('copy', help='copy game source code')
32-
parser_copy.add_argument('game', choices=games, help='game name')
33-
parser_copy.add_argument(
34-
'--force',
35-
action='store_true',
36-
help='overwrite existing file',
37-
)
33+
parser_play = subparsers.add_parser('play', help='play free Python games')
34+
parser_play.add_argument('game', choices=games, help='game name')
3835

39-
parser_show = subparsers.add_parser('show', help='show game source code')
40-
parser_show.add_argument('game', choices=games, help='game name')
36+
parser_show = subparsers.add_parser('show', help='show game source code')
37+
parser_show.add_argument('game', choices=games, help='game name')
4138

42-
args = parser.parse_args()
39+
parser_copy = subparsers.add_parser('copy', help='copy game source code')
40+
parser_copy.add_argument('game', choices=games, help='game name')
41+
parser_copy.add_argument(
42+
'--force',
43+
action='store_true',
44+
help='overwrite existing file',
45+
)
4346

44-
if args.command == 'list':
45-
for game in games:
46-
print(game)
47-
elif args.command == 'copy':
48-
filename = args.game + '.py'
47+
args = parser.parse_args()
4948

50-
with open(os.path.join(directory, filename)) as reader:
51-
text = reader.read()
49+
if args.command == 'list':
50+
for game in games:
51+
print(game)
52+
elif args.command == 'play':
53+
runpy.run_module('freegames.' + args.game)
54+
elif args.command == 'show':
55+
with open(os.path.join(directory, args.game + '.py')) as reader:
56+
print(reader.read())
57+
else:
58+
assert args.command == 'copy'
59+
filename = args.game + '.py'
5260

53-
cwd = os.getcwd()
54-
path = os.path.join(cwd, filename)
61+
with open(os.path.join(directory, filename)) as reader:
62+
text = reader.read()
63+
64+
cwd = os.getcwd()
65+
path = os.path.join(cwd, filename)
66+
67+
if args.force or not os.path.exists(path):
68+
with open(path, 'w') as writer:
69+
writer.write(text)
70+
else:
71+
print('ERROR: File already exists. Specify --force to overwrite.')
5572

56-
if args.force or not os.path.exists(path):
57-
with open(path, 'w') as writer:
58-
writer.write(text)
59-
else:
60-
print('ERROR: File already exists. Specify --force to overwrite.')
61-
else:
62-
assert args.command == 'show'
6373

64-
with open(os.path.join(directory, args.game + '.py')) as reader:
65-
print(reader.read())
74+
if __name__ == '__main__':
75+
main()

0 commit comments

Comments
 (0)