command_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. package flags
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. func TestCommandInline(t *testing.T) {
  7. var opts = struct {
  8. Value bool `short:"v"`
  9. Command struct {
  10. G bool `short:"g"`
  11. } `command:"cmd"`
  12. }{}
  13. p, ret := assertParserSuccess(t, &opts, "-v", "cmd", "-g")
  14. assertStringArray(t, ret, []string{})
  15. if p.Active == nil {
  16. t.Errorf("Expected active command")
  17. }
  18. if !opts.Value {
  19. t.Errorf("Expected Value to be true")
  20. }
  21. if !opts.Command.G {
  22. t.Errorf("Expected Command.G to be true")
  23. }
  24. if p.Command.Find("cmd") != p.Active {
  25. t.Errorf("Expected to find command `cmd' to be active")
  26. }
  27. }
  28. func TestCommandInlineMulti(t *testing.T) {
  29. var opts = struct {
  30. Value bool `short:"v"`
  31. C1 struct {
  32. } `command:"c1"`
  33. C2 struct {
  34. G bool `short:"g"`
  35. } `command:"c2"`
  36. }{}
  37. p, ret := assertParserSuccess(t, &opts, "-v", "c2", "-g")
  38. assertStringArray(t, ret, []string{})
  39. if p.Active == nil {
  40. t.Errorf("Expected active command")
  41. }
  42. if !opts.Value {
  43. t.Errorf("Expected Value to be true")
  44. }
  45. if !opts.C2.G {
  46. t.Errorf("Expected C2.G to be true")
  47. }
  48. if p.Command.Find("c1") == nil {
  49. t.Errorf("Expected to find command `c1'")
  50. }
  51. if c2 := p.Command.Find("c2"); c2 == nil {
  52. t.Errorf("Expected to find command `c2'")
  53. } else if c2 != p.Active {
  54. t.Errorf("Expected to find command `c2' to be active")
  55. }
  56. }
  57. func TestCommandFlagOrder1(t *testing.T) {
  58. var opts = struct {
  59. Value bool `short:"v"`
  60. Command struct {
  61. G bool `short:"g"`
  62. } `command:"cmd"`
  63. }{}
  64. assertParseFail(t, ErrUnknownFlag, "unknown flag `g'", &opts, "-v", "-g", "cmd")
  65. }
  66. func TestCommandFlagOrder2(t *testing.T) {
  67. var opts = struct {
  68. Value bool `short:"v"`
  69. Command struct {
  70. G bool `short:"g"`
  71. } `command:"cmd"`
  72. }{}
  73. assertParseSuccess(t, &opts, "cmd", "-v", "-g")
  74. if !opts.Value {
  75. t.Errorf("Expected Value to be true")
  76. }
  77. if !opts.Command.G {
  78. t.Errorf("Expected Command.G to be true")
  79. }
  80. }
  81. func TestCommandFlagOrderSub(t *testing.T) {
  82. var opts = struct {
  83. Value bool `short:"v"`
  84. Command struct {
  85. G bool `short:"g"`
  86. SubCommand struct {
  87. B bool `short:"b"`
  88. } `command:"sub"`
  89. } `command:"cmd"`
  90. }{}
  91. assertParseSuccess(t, &opts, "cmd", "sub", "-v", "-g", "-b")
  92. if !opts.Value {
  93. t.Errorf("Expected Value to be true")
  94. }
  95. if !opts.Command.G {
  96. t.Errorf("Expected Command.G to be true")
  97. }
  98. if !opts.Command.SubCommand.B {
  99. t.Errorf("Expected Command.SubCommand.B to be true")
  100. }
  101. }
  102. func TestCommandFlagOverride1(t *testing.T) {
  103. var opts = struct {
  104. Value bool `short:"v"`
  105. Command struct {
  106. Value bool `short:"v"`
  107. } `command:"cmd"`
  108. }{}
  109. assertParseSuccess(t, &opts, "-v", "cmd")
  110. if !opts.Value {
  111. t.Errorf("Expected Value to be true")
  112. }
  113. if opts.Command.Value {
  114. t.Errorf("Expected Command.Value to be false")
  115. }
  116. }
  117. func TestCommandFlagOverride2(t *testing.T) {
  118. var opts = struct {
  119. Value bool `short:"v"`
  120. Command struct {
  121. Value bool `short:"v"`
  122. } `command:"cmd"`
  123. }{}
  124. assertParseSuccess(t, &opts, "cmd", "-v")
  125. if opts.Value {
  126. t.Errorf("Expected Value to be false")
  127. }
  128. if !opts.Command.Value {
  129. t.Errorf("Expected Command.Value to be true")
  130. }
  131. }
  132. func TestCommandFlagOverrideSub(t *testing.T) {
  133. var opts = struct {
  134. Value bool `short:"v"`
  135. Command struct {
  136. Value bool `short:"v"`
  137. SubCommand struct {
  138. Value bool `short:"v"`
  139. } `command:"sub"`
  140. } `command:"cmd"`
  141. }{}
  142. assertParseSuccess(t, &opts, "cmd", "sub", "-v")
  143. if opts.Value {
  144. t.Errorf("Expected Value to be false")
  145. }
  146. if opts.Command.Value {
  147. t.Errorf("Expected Command.Value to be false")
  148. }
  149. if !opts.Command.SubCommand.Value {
  150. t.Errorf("Expected Command.Value to be true")
  151. }
  152. }
  153. func TestCommandFlagOverrideSub2(t *testing.T) {
  154. var opts = struct {
  155. Value bool `short:"v"`
  156. Command struct {
  157. Value bool `short:"v"`
  158. SubCommand struct {
  159. G bool `short:"g"`
  160. } `command:"sub"`
  161. } `command:"cmd"`
  162. }{}
  163. assertParseSuccess(t, &opts, "cmd", "sub", "-v")
  164. if opts.Value {
  165. t.Errorf("Expected Value to be false")
  166. }
  167. if !opts.Command.Value {
  168. t.Errorf("Expected Command.Value to be true")
  169. }
  170. }
  171. func TestCommandEstimate(t *testing.T) {
  172. var opts = struct {
  173. Value bool `short:"v"`
  174. Cmd1 struct {
  175. } `command:"remove"`
  176. Cmd2 struct {
  177. } `command:"add"`
  178. }{}
  179. p := NewParser(&opts, None)
  180. _, err := p.ParseArgs([]string{})
  181. assertError(t, err, ErrCommandRequired, "Please specify one command of: add or remove")
  182. }
  183. func TestCommandEstimate2(t *testing.T) {
  184. var opts = struct {
  185. Value bool `short:"v"`
  186. Cmd1 struct {
  187. } `command:"remove"`
  188. Cmd2 struct {
  189. } `command:"add"`
  190. }{}
  191. p := NewParser(&opts, None)
  192. _, err := p.ParseArgs([]string{"rmive"})
  193. assertError(t, err, ErrUnknownCommand, "Unknown command `rmive', did you mean `remove'?")
  194. }
  195. type testCommand struct {
  196. G bool `short:"g"`
  197. Executed bool
  198. EArgs []string
  199. }
  200. func (c *testCommand) Execute(args []string) error {
  201. c.Executed = true
  202. c.EArgs = args
  203. return nil
  204. }
  205. func TestCommandExecute(t *testing.T) {
  206. var opts = struct {
  207. Value bool `short:"v"`
  208. Command testCommand `command:"cmd"`
  209. }{}
  210. assertParseSuccess(t, &opts, "-v", "cmd", "-g", "a", "b")
  211. if !opts.Value {
  212. t.Errorf("Expected Value to be true")
  213. }
  214. if !opts.Command.Executed {
  215. t.Errorf("Did not execute command")
  216. }
  217. if !opts.Command.G {
  218. t.Errorf("Expected Command.C to be true")
  219. }
  220. assertStringArray(t, opts.Command.EArgs, []string{"a", "b"})
  221. }
  222. func TestCommandClosest(t *testing.T) {
  223. var opts = struct {
  224. Value bool `short:"v"`
  225. Cmd1 struct {
  226. } `command:"remove"`
  227. Cmd2 struct {
  228. } `command:"add"`
  229. }{}
  230. args := assertParseFail(t, ErrUnknownCommand, "Unknown command `addd', did you mean `add'?", &opts, "-v", "addd")
  231. assertStringArray(t, args, []string{"addd"})
  232. }
  233. func TestCommandAdd(t *testing.T) {
  234. var opts = struct {
  235. Value bool `short:"v"`
  236. }{}
  237. var cmd = struct {
  238. G bool `short:"g"`
  239. }{}
  240. p := NewParser(&opts, Default)
  241. c, err := p.AddCommand("cmd", "", "", &cmd)
  242. if err != nil {
  243. t.Fatalf("Unexpected error: %v", err)
  244. return
  245. }
  246. ret, err := p.ParseArgs([]string{"-v", "cmd", "-g", "rest"})
  247. if err != nil {
  248. t.Fatalf("Unexpected error: %v", err)
  249. return
  250. }
  251. assertStringArray(t, ret, []string{"rest"})
  252. if !opts.Value {
  253. t.Errorf("Expected Value to be true")
  254. }
  255. if !cmd.G {
  256. t.Errorf("Expected Command.G to be true")
  257. }
  258. if p.Command.Find("cmd") != c {
  259. t.Errorf("Expected to find command `cmd'")
  260. }
  261. if p.Commands()[0] != c {
  262. t.Errorf("Expected command %#v, but got %#v", c, p.Commands()[0])
  263. }
  264. if c.Options()[0].ShortName != 'g' {
  265. t.Errorf("Expected short name `g' but got %v", c.Options()[0].ShortName)
  266. }
  267. }
  268. func TestCommandNestedInline(t *testing.T) {
  269. var opts = struct {
  270. Value bool `short:"v"`
  271. Command struct {
  272. G bool `short:"g"`
  273. Nested struct {
  274. N string `long:"n"`
  275. } `command:"nested"`
  276. } `command:"cmd"`
  277. }{}
  278. p, ret := assertParserSuccess(t, &opts, "-v", "cmd", "-g", "nested", "--n", "n", "rest")
  279. assertStringArray(t, ret, []string{"rest"})
  280. if !opts.Value {
  281. t.Errorf("Expected Value to be true")
  282. }
  283. if !opts.Command.G {
  284. t.Errorf("Expected Command.G to be true")
  285. }
  286. assertString(t, opts.Command.Nested.N, "n")
  287. if c := p.Command.Find("cmd"); c == nil {
  288. t.Errorf("Expected to find command `cmd'")
  289. } else {
  290. if c != p.Active {
  291. t.Errorf("Expected `cmd' to be the active parser command")
  292. }
  293. if nested := c.Find("nested"); nested == nil {
  294. t.Errorf("Expected to find command `nested'")
  295. } else if nested != c.Active {
  296. t.Errorf("Expected to find command `nested' to be the active `cmd' command")
  297. }
  298. }
  299. }
  300. func TestRequiredOnCommand(t *testing.T) {
  301. var opts = struct {
  302. Value bool `short:"v" required:"true"`
  303. Command struct {
  304. G bool `short:"g"`
  305. } `command:"cmd"`
  306. }{}
  307. assertParseFail(t, ErrRequired, fmt.Sprintf("the required flag `%cv' was not specified", defaultShortOptDelimiter), &opts, "cmd")
  308. }
  309. func TestRequiredAllOnCommand(t *testing.T) {
  310. var opts = struct {
  311. Value bool `short:"v" required:"true"`
  312. Missing bool `long:"missing" required:"true"`
  313. Command struct {
  314. G bool `short:"g"`
  315. } `command:"cmd"`
  316. }{}
  317. assertParseFail(t, ErrRequired, fmt.Sprintf("the required flags `%smissing' and `%cv' were not specified", defaultLongOptDelimiter, defaultShortOptDelimiter), &opts, "cmd")
  318. }
  319. func TestDefaultOnCommand(t *testing.T) {
  320. var opts = struct {
  321. Command struct {
  322. G string `short:"g" default:"value"`
  323. } `command:"cmd"`
  324. }{}
  325. assertParseSuccess(t, &opts, "cmd")
  326. if opts.Command.G != "value" {
  327. t.Errorf("Expected G to be \"value\"")
  328. }
  329. }
  330. func TestAfterNonCommand(t *testing.T) {
  331. var opts = struct {
  332. Value bool `short:"v"`
  333. Cmd1 struct {
  334. } `command:"remove"`
  335. Cmd2 struct {
  336. } `command:"add"`
  337. }{}
  338. assertParseFail(t, ErrUnknownCommand, "Unknown command `nocmd'. Please specify one command of: add or remove", &opts, "nocmd", "remove")
  339. }
  340. func TestSubcommandsOptional(t *testing.T) {
  341. var opts = struct {
  342. Value bool `short:"v"`
  343. Cmd1 struct {
  344. } `command:"remove"`
  345. Cmd2 struct {
  346. } `command:"add"`
  347. }{}
  348. p := NewParser(&opts, None)
  349. p.SubcommandsOptional = true
  350. _, err := p.ParseArgs([]string{"-v"})
  351. if err != nil {
  352. t.Fatalf("Unexpected error: %v", err)
  353. return
  354. }
  355. if !opts.Value {
  356. t.Errorf("Expected Value to be true")
  357. }
  358. }
  359. func TestSubcommandsOptionalAfterNonCommand(t *testing.T) {
  360. var opts = struct {
  361. Value bool `short:"v"`
  362. Cmd1 struct {
  363. } `command:"remove"`
  364. Cmd2 struct {
  365. } `command:"add"`
  366. }{}
  367. p := NewParser(&opts, None)
  368. p.SubcommandsOptional = true
  369. retargs, err := p.ParseArgs([]string{"nocmd", "remove"})
  370. if err != nil {
  371. t.Fatalf("Unexpected error: %v", err)
  372. return
  373. }
  374. assertStringArray(t, retargs, []string{"nocmd", "remove"})
  375. }
  376. func TestCommandAlias(t *testing.T) {
  377. var opts = struct {
  378. Command struct {
  379. G string `short:"g" default:"value"`
  380. } `command:"cmd" alias:"cm"`
  381. }{}
  382. assertParseSuccess(t, &opts, "cm")
  383. if opts.Command.G != "value" {
  384. t.Errorf("Expected G to be \"value\"")
  385. }
  386. }
  387. func TestSubCommandFindOptionByLongFlag(t *testing.T) {
  388. var opts struct {
  389. Testing bool `long:"testing" description:"Testing"`
  390. }
  391. var cmd struct {
  392. Other bool `long:"other" description:"Other"`
  393. }
  394. p := NewParser(&opts, Default)
  395. c, _ := p.AddCommand("command", "Short", "Long", &cmd)
  396. opt := c.FindOptionByLongName("other")
  397. if opt == nil {
  398. t.Errorf("Expected option, but found none")
  399. }
  400. assertString(t, opt.LongName, "other")
  401. opt = c.FindOptionByLongName("testing")
  402. if opt == nil {
  403. t.Errorf("Expected option, but found none")
  404. }
  405. assertString(t, opt.LongName, "testing")
  406. }
  407. func TestSubCommandFindOptionByShortFlag(t *testing.T) {
  408. var opts struct {
  409. Testing bool `short:"t" description:"Testing"`
  410. }
  411. var cmd struct {
  412. Other bool `short:"o" description:"Other"`
  413. }
  414. p := NewParser(&opts, Default)
  415. c, _ := p.AddCommand("command", "Short", "Long", &cmd)
  416. opt := c.FindOptionByShortName('o')
  417. if opt == nil {
  418. t.Errorf("Expected option, but found none")
  419. }
  420. if opt.ShortName != 'o' {
  421. t.Errorf("Expected 'o', but got %v", opt.ShortName)
  422. }
  423. opt = c.FindOptionByShortName('t')
  424. if opt == nil {
  425. t.Errorf("Expected option, but found none")
  426. }
  427. if opt.ShortName != 't' {
  428. t.Errorf("Expected 'o', but got %v", opt.ShortName)
  429. }
  430. }