printusage_test.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package pflag
  2. import (
  3. "bytes"
  4. "io"
  5. "testing"
  6. )
  7. const expectedOutput = ` --long-form Some description
  8. --long-form2 Some description
  9. with multiline
  10. -s, --long-name Some description
  11. -t, --long-name2 Some description with
  12. multiline
  13. `
  14. func setUpPFlagSet(buf io.Writer) *FlagSet {
  15. f := NewFlagSet("test", ExitOnError)
  16. f.Bool("long-form", false, "Some description")
  17. f.Bool("long-form2", false, "Some description\n with multiline")
  18. f.BoolP("long-name", "s", false, "Some description")
  19. f.BoolP("long-name2", "t", false, "Some description with\n multiline")
  20. f.SetOutput(buf)
  21. return f
  22. }
  23. func TestPrintUsage(t *testing.T) {
  24. buf := bytes.Buffer{}
  25. f := setUpPFlagSet(&buf)
  26. f.PrintDefaults()
  27. res := buf.String()
  28. if res != expectedOutput {
  29. t.Errorf("Expected \n%s \nActual \n%s", expectedOutput, res)
  30. }
  31. }
  32. func setUpPFlagSet2(buf io.Writer) *FlagSet {
  33. f := NewFlagSet("test", ExitOnError)
  34. f.Bool("long-form", false, "Some description")
  35. f.Bool("long-form2", false, "Some description\n with multiline")
  36. f.BoolP("long-name", "s", false, "Some description")
  37. f.BoolP("long-name2", "t", false, "Some description with\n multiline")
  38. f.StringP("some-very-long-arg", "l", "test", "Some very long description having break the limit")
  39. f.StringP("other-very-long-arg", "o", "long-default-value", "Some very long description having break the limit")
  40. f.String("some-very-long-arg2", "very long default value", "Some very long description\nwith line break\nmultiple")
  41. f.SetOutput(buf)
  42. return f
  43. }
  44. const expectedOutput2 = ` --long-form Some description
  45. --long-form2 Some description
  46. with multiline
  47. -s, --long-name Some description
  48. -t, --long-name2 Some description with
  49. multiline
  50. -o, --other-very-long-arg string Some very long description having
  51. break the limit (default
  52. "long-default-value")
  53. -l, --some-very-long-arg string Some very long description having
  54. break the limit (default "test")
  55. --some-very-long-arg2 string Some very long description
  56. with line break
  57. multiple (default "very long default
  58. value")
  59. `
  60. func TestPrintUsage_2(t *testing.T) {
  61. buf := bytes.Buffer{}
  62. f := setUpPFlagSet2(&buf)
  63. res := f.FlagUsagesWrapped(80)
  64. if res != expectedOutput2 {
  65. t.Errorf("Expected \n%q \nActual \n%q", expectedOutput2, res)
  66. }
  67. }