Skip to content

Commit

Permalink
Add full set of CLI options
Browse files Browse the repository at this point in the history
  • Loading branch information
jdamcd committed Dec 30, 2024
1 parent c2618dc commit 8e2ace6
Showing 1 changed file with 47 additions and 8 deletions.
55 changes: 47 additions & 8 deletions cli/src/main/kotlin/com/jdamcd/arrivals/cli/Cli.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}

0 comments on commit 8e2ace6

Please sign in to comment.