help_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package flags
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "os/exec"
  9. "testing"
  10. "time"
  11. )
  12. func helpDiff(a, b string) (string, error) {
  13. atmp, err := ioutil.TempFile("", "help-diff")
  14. if err != nil {
  15. return "", err
  16. }
  17. btmp, err := ioutil.TempFile("", "help-diff")
  18. if err != nil {
  19. return "", err
  20. }
  21. if _, err := io.WriteString(atmp, a); err != nil {
  22. return "", err
  23. }
  24. if _, err := io.WriteString(btmp, b); err != nil {
  25. return "", err
  26. }
  27. ret, err := exec.Command("diff", "-u", "-d", "--label", "got", atmp.Name(), "--label", "expected", btmp.Name()).Output()
  28. os.Remove(atmp.Name())
  29. os.Remove(btmp.Name())
  30. return string(ret), nil
  31. }
  32. type helpOptions struct {
  33. Verbose []bool `short:"v" long:"verbose" description:"Show verbose debug information" ini-name:"verbose"`
  34. Call func(string) `short:"c" description:"Call phone number" ini-name:"call"`
  35. PtrSlice []*string `long:"ptrslice" description:"A slice of pointers to string"`
  36. OnlyIni string `ini-name:"only-ini" description:"Option only available in ini"`
  37. Other struct {
  38. StringSlice []string `short:"s" description:"A slice of strings"`
  39. IntMap map[string]int `long:"intmap" description:"A map from string to int" ini-name:"int-map"`
  40. } `group:"Other Options"`
  41. }
  42. func TestHelp(t *testing.T) {
  43. var opts helpOptions
  44. p := NewNamedParser("TestHelp", HelpFlag)
  45. p.AddGroup("Application Options", "The application options", &opts)
  46. _, err := p.ParseArgs([]string{"--help"})
  47. if err == nil {
  48. t.Fatalf("Expected help error")
  49. }
  50. if e, ok := err.(*Error); !ok {
  51. t.Fatalf("Expected flags.Error, but got %#T", err)
  52. } else {
  53. if e.Type != ErrHelp {
  54. t.Errorf("Expected flags.ErrHelp type, but got %s", e.Type)
  55. }
  56. expected := `Usage:
  57. TestHelp [OPTIONS]
  58. Application Options:
  59. -v, --verbose Show verbose debug information
  60. -c= Call phone number
  61. --ptrslice= A slice of pointers to string
  62. Other Options:
  63. -s= A slice of strings
  64. --intmap= A map from string to int
  65. Help Options:
  66. -h, --help Show this help message
  67. `
  68. if e.Message != expected {
  69. ret, err := helpDiff(e.Message, expected)
  70. if err != nil {
  71. t.Errorf("Unexpected diff error: %s", err)
  72. t.Errorf("Unexpected help message, expected:\n\n%s\n\nbut got\n\n%s", expected, e.Message)
  73. } else {
  74. t.Errorf("Unexpected help message:\n\n%s", ret)
  75. }
  76. }
  77. }
  78. }
  79. func TestMan(t *testing.T) {
  80. var opts helpOptions
  81. p := NewNamedParser("TestMan", HelpFlag)
  82. p.ShortDescription = "Test manpage generation"
  83. p.LongDescription = "This is a somewhat longer description of what this does"
  84. p.AddGroup("Application Options", "The application options", &opts)
  85. var buf bytes.Buffer
  86. p.WriteManPage(&buf)
  87. got := buf.String()
  88. tt := time.Now()
  89. expected := fmt.Sprintf(`.TH TestMan 1 "%s"
  90. .SH NAME
  91. TestMan \- Test manpage generation
  92. .SH SYNOPSIS
  93. \fBTestMan\fP [OPTIONS]
  94. .SH DESCRIPTION
  95. This is a somewhat longer description of what this does
  96. .SH OPTIONS
  97. .TP
  98. \fB-v, --verbose\fP
  99. Show verbose debug information
  100. .TP
  101. \fB-c\fP
  102. Call phone number
  103. .TP
  104. \fB--ptrslice\fP
  105. A slice of pointers to string
  106. .TP
  107. \fB-s\fP
  108. A slice of strings
  109. .TP
  110. \fB--intmap\fP
  111. A map from string to int
  112. `, tt.Format("2 January 2006"))
  113. if got != expected {
  114. ret, err := helpDiff(got, expected)
  115. if err != nil {
  116. t.Errorf("Unexpected man page, expected:\n\n%s\n\nbut got\n\n%s", expected, got)
  117. } else {
  118. t.Errorf("Unexpected man page:\n\n%s", ret)
  119. }
  120. }
  121. }