# HG changeset patch # User Simon Cross # Date 1378038073 -7200 # Node ID fe1426d09074ea64618ba1fe89413b98f20a6b89 # Parent 17b233a546513922d9d4fd99e7bcd6bb23f98e1f# Parent 980339c28b42d536aeb09ab915922bd5c18c2fc1 Merge. diff -r 980339c28b42 -r fe1426d09074 nagslang/constants.py --- a/nagslang/constants.py Sun Sep 01 14:19:55 2013 +0200 +++ b/nagslang/constants.py Sun Sep 01 14:21:13 2013 +0200 @@ -1,2 +1,6 @@ SCREEN = (800, 600) FPS = 40 +DEFAULTS = dict( + debug=False, + sound=True, +) diff -r 980339c28b42 -r fe1426d09074 nagslang/options.py --- a/nagslang/options.py Sun Sep 01 14:19:55 2013 +0200 +++ b/nagslang/options.py Sun Sep 01 14:21:13 2013 +0200 @@ -1,3 +1,38 @@ +import optparse +import os + +from nagslang.constants import DEFAULTS + + +class AttrDict(dict): + '''A dict with attribute access''' + def __getattr__(self, attr): + return self[attr] + + +options = AttrDict() + + def parse_args(args): - #TODO - return + ''' + Parse arguments and store them in the options dictionary. + + Note: If you add arguments, you need to add an appropriate default to the + DEFAULTS dict. + ''' + options.update(DEFAULTS) + + options.debug = 'DEBUG' in os.environ + + parser = optparse.OptionParser() + parser.add_option('--no-sound', + dest='sound', action='store_false', default=True, + help='Disable sound') + if options.debug: + parser.add_option('--area', help='Initial area') + + opts, _ = parser.parse_args(args) + + for k in DEFAULTS: + if getattr(opts, k, None) is not None: + options[k] = getattr(opts, k)