Package horizons :: Package util :: Module cmdlineoptions
[hide private]
[frames] | no frames]

Source Code for Module horizons.util.cmdlineoptions

  1  #!/usr/bin/env python3 
  2   
  3  # ################################################### 
  4  # Copyright (C) 2008-2017 The Unknown Horizons Team 
  5  # team@unknown-horizons.org 
  6  # This file is part of Unknown Horizons. 
  7  # 
  8  # Unknown Horizons is free software; you can redistribute it and/or modify 
  9  # it under the terms of the GNU General Public License as published by 
 10  # the Free Software Foundation; either version 2 of the License, or 
 11  # (at your option) any later version. 
 12  # 
 13  # This program is distributed in the hope that it will be useful, 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  # GNU General Public License for more details. 
 17  # 
 18  # You should have received a copy of the GNU General Public License 
 19  # along with this program; if not, write to the 
 20  # Free Software Foundation, Inc., 
 21  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 22  # ################################################### 
 23   
 24  import datetime 
 25  import optparse 
 26  import re 
 27   
 28   
29 -def get_option_parser():
30 """Returns inited OptionParser object""" 31 from horizons.constants import VERSION 32 p = optparse.OptionParser(usage="%prog [options]", version=VERSION.string()) 33 p.add_option("-d", "--debug", dest="debug", action="store_true", 34 default=False, help="Enable debug output to stderr and a logfile.") 35 p.add_option("--fife-path", dest="fife_path", metavar="<path>", 36 help="Specify the path to FIFE root directory.") 37 p.add_option("--restore-settings", dest="restore_settings", action="store_true", default=False, 38 help="Restores the default settings. " 39 "Useful if Unknown Horizons crashes on startup due to misconfiguration.") 40 p.add_option("--mp-master", dest="mp_master", metavar="<ip:port>", 41 help="Specify alternative multiplayer master server.") 42 p.add_option("--mp-bind", dest="mp_bind", metavar="<ip:port>", 43 help="Specify network address to bind local network client to. " 44 "This is useful if NAT holepunching is not working but you can forward a static port.") 45 46 start_uh = optparse.OptionGroup(p, "Starting Unknown Horizons") 47 start_uh.add_option("--start-map", dest="start_map", metavar="<map>", 48 help="Starts <map>. <map> is the mapname.") 49 start_uh.add_option("--start-random-map", dest="start_random_map", action="store_true", 50 help="Starts a random map.") 51 start_uh.add_option("--start-specific-random-map", dest="start_specific_random_map", 52 metavar="<seed>", help="Starts a random map with seed <seed>.") 53 start_uh.add_option("--start-scenario", dest="start_scenario", metavar="<scenario>", 54 help="Starts <scenario>. <scenario> is the scenarioname.") 55 start_uh.add_option("--start-dev-map", dest="start_dev_map", action="store_true", 56 default=False, help="Starts the development map without displaying the main menu.") 57 start_uh.add_option("--load-game", dest="load_game", metavar="<game>", 58 help="Loads a saved game. <game> is the saved game's name.") 59 start_uh.add_option("--load-last-quicksave", dest="load_quicksave", action="store_true", 60 help="Loads the last quicksave.") 61 start_uh.add_option("--edit-map", dest="edit_map", metavar="<map>", 62 help="Edit map <map>.") 63 start_uh.add_option("--edit-game-map", dest="edit_game_map", metavar="<game>", 64 help="Edit the map from the saved game <game>.") 65 start_uh.add_option("--no-audio", dest="no_audio", action="store_true", help="Starts UH without sounds.") 66 p.add_option_group(start_uh) 67 68 ai_group = optparse.OptionGroup(p, "AI options") 69 ai_group.add_option("--ai-players", dest="ai_players", metavar="<ai_players>", 70 type="int", default=0, 71 help="Uses <ai_players> AI players (excludes the possible human-AI hybrid; defaults to 0).") 72 ai_group.add_option("--human-ai-hybrid", dest="human_ai", action="store_true", 73 help="Makes the human player a human-AI hybrid (for development only).") 74 ai_group.add_option("--force-player-id", dest="force_player_id", 75 metavar="<force_player_id>", type="int", default=None, 76 help="Set the player with id <force_player_id> as the active (human) player.") 77 ai_group.add_option("--ai-highlights", dest="ai_highlights", action="store_true", 78 help="Shows AI plans as highlights (for development only).") 79 ai_group.add_option("--ai-combat-highlights", dest="ai_combat_highlights", action="store_true", 80 help="Highlights combat ranges for units controlled by AI Players (for development only).") 81 p.add_option_group(ai_group) 82 83 dev_group = optparse.OptionGroup(p, "Development options") 84 dev_group.add_option("--debug-log-only", dest="debug_log_only", action="store_true", 85 default=False, help="Write debug output only to logfile, not to console. Implies -d.") 86 dev_group.add_option("--debug-module", action="append", dest="debug_module", 87 metavar="<module>", default=[], 88 help="Enable logging for a certain logging module (for developing only).") 89 dev_group.add_option("--logfile", dest="logfile", metavar="<filename>", 90 help="Writes log to <filename> instead of to the uh-userdir") 91 dev_group.add_option("--max-ticks", dest="max_ticks", metavar="<max_ticks>", type="int", 92 help="Run the game for <max_ticks> ticks.") 93 dev_group.add_option("--no-freeze-protection", dest="freeze_protection", action="store_false", 94 default=True, help="Disable freeze protection.") 95 dev_group.add_option("--string-previewer", dest="stringpreview", action="store_true", 96 default=False, help="Enable the string previewer tool for scenario writers") 97 dev_group.add_option("--no-preload", dest="nopreload", action="store_true", 98 default=False, help="Disable preloading while in main menu") 99 dev_group.add_option("--game-speed", dest="gamespeed", metavar="<game_speed>", type="float", 100 help="Run the game in the given speed (Values: 0.5, 1, 2, 3, 4, 6, 8, 11, 20)") 101 dev_group.add_option("--gui-test", dest="gui_test", metavar="<test>", 102 default=False, help=optparse.SUPPRESS_HELP) 103 dev_group.add_option("--gui-log", dest="log_gui", action="store_true", 104 default=False, help="Log gui interactions") 105 dev_group.add_option("--sp-seed", dest="sp_seed", metavar="<seed>", type="int", 106 help="Use this seed for singleplayer sessions.") 107 dev_group.add_option("--generate-minimap", dest="generate_minimap", 108 metavar="<parameters>", help=optparse.SUPPRESS_HELP) 109 dev_group.add_option("--create-mp-game", action="store_true", dest="create_mp_game", 110 help="Create an multiplayer game with default settings.") 111 dev_group.add_option("--join-mp-game", action="store_true", dest="join_mp_game", 112 help="Join first multiplayer game.") 113 if VERSION.IS_DEV_VERSION: 114 dev_group.add_option("--no-atlas-generation", action="store_false", dest="atlas_generation", 115 help="Disable atlas generation.") 116 # Add dummy default variables for the DEV_VERSION groups above when in release mode 117 p.set_defaults(atlas_generation=True) 118 p.add_option_group(dev_group) 119 120 return p
121 122
123 -class ManPageFormatter(optparse.HelpFormatter):
124 """Formatter that extracts our huge option list into manpage format. 125 126 Inspired by and mostly copied from this blog post: 127 http://andialbrecht.wordpress.com/2009/03/17/creating-a-man-page-with-distutils-and-optparse/ 128 """
129 - def __init__(self, indent_increment=2, max_help_position=24, 130 width=72, short_first=1):
131 optparse.HelpFormatter.__init__(self, 132 indent_increment, max_help_position, width, short_first)
133
134 - def _markup(self, text):
135 return text.replace('-', r'\-')
136
137 - def optmarkup(self, text):
138 """Highlight flags only""" 139 replace_with = r'\\fB\1\\fR' 140 pattern = r'(--\w*)(?:, )?' 141 text = re.sub(pattern, replace_with, text) 142 pattern = r'(-\w*)' 143 text = re.sub(pattern, replace_with, text) 144 return self._markup(text)
145
146 - def format_text(self, text):
147 return self._markup(text)
148
149 - def format_usage(self, usage):
150 """Overridden, else it would print 'options.py usage'.""" 151 return r'''\ 152 ." some portability stuff 153 .ie \n(.g .ds Aq \(aq 154 .el .ds Aq ' 155 ." disable hyphenation and justification (adjust text to left margin only) 156 .nh 157 .ad l 158 .SH "NAME" 159 unknown-horizons \- real-time strategy/simulation game 160 .SH "SYNOPSIS" 161 .HP \w'\fBunknown\-horizons\fR\ 'u 162 \fBunknown\-horizons\fR [{\fB\-h\fR\ |\ \fB\-\-help\fR}] 163 .SH "DESCRIPTION" 164 .PP 165 Isometric 2D real-time strategy/simulation fun. 166 .br 167 It puts emphasis on the economy and city building aspects. 168 .br 169 Expand your small settlement to a strong and wealthy colony, collect 170 taxes and supply your inhabitants with valuable goods. 171 .br 172 Increase your power with a well balanced economy, with strategic 173 trade and diplomacy. 174 .SH "OPTIONS"'''
175
176 - def format_heading(self, text):
177 """Format an option group..""" 178 if self.level == 0: 179 return '' 180 return r'''.TP 181 \fB{}\fR 182 '''.format(self._markup(text.upper()))
183
184 - def format_option(self, option, *args, **kwargs):
185 """Format a single option. 186 187 The base class takes care to replace custom optparse values.""" 188 result = [] 189 opts = self.option_strings[option] 190 help_text = self.expand_default(option) 191 result.append(r'''\ 192 .TP 193 .B 194 {} 195 {} 196 '''.format(self.optmarkup(opts), self._markup(help_text))) 197 198 return ''.join(result)
199 200 201 if __name__ == '__main__': 202 formatter = ManPageFormatter() 203 204 p = get_option_parser() 205 p.formatter = formatter 206 207 today = datetime.date.today() 208 print(r'''\ 209 '\" t 210 .\" Title: unknown-horizons 211 .\" Author: The Unknown Horizons Team <team@unknown-horizons.org> 212 .\" Date: {0} 213 .\" Language: English 214 .\" 215 .TH "UNKNOWN\-HORIZONS" "6" "{0}" "unknown-horizons" "Unknown Horizons User Commands" 216 '''.format(datetime.date.today())) 217 p.print_help() 218 219 print(r'''\ 220 .SH "BUGS" 221 .PP 222 The bugtracker can be found at \fBhttps://github.com/unknown-horizons/unknown-horizons/issues\fR\&. 223 .SH "AUTHOR" 224 .PP 225 \fBThe Unknown Horizons Team\fR <\&team@unknown-horizons\&.org\&> 226 .RS 4 227 .RE 228 .SH "COPYRIGHT" 229 .br 230 Copyright \(co 2008-2017 The Unknown Horizons Team 231 .br 232 .PP 233 Permission is granted to copy, distribute and/or modify this document under the 234 terms of the GNU General Public License, Version 3 or (at your option) any later 235 version published by the Free Software Foundation\&. 236 .sp 237 ''') 238