shell.ml 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. (*
  2. * shell.ml
  3. *
  4. * Created by Marcus Rohrmoser on 16.05.20.
  5. * Copyright © The geohash developers. All rights reserved.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *)
  20. (* https://caml.inria.fr/pub/docs/manual-ocaml/libref/Sys.html *)
  21. let err i msgs =
  22. let exe = Filename.basename Sys.executable_name in
  23. msgs |> List.cons exe |> String.concat ": " |> prerr_endline;
  24. i
  25. let to_hash h = Ok [ h; "not implemented yet." ]
  26. let print_version oc =
  27. (* not via dune-build-info dependency https://dune.readthedocs.io/en/stable/dune-libs.html#dune-build-info-library
  28. * but rather via a dune variable https://dune.readthedocs.io/en/stable/concepts.html#variables
  29. * written to Version.ml by a bin/dune stanza. *)
  30. let v = Version.dune_project_num in
  31. let exe = Filename.basename Sys.executable_name in
  32. Printf.fprintf oc "%s: https://mro.name/%s/v%s\n" exe "geohash" v;
  33. 0
  34. let print_help oc =
  35. let exe = Filename.basename Sys.executable_name in
  36. Printf.fprintf oc
  37. "\n\
  38. Convert one lat,lon pair or geohash to gpx with bbox and geohash comment.\n\n\
  39. Works as a webserver CGI or commandline converter.\n\n\
  40. If run from commandline:\n\n\
  41. SYNOPSIS\n\n\
  42. \ $ %s -V\n\n\
  43. \ $ %s -h\n\n\
  44. \ $ %s --doap\n\n\
  45. \ $ %s 'geo:47.67726,12.47077?z=19'\n\n"
  46. exe exe exe exe;
  47. 0
  48. let exec args =
  49. let oc = stdout in
  50. match args |> List.tl with
  51. | [ "-h" ] | [ "--help" ] -> print_help oc
  52. | [ "-V" ] | [ "--version" ] -> print_version oc
  53. | [ "--doap" ] ->
  54. let r n = n |> Res.read |> Option.value ~default:"" in
  55. Printf.fprintf oc "%s" (r "doap.rdf");
  56. 0
  57. | [ i ] ->
  58. (i |> to_hash |> function
  59. | Ok h -> h |> String.concat " -> " |> Printf.fprintf oc "%s"
  60. | Error _ -> "ouch" |> Printf.fprintf oc "%s");
  61. 0
  62. | _ -> err 2 [ "get help with -h" ]