changeset 10:cbe4c797c9a6

Fix bug in parsing --length
author David Barts <n5jrn@me.com>
date Sat, 01 Oct 2022 20:43:59 -0700
parents 72619175004e
children c69665ff37d0
files src/main/kotlin/name/blackcap/passman/CreateSubcommand.kt src/main/kotlin/name/blackcap/passman/UpdateSubcommand.kt
diffstat 2 files changed, 12 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/kotlin/name/blackcap/passman/CreateSubcommand.kt	Sat Oct 01 09:57:23 2022 -0700
+++ b/src/main/kotlin/name/blackcap/passman/CreateSubcommand.kt	Sat Oct 01 20:43:59 2022 -0700
@@ -38,8 +38,10 @@
             val length = try {
                 rawLength?.toInt() ?: DEFAULT_GENERATED_LENGTH
             } catch (e: NumberFormatException) {
+                -1
+            }
+            if (length < MIN_GENERATED_LENGTH) {
                 die("${see(rawLength)} - invalid length")
-                -1  /* will never happen */
             }
             Entry.withGeneratedPassword(length,
                 commandLine.hasOption(SYMBOLS),
--- a/src/main/kotlin/name/blackcap/passman/UpdateSubcommand.kt	Sat Oct 01 09:57:23 2022 -0700
+++ b/src/main/kotlin/name/blackcap/passman/UpdateSubcommand.kt	Sat Oct 01 20:43:59 2022 -0700
@@ -57,14 +57,15 @@
         db = Database.open()
         nameIn = commandLine.args[0]
         id = db.makeKey(nameIn)
-        length = commandLine.getOptionValue(LENGTH)?.let { rawLength ->
-            try {
-                rawLength.toInt()
-            } catch (e: NumberFormatException) {
-                die("${see(rawLength)} - invalid length")
-                -1  /* will never happen */
-            }
-        } ?: DEFAULT_GENERATED_LENGTH
+        val rawLength = commandLine.getOptionValue(LENGTH)
+        length = try {
+            rawLength?.toInt() ?: DEFAULT_GENERATED_LENGTH
+        } catch (e: NumberFormatException) {
+            -1
+        }
+        if (length < MIN_GENERATED_LENGTH) {
+            die("${see(rawLength)} - invalid length")
+        }
     }
 
     private fun checkArguments(): Unit {