Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An invalid theme name makes program fail #16

Open
bittner opened this issue Oct 28, 2018 · 0 comments
Open

An invalid theme name makes program fail #16

bittner opened this issue Oct 28, 2018 · 0 comments

Comments

@bittner
Copy link

bittner commented Oct 28, 2018

If you specify a theme name in the CLI that doesn't exist the program fails with a KeyError, e.g.

$ python3 snake -t classi
Traceback (most recent call last):
  File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "snake/__main__.py", line 26, in <module>
    run()
  File "snake/__main__.py", line 16, in run
    stage.init()
  File "snake/stage.py", line 43, in init
    chosen_theme = themes.game_themes[parser.options.theme]
KeyError: 'classi'

This can be avoided by allowing only valid theme names in the CLI parser, e.g. via choices in argparse. Ideally, you fill in the choices dynamically, directly from the themes module, e.g.

import argparse
from . import themes

parser = argparse.ArgumentParser(prog='snake')
parser.add_argument('-t', '--theme', type=str, choices=list(themes.game_themes))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant