e280b67773b9e0703cbf9f47fa6b7bfeb9cc60a3
[naja.git] / naja / options.py
1 import optparse
2 import os
3 import sys
4
5 from naja.attrdict import AttrDict
6 from naja.constants import DEFAULTS
7
8
9 options = AttrDict()
10
11
12 def parse_args(args):
13     '''
14     Parse arguments and store them in the options dictionary.
15
16     Note: If you add arguments, you need to add an appropriate default to the
17     DEFAULTS dict.
18     '''
19     options.update(DEFAULTS)
20
21     options.debug = 'DEBUG' in os.environ
22
23     parser = optparse.OptionParser()
24     parser.add_option('--no-sound',
25                       dest='sound', action='store_false', default=True,
26                       help='Disable all sound, including music')
27
28     parser.add_option('--no-music',
29                       dest='music', action='store_false', default=True,
30                       help='Disable music (but not sound)')
31
32     parser.add_option("--save-location", default=_get_default_save_location(),
33                       dest="save_location", help="Saved game location")
34
35     if options.debug:
36         parser.add_option('--initial-bits', type=int,
37                           help='Initial player bits')
38
39     opts, _ = parser.parse_args(args)
40
41     for k in DEFAULTS:
42         if getattr(opts, k, None) is not None:
43             options[k] = getattr(opts, k)
44     options['save_location'] = opts.save_location
45
46
47 def _get_default_save_location():
48     """Return a default save game location."""
49     app = "naja"
50     if sys.platform.startswith("win"):
51         if "APPDATA" in os.environ:
52             return os.path.join(os.environ["APPDATA"], app)
53         return os.path.join(os.path.expanduser("~"), "." + app)
54     elif 'XDG_DATA_HOME' in os.environ:
55         return os.path.join(os.environ["XDG_DATA_HOME"], app)
56     return os.path.join(os.path.expanduser("~"), ".local", "share", app)