command_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package flags
  2. import (
  3. "testing"
  4. )
  5. func TestCommandInline(t *testing.T) {
  6. var opts = struct {
  7. Value bool `short:"v"`
  8. Command struct {
  9. G bool `short:"g"`
  10. } `command:"cmd"`
  11. }{}
  12. p, ret := assertParserSuccess(t, &opts, "-v", "cmd", "-g")
  13. assertStringArray(t, ret, []string{})
  14. if p.Active == nil {
  15. t.Errorf("Expected active command")
  16. }
  17. if !opts.Value {
  18. t.Errorf("Expected Value to be true")
  19. }
  20. if !opts.Command.G {
  21. t.Errorf("Expected Command.G to be true")
  22. }
  23. if p.Command.Find("cmd") != p.Active {
  24. t.Errorf("Expected to find command `cmd' to be active")
  25. }
  26. }
  27. func TestCommandInlineMulti(t *testing.T) {
  28. var opts = struct {
  29. Value bool `short:"v"`
  30. C1 struct {
  31. } `command:"c1"`
  32. C2 struct {
  33. G bool `short:"g"`
  34. } `command:"c2"`
  35. }{}
  36. p, ret := assertParserSuccess(t, &opts, "-v", "c2", "-g")
  37. assertStringArray(t, ret, []string{})
  38. if p.Active == nil {
  39. t.Errorf("Expected active command")
  40. }
  41. if !opts.Value {
  42. t.Errorf("Expected Value to be true")
  43. }
  44. if !opts.C2.G {
  45. t.Errorf("Expected C2.G to be true")
  46. }
  47. if p.Command.Find("c1") == nil {
  48. t.Errorf("Expected to find command `c1'")
  49. }
  50. if c2 := p.Command.Find("c2"); c2 == nil {
  51. t.Errorf("Expected to find command `c2'")
  52. } else if c2 != p.Active {
  53. t.Errorf("Expected to find command `c2' to be active")
  54. }
  55. }
  56. func TestCommandFlagOrder1(t *testing.T) {
  57. var opts = struct {
  58. Value bool `short:"v"`
  59. Command struct {
  60. G bool `short:"g"`
  61. } `command:"cmd"`
  62. }{}
  63. assertParseFail(t, ErrUnknownFlag, "unknown flag `g'", &opts, "-v", "-g", "cmd")
  64. }
  65. func TestCommandFlagOrder2(t *testing.T) {
  66. var opts = struct {
  67. Value bool `short:"v"`
  68. Command struct {
  69. G bool `short:"g"`
  70. } `command:"cmd"`
  71. }{}
  72. assertParseFail(t, ErrUnknownFlag, "unknown flag `v'", &opts, "cmd", "-v", "-g")
  73. }
  74. func TestCommandEstimate(t *testing.T) {
  75. var opts = struct {
  76. Value bool `short:"v"`
  77. Cmd1 struct {
  78. } `command:"remove"`
  79. Cmd2 struct {
  80. } `command:"add"`
  81. }{}
  82. p := NewParser(&opts, None)
  83. _, err := p.ParseArgs([]string{})
  84. assertError(t, err, ErrRequired, "Please specify one command of: add or remove")
  85. }
  86. type testCommand struct {
  87. G bool `short:"g"`
  88. Executed bool
  89. EArgs []string
  90. }
  91. func (c *testCommand) Execute(args []string) error {
  92. c.Executed = true
  93. c.EArgs = args
  94. return nil
  95. }
  96. func TestCommandExecute(t *testing.T) {
  97. var opts = struct {
  98. Value bool `short:"v"`
  99. Command testCommand `command:"cmd"`
  100. }{}
  101. assertParseSuccess(t, &opts, "-v", "cmd", "-g", "a", "b")
  102. if !opts.Value {
  103. t.Errorf("Expected Value to be true")
  104. }
  105. if !opts.Command.Executed {
  106. t.Errorf("Did not execute command")
  107. }
  108. if !opts.Command.G {
  109. t.Errorf("Expected Command.C to be true")
  110. }
  111. assertStringArray(t, opts.Command.EArgs, []string{"a", "b"})
  112. }
  113. func TestCommandClosest(t *testing.T) {
  114. var opts = struct {
  115. Value bool `short:"v"`
  116. Cmd1 struct {
  117. } `command:"remove"`
  118. Cmd2 struct {
  119. } `command:"add"`
  120. }{}
  121. assertParseFail(t, ErrRequired, "Unknown command `addd', did you mean `add'?", &opts, "-v", "addd")
  122. }
  123. func TestCommandAdd(t *testing.T) {
  124. var opts = struct {
  125. Value bool `short:"v"`
  126. }{}
  127. var cmd = struct {
  128. G bool `short:"g"`
  129. }{}
  130. p := NewParser(&opts, Default)
  131. c, err := p.AddCommand("cmd", "", "", &cmd)
  132. if err != nil {
  133. t.Fatalf("Unexpected error: %v", err)
  134. return
  135. }
  136. ret, err := p.ParseArgs([]string{"-v", "cmd", "-g", "rest"})
  137. if err != nil {
  138. t.Fatalf("Unexpected error: %v", err)
  139. return
  140. }
  141. assertStringArray(t, ret, []string{"rest"})
  142. if !opts.Value {
  143. t.Errorf("Expected Value to be true")
  144. }
  145. if !cmd.G {
  146. t.Errorf("Expected Command.G to be true")
  147. }
  148. if p.Command.Find("cmd") != c {
  149. t.Errorf("Expected to find command `cmd'")
  150. }
  151. if p.Commands()[0] != c {
  152. t.Errorf("Espected command #v, but got #v", c, p.Commands()[0])
  153. }
  154. if c.Options()[0].ShortName != 'g' {
  155. t.Errorf("Expected short name `g' but got %v", c.Options()[0].ShortName)
  156. }
  157. }
  158. func TestCommandNestedInline(t *testing.T) {
  159. var opts = struct {
  160. Value bool `short:"v"`
  161. Command struct {
  162. G bool `short:"g"`
  163. Nested struct {
  164. N string `long:"n"`
  165. } `command:"nested"`
  166. } `command:"cmd"`
  167. }{}
  168. p, ret := assertParserSuccess(t, &opts, "-v", "cmd", "-g", "nested", "--n", "n", "rest")
  169. assertStringArray(t, ret, []string{"rest"})
  170. if !opts.Value {
  171. t.Errorf("Expected Value to be true")
  172. }
  173. if !opts.Command.G {
  174. t.Errorf("Expected Command.G to be true")
  175. }
  176. assertString(t, opts.Command.Nested.N, "n")
  177. if c := p.Command.Find("cmd"); c == nil {
  178. t.Errorf("Expected to find command `cmd'")
  179. } else {
  180. if c != p.Active {
  181. t.Errorf("Expected `cmd' to be the active parser command")
  182. }
  183. if nested := c.Find("nested"); nested == nil {
  184. t.Errorf("Expected to find command `nested'")
  185. } else if nested != c.Active {
  186. t.Errorf("Expected to find command `nested' to be the active `cmd' command")
  187. }
  188. }
  189. }