From 77774eed6639d2607244b6fbed2f4dda0540a989 Mon Sep 17 00:00:00 2001 From: Miles Alan Date: Thu, 20 Jun 2024 11:28:45 -0400 Subject: [PATCH] Further cleanup CLI, break consts out to seperate file --- build.sh | 2 +- cli/const.go | 35 ++++++++++++++++++++++++ cli/mobroutecli.go | 66 +++++++++++----------------------------------- 3 files changed, 51 insertions(+), 52 deletions(-) create mode 100644 cli/const.go diff --git a/build.sh b/build.sh index 3450a71..ee04399 100755 --- a/build.sh +++ b/build.sh @@ -53,7 +53,7 @@ pp() { } build() { - go build -o mobroute -tags=sqlite_math_functions -buildvcs=false cli/mobroutecli.go + go build -o mobroute -tags=sqlite_math_functions -buildvcs=false cli/* } run() { diff --git a/cli/const.go b/cli/const.go new file mode 100644 index 0000000..840d05e --- /dev/null +++ b/cli/const.go @@ -0,0 +1,35 @@ +package main + +const ( + subcmdRouteExamples = ` + - Brussels Routing Example: + {"route_params":{"feed_ids":[1088],"from":[50.85728,4.351426],"to":[50.83214,4.350534],"transfer_categories":["f","i"],"output_formats":["legs","diagnostics","mapurl","request"]}} + + - Krakow Routing Example: + {"route_params":{"feed_ids":[1270],"from":[50.012338,19.88192],"to":[50.08785,20.02387],"transfer_categories":["f","i"],"output_formats":["legs","diagnostics","mapurl","request"]}} + + - NYC Routing Example: + {"route_params":{"feed_ids":[516],"from":[40.70940,-74.00537],"to":[40.72879,-73.95215],"transfer_categories":["f","i"],"output_formats":["legs","diagnostics","mapurl","request"]}} + ` + + subcmdRouteDoc = ` +JSON Parameters for oneshot route subcommand, should be in format of: {"route_params": {"feed_ids": [516],...}} + +Valid route parameters: + - feed_ids: Array of feed IDs (MDBID) to use for routing requests (default: [516]) + - from: Object with two float properties, lat & lon (default: [40.70940, -74.00537]) + - to: Object with two float properties, lat & lon (default: [40.72879, -73.95215]) + - time: Datetime to depart from trip, if unset defaults to current time / now (default: null) + - max_walk_seconds: Maximum seconds walking distance on start/end of trip to get to/from origin/destination stops (default: 1200) + - max_trip_seconds: Maximum seconds the entire route/trip can take (default: 14400) + - min_transfer_seconds: Minimum seconds each transfer in the route can take (default: 180) + - max_transfer_seconds: Maximum seconds each transfer in the route can take (default: 2400) + - max_n_transfers: Maximum number of transfers a route can contain (default: 20) + - walkspeed_km_hr: Ratio of kilometers per hour the user walk (default: 4.5) + - transfer_categories: Array of transfers categories to load - "f" for feed, "i" for implicit, "g" for generated (default: ["f", "i"]) + - cache_dir: Directory to store sqlite db, defaults to ~/.cache/mobroute if null (default: null) + - output_formats: Array of formats for the output may include - "legs", "geojson", "diagnostics", "mapurl", "request" (default: ["legs"]) + ` + + subcmdMobsqlDoc = `JSON Parameters for mobsql subcommands` +) diff --git a/cli/mobroutecli.go b/cli/mobroutecli.go index e125a5b..4be6d32 100644 --- a/cli/mobroutecli.go +++ b/cli/mobroutecli.go @@ -23,54 +23,16 @@ func main() { jsonBytes []byte commandData any err error - flagBaseDebugCategories string - flagBaseDebugProfile string - flagBaseOutputFormat string - - defaultJSONBufferRoute = `{"route_params": {}}` - defaultJSONBufferMobsql = `{"mobsql_params": {"feed_ids": [], "op": "status"}}` - - examplesSubcommandRoute = ` - - Brussels Routing Example: - {"route_params":{"feed_ids":[1088],"from":[50.85728,4.351426],"to":[50.83214,4.350534],"transfer_categories":["f","i"],"output_formats":["legs","diagnostics","mapurl","request"]}} - - - Krakow Routing Example: - {"route_params":{"feed_ids":[1270],"from":[50.012338,19.88192],"to":[50.08785,20.02387],"transfer_categories":["f","i"],"output_formats":["legs","diagnostics","mapurl","request"]}} - - - NYC Routing Example: - {"route_params":{"feed_ids":[516],"from":[40.70940,-74.00537],"to":[40.72879,-73.95215],"transfer_categories":["f","i"],"output_formats":["legs","diagnostics","mapurl","request"]}} - ` - - flagMobsqlSet = flag.NewFlagSet("mobsql", flag.ExitOnError) - flagMobsqlParams = flagMobsqlSet.String( - "mp", defaultJSONBufferMobsql, - `JSON Parameters for mobsql subcommands`, - ) - - flagRouteSet = flag.NewFlagSet("route", flag.ExitOnError) - flagRouteParams = flagRouteSet.String( - "rp", - defaultJSONBufferRoute, - `JSON Parameters for oneshot route subcommand, should be in format of: {"route_params": {"feed_ids": [516],...}} - -Valid route parameters: - - feed_ids: Array of feed IDs (MDBID) to use for routing requests (default: [516]) - - from: Object with two float properties, lat & lon (default: [40.70940, -74.00537]) - - to: Object with two float properties, lat & lon (default: [40.72879, -73.95215]) - - time: Datetime to depart from trip, if unset defaults to current time / now (default: null) - - max_walk_seconds: Maximum seconds walking distance on start/end of trip to get to/from origin/destination stops (default: 1200) - - max_trip_seconds: Maximum seconds the entire route/trip can take (default: 14400) - - min_transfer_seconds: Minimum seconds each transfer in the route can take (default: 180) - - max_transfer_seconds: Maximum seconds each transfer in the route can take (default: 2400) - - max_n_transfers: Maximum number of transfers a route can contain (default: 20) - - walkspeed_km_hr: Ratio of kilometers per hour the user walk (default: 4.5) - - transfer_categories: Array of transfers categories to load - "f" for feed, "i" for implicit, "g" for generated (default: ["f", "i"]) - - cache_dir: Directory to store sqlite db, defaults to ~/.cache/mobroute if null (default: null) - - output_formats: Array of formats for the output may include - "legs", "geojson", "diagnostics", "mapurl", "request" (default: ["legs"]) - `) - ) + flagBaseDebugCategories, flagBaseDebugProfile, flagBaseOutputFormat string + + flagMobsqlSet = flag.NewFlagSet("mobsql", flag.ExitOnError) + flagMobsqlParamsDefault = `{"mobsql_params": {"feed_ids": [], "op": "status"}}` + flagMobsqlParams = flagMobsqlSet.String("mp", flagMobsqlParamsDefault, subcmdMobsqlDoc) - _ = examplesSubcommandRoute + flagRouteSet = flag.NewFlagSet("route", flag.ExitOnError) + flagRouteParamsDefault = `{"route_params": {}}` + flagRouteParams = flagRouteSet.String("rp", flagRouteParamsDefault, subcmdRouteDoc) + ) // Same usage for all subcommands for _, f := range []*flag.FlagSet{flagRouteSet} { @@ -80,12 +42,13 @@ Valid route parameters: } validSubcommands := []string{ + // -version "version", - // -route -routeparams {} + // -route -rp '{}' "route", - // -mobsql_{status,load,compute,purgegtfs,purgecomputed,purgeall} {} + // -mobsql_{status,load,compute,purgegtfs,purgecomputed,purgeall} -mp '{}' "mobsql_status", "mobsql_load", "mobsql_compute", @@ -103,7 +66,7 @@ Valid route parameters: if os.Args[1] == "route" { var jsonBuffer string flagRouteSet.Parse(os.Args[2:]) - if *flagRouteParams == defaultJSONBufferRoute { + if *flagRouteParams == flagRouteParamsDefault { flagRouteSet.Usage() return } else { @@ -126,7 +89,7 @@ Valid route parameters: "mobsql_purgegtfs", "mobsql_purgecomputed", "mobsql_purgeall", }) { var jsonBuffer string - if *flagMobsqlParams == defaultJSONBufferMobsql { + if *flagMobsqlParams == flagMobsqlParamsDefault { flagMobsqlSet.Usage() return } else { @@ -201,3 +164,4 @@ func globalsSetup(debugCategories, debugProfile string) func() { return func() {} } + -- 2.45.2