From 8e2ace6f82013149b74b64566581451c0a2ad948 Mon Sep 17 00:00:00 2001 From: Jamie McDonald Date: Mon, 30 Dec 2024 19:30:28 +0000 Subject: [PATCH] Add full set of CLI options --- .../kotlin/com/jdamcd/arrivals/cli/Cli.kt | 55 ++++++++++++++++--- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/cli/src/main/kotlin/com/jdamcd/arrivals/cli/Cli.kt b/cli/src/main/kotlin/com/jdamcd/arrivals/cli/Cli.kt index 2ba1ac0..7009263 100644 --- a/cli/src/main/kotlin/com/jdamcd/arrivals/cli/Cli.kt +++ b/cli/src/main/kotlin/com/jdamcd/arrivals/cli/Cli.kt @@ -2,7 +2,9 @@ package com.jdamcd.arrivals.cli import com.github.ajalt.clikt.command.SuspendingCliktCommand import com.github.ajalt.clikt.command.main -import com.github.ajalt.clikt.parameters.options.default +import com.github.ajalt.clikt.parameters.groups.OptionGroup +import com.github.ajalt.clikt.parameters.groups.defaultByName +import com.github.ajalt.clikt.parameters.groups.groupChoice import com.github.ajalt.clikt.parameters.options.option import com.github.ajalt.clikt.parameters.types.choice import com.jdamcd.arrivals.Arrivals @@ -26,17 +28,54 @@ private class Cli : private val arrivals: Arrivals by inject() private val settings: Settings by inject() - val mode by option().choice("tfl", "gtfs").default("tfl") + val mode by option() + .groupChoice("tfl" to Tfl(), "gtfs" to Gtfs()) + .defaultByName("tfl") override suspend fun run() { - if (mode == "gtfs") { - settings.mode = SettingsConfig.MODE_GTFS + configure() + try { + val result = arrivals.latest() + echo(result.station) + result.arrivals.forEach { + echo("%-24s\t%6s".format(it.destination, it.time)) + } + } catch (e: Exception) { + echo(e.message) } + } + + private fun configure() { + when (mode) { + is Tfl -> { + val mode = mode as Tfl + settings.mode = SettingsConfig.MODE_TFL + mode.stationId?.let { settings.tflStopId = it } + mode.platform?.let { settings.tflPlatform = it } + mode.direction?.let { settings.tflDirection = it } + } - val result = arrivals.latest() - echo(result.station) - result.arrivals.forEach { - echo("%-24s\t%6s".format(it.destination, it.time)) + is Gtfs -> { + val mode = mode as Gtfs + settings.mode = SettingsConfig.MODE_GTFS + mode.stopId?.let { settings.gtfsStop = it } + mode.realtimeUrl?.let { settings.gtfsRealtime = it } + mode.scheduleUrl?.let { settings.gtfsSchedule = it } + } } } } + +private sealed class TransitProvider(name: String) : OptionGroup(name) + +private class Tfl : TransitProvider("TfL options") { + val stationId by option() + val platform by option() + val direction by option().choice("inbound", "outbound", "all") +} + +private class Gtfs : TransitProvider("GTFS options") { + val stopId by option() + val realtimeUrl by option() + val scheduleUrl by option() +}