cfg.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package zygo
  2. import (
  3. "flag"
  4. )
  5. // configure a glisp repl
  6. type ZlispConfig struct {
  7. CpuProfile string
  8. MemProfile string
  9. ExitOnFailure bool
  10. CountFuncCalls bool
  11. Flags *flag.FlagSet
  12. ExtensionsVersion string
  13. Command string
  14. Sandboxed bool
  15. Quiet bool
  16. Trace bool
  17. LoadDemoStructs bool
  18. AfterScriptDontExit bool
  19. // liner bombs under emacs, avoid it with this flag.
  20. NoLiner bool
  21. Prompt string // default "zygo> "
  22. }
  23. func NewZlispConfig(cmdname string) *ZlispConfig {
  24. return &ZlispConfig{
  25. Flags: flag.NewFlagSet(cmdname, flag.ExitOnError),
  26. }
  27. }
  28. // call DefineFlags before myflags.Parse()
  29. func (c *ZlispConfig) DefineFlags() {
  30. c.Flags.StringVar(&c.CpuProfile, "cpuprofile", "", "write cpu profile to file")
  31. c.Flags.StringVar(&c.MemProfile, "memprofile", "", "write mem profile to file")
  32. c.Flags.BoolVar(&c.ExitOnFailure, "exitonfail", false, "exit on failure instead of starting repl")
  33. c.Flags.BoolVar(&c.CountFuncCalls, "countcalls", false, "count how many times each function is run")
  34. c.Flags.StringVar(&c.Command, "c", "", "expressions to evaluate")
  35. c.Flags.BoolVar(&c.AfterScriptDontExit, "i", false, "after running the command line script, remain interactive rather than exiting")
  36. c.Flags.BoolVar(&c.Sandboxed, "sandbox", false, "run sandboxed; disallow system/external interaction functions")
  37. c.Flags.BoolVar(&c.Quiet, "quiet", false, "start repl without printing the version/mode/help banner")
  38. c.Flags.BoolVar(&c.Trace, "trace", false, "trace execution (warning: very verbose and slow)")
  39. c.Flags.BoolVar(&c.LoadDemoStructs, "demo", false, "load the demo structs: Event, Snoopy, Hornet, Weather and friends.")
  40. }
  41. // call c.ValidateConfig() after myflags.Parse()
  42. func (c *ZlispConfig) ValidateConfig() error {
  43. if c.Prompt == "" {
  44. c.Prompt = "zygo> "
  45. }
  46. return nil
  47. }