main.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/calmh/syncthing/github.com/jessevdk/go-flags"
  6. "os"
  7. "strconv"
  8. "strings"
  9. )
  10. type EditorOptions struct {
  11. Input string `short:"i" long:"input" description:"Input file" default:"-"`
  12. Output string `short:"o" long:"output" description:"Output file" default:"-"`
  13. }
  14. type Point struct {
  15. X, Y int
  16. }
  17. func (p *Point) UnmarshalFlag(value string) error {
  18. parts := strings.Split(value, ",")
  19. if len(parts) != 2 {
  20. return errors.New("Expected two numbers separated by a ,")
  21. }
  22. x, err := strconv.ParseInt(parts[0], 10, 32)
  23. if err != nil {
  24. return err
  25. }
  26. y, err := strconv.ParseInt(parts[1], 10, 32)
  27. if err != nil {
  28. return err
  29. }
  30. p.X = int(x)
  31. p.Y = int(y)
  32. return nil
  33. }
  34. func (p Point) MarshalFlag() (string, error) {
  35. return fmt.Sprintf("%d,%d", p.X, p.Y), nil
  36. }
  37. type Options struct {
  38. // Example of verbosity with level
  39. Verbose []bool `short:"v" long:"verbose" description:"Verbose output"`
  40. // Example of optional value
  41. User string `short:"u" long:"user" description:"User name" optional:"yes" optional-value:"pancake"`
  42. // Example of map with multiple default values
  43. Users map[string]string `long:"users" description:"User e-mail map" default:"system:[email protected]" default:"admin:[email protected]"`
  44. // Example of option group
  45. Editor EditorOptions `group:"Editor Options"`
  46. // Example of custom type Marshal/Unmarshal
  47. Point Point `long:"point" description:"A x,y point" default:"1,2"`
  48. }
  49. var options Options
  50. var parser = flags.NewParser(&options, flags.Default)
  51. func main() {
  52. if _, err := parser.Parse(); err != nil {
  53. os.Exit(1)
  54. }
  55. }