From d49a628441668b9bb36129f7ef6a39d32ca4d75b Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Sat, 17 May 2014 13:35:50 +0200 Subject: [PATCH] Saner save game loading. --- naja/options.py | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/naja/options.py b/naja/options.py index d37f1be..efde88c 100644 --- a/naja/options.py +++ b/naja/options.py @@ -9,33 +9,21 @@ from naja.constants import DEFAULTS options = AttrDict() -def check_min_max(option, value, min_value, max_value): - ''' - Check value lies between min and max and raise OptionValueError if it does - not. - ''' - if not (min_value <= value <= max_value): - raise optparse.OptionValueError( - "Value of %s should be between %s and %s but got: %r" - % (option.dest, min, max, value)) - - -def load_game(option, opt_str, value, parser): +def load_game(parser, slot_num): ''' Load a save game and store it in parser.values.game_state. ''' - check_min_max(option, value, 0, 7) - # madness takes its toll ... - options.save_location = parser.values.save_location + if not (0 <= slot_num <= 7): + parser.error("--load accepts a slot number from 0 to 7.") from naja.scenes.load_save import SaveGameSlot - state = SaveGameSlot(value).load() + state = SaveGameSlot(slot_num).load() if state is None: - raise optparse.OptionValueError( - "Could not load game from slot %s" % (value,)) - parser.values.game_state = state + raise parser.error( + "Could not load game from slot %s" % (slot_num,)) + options.game_state = state -def load_deck(option, opt_str, value, parser): +def load_deck(parser, deck): ''' Create a new game for a specific deck and store it in parser.values.game_state. @@ -73,11 +61,9 @@ def parse_args(args): parser.add_option('--cheat-enabled', default=False, action='store_true', help='For those too lazy to type the KONAMI code') - parser.add_option('--deck', default=None, action="callback", - callback=load_deck, + parser.add_option('--deck', default=None, help='Start with a new game for a specific deck') - parser.add_option('--load', default=None, type=int, action="callback", - callback=load_game, + parser.add_option('--load', default=None, type=int, help='Start with a specific save game loaded (0-7)') opts, _ = parser.parse_args(args) @@ -86,6 +72,11 @@ def parse_args(args): if getattr(opts, k, None) is not None: options[k] = getattr(opts, k) + if opts.load is not None: + load_game(parser, opts.load) + if opts.deck is not None: + load_deck(parser, opts.deck) + def _get_default_save_location(): """Return a default save game location.""" -- 2.34.1