Allow user to select repository file
John Smith

John Smith commited on 2021-355 22:15:38
Showing 1 changed files, with 12 additions and 5 deletions.


Whilst a hardcoded default remains, the second command-line argument
is now read as a repository path.
Errors in loading decks are now handled with error messages.
... ...
@@ -39,11 +39,10 @@ else
39 39
     action = "h"
40 40
 end
41 41
 
42
---[[
43
-    repository file is hardcoded for now;
44
-    TODO: should be controllable by user in the future
45
-]]
46 42
 local deckfname = "example.rec"
43
+if arg[2] then
44
+    deckfname = arg[2]
45
+end
47 46
 
48 47
 --[[
49 48
     do requested action
... ...
@@ -51,6 +50,7 @@ local deckfname = "example.rec"
51 50
 -- Add card(s)
52 51
 if action == "a" then
53 52
     local deck = loader.load(deckfname)
53
+    if deck then
54 54
         io.write("Reading prompts -- press Ctrl-D to stop\n")
55 55
         local rt = io.read("a")
56 56
         local cardctr = 0
... ...
@@ -73,15 +73,19 @@ if action == "a" then
73 73
         if cardctr > 0 then
74 74
             loader.save(deckfname, deck)
75 75
         end
76
+    else
77
+        io.write("Failed to load deck\n")
78
+    end
76 79
 -- Help
77 80
 elseif action == "h" then
78 81
     io.write(string.format(
79
-        "Usage: %s ACTION\n\nACTION may be one of\n    (a)dd card\n    (h)elp\n    (r)eview session\n    (s)tatistics\n",
82
+        "Usage: %s ACTION [REPO]\n\nACTION may be one of\n    (a)dd card\n    (h)elp\n    (r)eview session\n    (s)tatistics\n",
80 83
         arg[0]
81 84
     ))
82 85
 -- Review session
83 86
 elseif action == "r" then
84 87
     local deck = loader.load(deckfname)
88
+    if deck then
85 89
         local cardcount = 0
86 90
         local start = os.time()
87 91
         -- review each card that needs it
... ...
@@ -120,6 +124,9 @@ elseif action == "r" then
120 124
             ))
121 125
             loader.save(deckfname, deck)
122 126
         end
127
+    else
128
+        io.write("Failed to load deck\n")
129
+    end
123 130
 -- Statistics
124 131
 elseif action == "s" then
125 132
     -- TODO
126 133