ecal.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package main
  11. import (
  12. "flag"
  13. "fmt"
  14. "os"
  15. "devt.de/krotik/ecal/cli/tool"
  16. "devt.de/krotik/ecal/config"
  17. )
  18. func main() {
  19. tool.RunPackedBinary() // See if we try to run a standalone binary
  20. // Initialize the default command line parser
  21. flag.CommandLine.Init(os.Args[0], flag.ContinueOnError)
  22. // Define default usage message
  23. flag.Usage = func() {
  24. // Print usage for tool selection
  25. fmt.Println(fmt.Sprintf("Usage of %s <tool>", os.Args[0]))
  26. fmt.Println()
  27. fmt.Println(fmt.Sprintf("ECAL %v - Event Condition Action Language", config.ProductVersion))
  28. fmt.Println()
  29. fmt.Println("Available commands:")
  30. fmt.Println()
  31. fmt.Println(" console Interactive console (default)")
  32. fmt.Println(" debug Run in debug mode")
  33. fmt.Println(" format Format all ECAL files in a directory structure")
  34. fmt.Println(" pack Create a single executable from ECAL code")
  35. fmt.Println(" run Execute ECAL code")
  36. fmt.Println()
  37. fmt.Println(fmt.Sprintf("Use %s <command> -help for more information about a given command.", os.Args[0]))
  38. fmt.Println()
  39. }
  40. // Parse the command bit
  41. if err := flag.CommandLine.Parse(os.Args[1:]); err == nil {
  42. interpreter := tool.NewCLIInterpreter()
  43. if len(flag.Args()) > 0 {
  44. arg := flag.Args()[0]
  45. if arg == "console" {
  46. err = interpreter.Interpret(true)
  47. } else if arg == "run" {
  48. err = interpreter.Interpret(false)
  49. } else if arg == "debug" {
  50. debugInterpreter := tool.NewCLIDebugInterpreter(interpreter)
  51. err = debugInterpreter.Interpret()
  52. } else if arg == "pack" {
  53. packer := tool.NewCLIPacker()
  54. err = packer.Pack()
  55. } else if arg == "format" {
  56. err = tool.Format()
  57. } else {
  58. flag.Usage()
  59. }
  60. } else if err == nil {
  61. err = interpreter.Interpret(true)
  62. }
  63. if err != nil {
  64. fmt.Println(fmt.Sprintf("Error: %v", err))
  65. }
  66. }
  67. }