X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;ds=sidebyside;f=naja%2Foptions.py;h=d37f1be1d459861b37db7ff5552d6290b44f1170;hb=a97bdc61b9c4049e2aa20278c814b8bb8b8273cc;hp=4c5abea84372449ce6fe57f6390962fb32fbf4b6;hpb=cdb82efa9692569bc506eb48c7540a83365a7bb3;p=naja.git diff --git a/naja/options.py b/naja/options.py index 4c5abea..d37f1be 100644 --- a/naja/options.py +++ b/naja/options.py @@ -9,6 +9,41 @@ 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): + ''' + 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 + from naja.scenes.load_save import SaveGameSlot + state = SaveGameSlot(value).load() + if state is None: + raise optparse.OptionValueError( + "Could not load game from slot %s" % (value,)) + parser.values.game_state = state + + +def load_deck(option, opt_str, value, parser): + ''' + Create a new game for a specific deck and store it in + parser.values.game_state. + ''' + raise optparse.OptionalValueError( + "Deck loading not implemented.") + + def parse_args(args): ''' Parse arguments and store them in the options dictionary. @@ -35,14 +70,21 @@ def parse_args(args): if options.debug: parser.add_option('--initial-bits', type=int, help='Initial player bits') + 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, + help='Start with a new game for a specific deck') + parser.add_option('--load', default=None, type=int, action="callback", + callback=load_game, + help='Start with a specific save game loaded (0-7)') opts, _ = parser.parse_args(args) for k in DEFAULTS: if getattr(opts, k, None) is not None: options[k] = getattr(opts, k) - options['save_location'] = opts.save_location - options['cheat_enabled'] = False def _get_default_save_location():