@@ -2,7 +2,9 @@ package com.jdamcd.arrivals.cli
22
33import com.github.ajalt.clikt.command.SuspendingCliktCommand
44import com.github.ajalt.clikt.command.main
5- import com.github.ajalt.clikt.parameters.options.default
5+ import com.github.ajalt.clikt.parameters.groups.OptionGroup
6+ import com.github.ajalt.clikt.parameters.groups.defaultByName
7+ import com.github.ajalt.clikt.parameters.groups.groupChoice
68import com.github.ajalt.clikt.parameters.options.option
79import com.github.ajalt.clikt.parameters.types.choice
810import com.jdamcd.arrivals.Arrivals
@@ -26,17 +28,54 @@ private class Cli :
2628 private val arrivals: Arrivals by inject()
2729 private val settings: Settings by inject()
2830
29- val mode by option().choice(" tfl" , " gtfs" ).default(" tfl" )
31+ val mode by option()
32+ .groupChoice(" tfl" to Tfl (), " gtfs" to Gtfs ())
33+ .defaultByName(" tfl" )
3034
3135 override suspend fun run () {
32- if (mode == " gtfs" ) {
33- settings.mode = SettingsConfig .MODE_GTFS
36+ configure()
37+ try {
38+ val result = arrivals.latest()
39+ echo(result.station)
40+ result.arrivals.forEach {
41+ echo(" %-24s\t %6s" .format(it.destination, it.time))
42+ }
43+ } catch (e: Exception ) {
44+ echo(e.message)
3445 }
46+ }
47+
48+ private fun configure () {
49+ when (mode) {
50+ is Tfl -> {
51+ val mode = mode as Tfl
52+ settings.mode = SettingsConfig .MODE_TFL
53+ mode.stationId?.let { settings.tflStopId = it }
54+ mode.platform?.let { settings.tflPlatform = it }
55+ mode.direction?.let { settings.tflDirection = it }
56+ }
3557
36- val result = arrivals.latest()
37- echo(result.station)
38- result.arrivals.forEach {
39- echo(" %-24s\t %6s" .format(it.destination, it.time))
58+ is Gtfs -> {
59+ val mode = mode as Gtfs
60+ settings.mode = SettingsConfig .MODE_GTFS
61+ mode.stopId?.let { settings.gtfsStop = it }
62+ mode.realtimeUrl?.let { settings.gtfsRealtime = it }
63+ mode.scheduleUrl?.let { settings.gtfsSchedule = it }
64+ }
4065 }
4166 }
4267}
68+
69+ private sealed class TransitProvider (name : String ) : OptionGroup(name)
70+
71+ private class Tfl : TransitProvider (" TfL options" ) {
72+ val stationId by option()
73+ val platform by option()
74+ val direction by option().choice(" inbound" , " outbound" , " all" )
75+ }
76+
77+ private class Gtfs : TransitProvider (" GTFS options" ) {
78+ val stopId by option()
79+ val realtimeUrl by option()
80+ val scheduleUrl by option()
81+ }
0 commit comments