Add save-game loading.
[naja.git] / naja / options.py
index 6fe75660987b364ab138dc5144156123c72d9b3c..d37f1be1d459861b37db7ff5552d6290b44f1170 100644 (file)
@@ -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.
@@ -38,10 +73,12 @@ 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,
+        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,
-                          help='Start with a specific save game loaded')
+        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)