options.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "github.com/pborman/getopt/v2"
  8. )
  9. type Options struct {
  10. Help bool
  11. Version bool
  12. Servers []string
  13. Split bool
  14. KeepOld bool
  15. OutputDir string
  16. }
  17. func Help() {
  18. getopt.PrintUsage(os.Stdout)
  19. os.Exit(0)
  20. }
  21. func Version() {
  22. fmt.Printf("masto-emoji-pack v%s\n", VERSION)
  23. os.Exit(0)
  24. }
  25. func Usage(err error) {
  26. fmt.Fprintln(os.Stderr, err)
  27. getopt.Usage()
  28. os.Exit(2)
  29. }
  30. func Parse() {
  31. if err := getopt.Getopt(nil); err != nil {
  32. Usage(err)
  33. }
  34. }
  35. func parseOptions() (options Options) {
  36. getopt.SetParameters("DOMAIN...")
  37. help := getopt.BoolLong("help", 'h', "show help message")
  38. version := getopt.BoolLong("version", 'v', "show version info")
  39. split := getopt.BoolLong("split", 's', "split emoji pack via category")
  40. keepOld := getopt.BoolLong("keep-old", 'k', "keep old files")
  41. dir := getopt.StringLong("path", 'p', os.TempDir(), "generate emoji pack directory", "PATH")
  42. Parse()
  43. options = Options{
  44. Help: *help,
  45. Version: *version,
  46. Servers: getopt.Args(),
  47. Split: *split,
  48. KeepOld: *keepOld,
  49. OutputDir: filepath.Clean(*dir),
  50. }
  51. if options.Help {
  52. Help()
  53. }
  54. if options.Version {
  55. Version()
  56. }
  57. if len(options.Servers) == 0 {
  58. Usage(errors.New("must be specified: DOMAIN..."))
  59. }
  60. return
  61. }