changeset 6:cd32e08fa37f

Add -latfirst option.
author David Barts <n5jrn@me.com>
date Wed, 25 Aug 2021 08:26:05 -0700
parents a725bd48bc0b
children 63eab373d348
files MakeWaypoints.kt
diffstat 1 files changed, 25 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/MakeWaypoints.kt	Wed Aug 25 07:44:53 2021 -0700
+++ b/MakeWaypoints.kt	Wed Aug 25 08:26:05 2021 -0700
@@ -16,15 +16,29 @@
 }
 
 fun main(args: Array<String>): Unit {
-    if (args.size != 1) {
-        System.err.println("${MYNAME}: expecting a single file name")
+    /* parse arguments */
+    val syntaxError = "${MYNAME}: syntax: [-latfirst] file"
+    if (args.size < 1 || args.size > 2) {
+        System.err.println(syntaxError)
         System.exit(2)
     }
+    var latFirst = false
+    var fileArg = 0
+    if (args[0].startsWith('-')) {
+        latFirst = args[0].equals("-latfirst", ignoreCase = true)
+        if (!latFirst || args.size != 2) {
+            System.err.println(syntaxError)
+            System.exit(2)
+        }
+        fileArg = 1
+    }
+    val outName = args[fileArg]
 
+    /* explain purpose */
     println("Enter waypoints below, end with empty line.")
     println()
 
-    val outName = args[0]
+    /* process input from user */
     val invalid = "${MYNAME}: invalid entry, please try again"
     FileOutputStream(outName).use stream@{
         XMLOutputFactory.newInstance().createXMLStreamWriter(it, CHARSET).run {
@@ -35,7 +49,10 @@
             writeDefaultNamespace("http://www.topografix.com/GPX/1/1")
 
             while (true) {
-                print("Longitude, latitude: ")
+                if (latFirst)
+                    print("Latitude, longitude: ")
+                else
+                    print("Longitude, latitude: ")
                 val longLat = readLine()
                 if (longLat.isNullOrEmpty()) {
                     writeEndDocument()
@@ -47,8 +64,10 @@
                     System.err.println(invalid)
                     continue
                 }
-                val long = parts[0].trim()
-                val lat = parts[1].trim()
+                val (lat, long) = if (latFirst)
+                    Pair(parts[0].trim(), parts[1].trim())
+                else
+                    Pair(parts[1].trim(), parts[0].trim())
                 if (lat.toDoubleOrNull() == null || long.toDoubleOrNull() == null) {
                     System.err.println(invalid)
                     continue