annotate pytouhou/options.py @ 762:cad9e422b18f

interpreters, runners: Add missing mod/lib files.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 06 Jan 2021 01:47:24 +0100
parents 4fa0a8e7d941
children 7492d384d122
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
565
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 ##
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 ## Copyright (C) 2014 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 ##
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 ## by the Free Software Foundation; version 3 only.
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 ##
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 ## GNU General Public License for more details.
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 ##
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14
567
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
15 import os
590
e15672733c93 Switch to Python 3.x instead of 2.7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 589
diff changeset
16 from configparser import RawConfigParser, NoOptionError
567
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
17
568
e7a4731a278b Add a GTK+ main menu, mimicking the original EoSD one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 567
diff changeset
18 from pytouhou.utils.xdg import load_config_paths, save_config_path
567
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
19
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
20
615
d1f0bb0b7a17 Don’t inherit explicitely from object, we are not on Python 2.7 anymore. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 597
diff changeset
21 class Options:
567
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
22 def __init__(self, name, defaults):
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
23 load_paths = list(reversed([os.path.join(directory, '%s.cfg' % name)
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
24 for directory
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
25 in load_config_paths(name)]))
568
e7a4731a278b Add a GTK+ main menu, mimicking the original EoSD one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 567
diff changeset
26 self.save_path = os.path.join(save_config_path(name), '%s.cfg' % name)
567
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
27
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
28 self.config = RawConfigParser(defaults)
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
29 self.paths = self.config.read(load_paths)
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
30 self.section = name if self.config.has_section(name) else 'DEFAULT'
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
31
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
32 def get(self, option):
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
33 try:
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
34 return self.config.get(self.section, option)
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
35 except NoOptionError:
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
36 return None
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
37
568
e7a4731a278b Add a GTK+ main menu, mimicking the original EoSD one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 567
diff changeset
38 def set(self, option, value):
e7a4731a278b Add a GTK+ main menu, mimicking the original EoSD one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 567
diff changeset
39 if value is not None:
e7a4731a278b Add a GTK+ main menu, mimicking the original EoSD one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 567
diff changeset
40 self.config.set(self.section, option, value)
e7a4731a278b Add a GTK+ main menu, mimicking the original EoSD one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 567
diff changeset
41 else:
e7a4731a278b Add a GTK+ main menu, mimicking the original EoSD one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 567
diff changeset
42 self.config.remove_option(self.section, option)
e7a4731a278b Add a GTK+ main menu, mimicking the original EoSD one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 567
diff changeset
43
e7a4731a278b Add a GTK+ main menu, mimicking the original EoSD one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 567
diff changeset
44 defaults = self.config._defaults
e7a4731a278b Add a GTK+ main menu, mimicking the original EoSD one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 567
diff changeset
45 self.config._defaults = None
e7a4731a278b Add a GTK+ main menu, mimicking the original EoSD one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 567
diff changeset
46 with open(self.save_path, 'w') as save_file:
e7a4731a278b Add a GTK+ main menu, mimicking the original EoSD one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 567
diff changeset
47 self.config.write(save_file)
e7a4731a278b Add a GTK+ main menu, mimicking the original EoSD one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 567
diff changeset
48 self.config._defaults = defaults
e7a4731a278b Add a GTK+ main menu, mimicking the original EoSD one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 567
diff changeset
49
567
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
50
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
51 def patch_argument_parser():
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
52 from argparse import ArgumentParser, _ActionsContainer
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
53
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
54 original_method = _ActionsContainer.add_argument
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
55
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
56 def add_argument(self, *args, **kwargs):
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
57 if 'default' not in kwargs:
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
58 dest = kwargs.get('dest')
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
59 if dest is None:
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
60 for dest in args:
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
61 dest = dest.lstrip('-')
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
62 value = self.default.get(dest)
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
63 if value is not None:
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
64 break
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
65 else:
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
66 dest = dest.replace('_', '-')
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
67 value = self.default.get(dest)
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
68 if value is not None:
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
69 argument_type = kwargs.get('type')
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
70 if argument_type is not None:
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
71 value = argument_type(value)
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
72 action = kwargs.get('action')
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
73 if action == 'store_true':
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
74 value = value.lower() == 'true'
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
75 elif action == 'store_false':
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
76 value = value.lower() != 'true'
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
77 if kwargs.get('nargs') == '*' and isinstance(value, str):
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
78 value = value.split()
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
79 kwargs['default'] = value
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
80 elif dest == 'double-buffer':
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
81 kwargs['default'] = None
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
82 return original_method(self, *args, **kwargs)
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
83 _ActionsContainer.add_argument = add_argument
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
84
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
85 class Parser(ArgumentParser):
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
86 def __init__(self, *args, **kwargs):
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
87 self.default = kwargs.pop('default')
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
88 ArgumentParser.__init__(self, *args, **kwargs)
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
89
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
90 def add_argument_group(self, *args, **kwargs):
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
91 group = ArgumentParser.add_argument_group(self, *args, **kwargs)
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
92 group.default = self.default
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
93 group.add_argument_group = self.add_argument_group
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
94 group.add_mutually_exclusive_group = self.add_mutually_exclusive_group
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
95 return group
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
96
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
97 def add_mutually_exclusive_group(self, *args, **kwargs):
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
98 group = ArgumentParser.add_mutually_exclusive_group(self, *args, **kwargs)
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
99 group.default = self.default
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
100 group.add_argument_group = self.add_argument_group
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
101 group.add_mutually_exclusive_group = self.add_mutually_exclusive_group
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
102 return group
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
103
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
104 return Parser
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
105
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
106
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
107 ArgumentParser = patch_argument_parser()
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
108
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
109
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
110 def parse_config(section, defaults):
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
111 return Options(section, defaults)
565
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
113
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
114 def parse_arguments(defaults):
567
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
115 parser = ArgumentParser(description='Libre reimplementation of the Touhou 6 engine.', default=defaults)
565
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
116
567
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
117 parser.add_argument('data', metavar='DAT', nargs='*', help='Game’s data files')
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
118 parser.add_argument('-p', '--path', metavar='DIRECTORY', help='Game directory path.')
565
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119 parser.add_argument('--debug', action='store_true', help='Set unlimited continues, and perhaps other debug features.')
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
120 parser.add_argument('--verbosity', metavar='VERBOSITY', choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], help='Select the wanted logging level.')
568
e7a4731a278b Add a GTK+ main menu, mimicking the original EoSD one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 567
diff changeset
121 parser.add_argument('--no-menu', action='store_true', help='Disable the menu.')
565
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
122
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
123 game_group = parser.add_argument_group('Game options')
567
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
124 game_group.add_argument('-s', '--stage', metavar='STAGE', type=int, help='Stage, 1 to 7 (Extra), nothing means story mode.')
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
125 game_group.add_argument('-r', '--rank', metavar='RANK', type=int, help='Rank, from 0 (Easy, default) to 3 (Lunatic).')
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
126 game_group.add_argument('-c', '--character', metavar='CHARACTER', type=int, help='Select the character to use, from 0 (ReimuA, default) to 3 (MarisaB).')
565
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
127 game_group.add_argument('-b', '--boss-rush', action='store_true', help='Fight only bosses.')
597
244c99c568c8 Don’t hardcode the available games and interfaces, and relocation them.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
128 game_group.add_argument('--game', metavar='GAME', help='Select the game engine to use.')
244c99c568c8 Don’t hardcode the available games and interfaces, and relocation them.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
129 game_group.add_argument('--interface', metavar='INTERFACE', help='Select the interface to use.')
567
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
130 game_group.add_argument('--hints', metavar='HINTS', help='Hints file, to display text while playing.')
565
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
131
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
132 replay_group = parser.add_argument_group('Replay options')
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
133 replay_group.add_argument('--replay', metavar='REPLAY', help='Select a file to replay.')
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
134 replay_group.add_argument('--save-replay', metavar='REPLAY', help='Save the upcoming game into a replay file.')
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
135 replay_group.add_argument('--skip-replay', action='store_true', help='Skip the replay and start to play when it’s finished.')
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
136
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
137 netplay_group = parser.add_argument_group('Netplay options')
567
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
138 netplay_group.add_argument('--port', metavar='PORT', type=int, help='Local port to use.')
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
139 netplay_group.add_argument('--remote', metavar='REMOTE', help='Remote address.')
565
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
140 netplay_group.add_argument('--friendly-fire', action='store_true', help='Allow friendly-fire during netplay.')
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
141
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
142 graphics_group = parser.add_argument_group('Graphics options')
636
4fa0a8e7d941 Add a GLFW implementation of gui.Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 615
diff changeset
143 graphics_group.add_argument('--frontend', metavar='FRONTEND', choices=['glfw', 'sdl'], help='Which windowing library to use (glfw or sdl).')
567
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
144 graphics_group.add_argument('--backend', metavar='BACKEND', choices=['opengl', 'sdl'], nargs='*', help='Which backend to use (opengl or sdl).')
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
145 graphics_group.add_argument('--fps-limit', metavar='FPS', type=int, help='Set fps limit. A value of 0 disables fps limiting, while a negative value limits to 60 fps if and only if vsync doesn’t work.')
589
0768122da817 Add a frameskip option, and use swap interval to implement it.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 568
diff changeset
146 graphics_group.add_argument('--frameskip', metavar='FRAMESKIP', type=int, help='Set the frameskip, as 1/FRAMESKIP, or disabled if 0.')
565
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
147 graphics_group.add_argument('--no-background', action='store_false', help='Disable background display (huge performance boost on slow systems).')
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
148 graphics_group.add_argument('--no-particles', action='store_false', help='Disable particles handling (huge performance boost on slow systems).')
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
149 graphics_group.add_argument('--no-sound', action='store_false', help='Disable music and sound effects.')
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
150
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
151 opengl_group = parser.add_argument_group('OpenGL backend options')
567
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
152 opengl_group.add_argument('--gl-flavor', choices=['core', 'es', 'compatibility', 'legacy'], help='OpenGL profile to use.')
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
153 opengl_group.add_argument('--gl-version', type=float, help='OpenGL version to use.')
565
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
154
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
155 double_buffer = opengl_group.add_mutually_exclusive_group()
567
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
156 double_buffer.add_argument('--double-buffer', dest='double_buffer', action='store_true', help='Enable double buffering.')
b2269b9c6119 Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 565
diff changeset
157 double_buffer.add_argument('--single-buffer', dest='double_buffer', action='store_false', help='Disable double buffering.')
565
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
158
5f7f859a72f9 Move CLI options to their own module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
159 return parser.parse_args()