| 1 | package opt_test |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | |
| 7 | "git.sr.ht/~rjarry/go-opt/v2" |
| 8 | "github.com/stretchr/testify/assert" |
| 9 | ) |
| 10 | |
| 11 | type Cmd interface { |
| 12 | Name() string |
| 13 | Desc() string |
| 14 | } |
| 15 | |
| 16 | type TopLevelStruct struct { |
| 17 | Cmd Cmd `opt:":cmd:" action:"ParseCmd" complete:"CompleteCmd"` |
| 18 | } |
| 19 | |
| 20 | type FooCmd struct { |
| 21 | Fast bool `opt:"-f,--fast" desc:"Foo faster."` |
| 22 | } |
| 23 | |
| 24 | type BarCmd struct { |
| 25 | Location string `opt:"name" desc:"The location of this bar."` |
| 26 | } |
| 27 | |
| 28 | type BazCmd struct { |
| 29 | Debug uint `opt:"-d,--debug" desc:"Baz louder."` |
| 30 | } |
| 31 | |
| 32 | func (f *FooCmd) Name() string { return "foo" } |
| 33 | func (f *BarCmd) Name() string { return "bar" } |
| 34 | func (f *BazCmd) Name() string { return "baz" } |
| 35 | func (f *FooCmd) Desc() string { return "Foo foo foo foo foo." } |
| 36 | func (f *BarCmd) Desc() string { return "Bar bar bar bar bar." } |
| 37 | func (f *BazCmd) Desc() string { return "Baz baz baz baz baz." } |
| 38 | |
| 39 | var subcmds []Cmd = []Cmd{new(FooCmd), new(BarCmd), new(BazCmd)} |
| 40 | |
| 41 | func (t *TopLevelStruct) ParseCmd(arg string) error { |
| 42 | for _, cmd := range subcmds { |
| 43 | if cmd.Name() == arg { |
| 44 | t.Cmd = cmd |
| 45 | return nil |
| 46 | } |
| 47 | } |
| 48 | return fmt.Errorf("invalid subcommand %q", arg) |
| 49 | } |
| 50 | |
| 51 | func (t *TopLevelStruct) CompleteCmd(arg string) []string { |
| 52 | var comps []string |
| 53 | for _, cmd := range subcmds { |
| 54 | comps = append(comps, cmd.Name()+"\n"+cmd.Desc()) |
| 55 | } |
| 56 | return comps |
| 57 | } |
| 58 | |
| 59 | func TestParseSubCommand(t *testing.T) { |
| 60 | var s TopLevelStruct |
| 61 | |
| 62 | spec := opt.NewCmdSpec("pouet", &s) |
| 63 | assert.Equal(t, "pouet <cmd> ...", spec.Usage()) |
| 64 | |
| 65 | err := opt.CmdlineToStruct("pouet ok prout", &s) |
| 66 | assert.ErrorContains(t, err, `<cmd>: invalid subcommand "ok". Usage: pouet <cmd> ...`) |
| 67 | |
| 68 | err = opt.CmdlineToStruct("pouet foo -f", &s) |
| 69 | assert.Nil(t, err) |
| 70 | assert.NotNil(t, s.Cmd) |
| 71 | assert.Equal(t, s.Cmd.Desc(), "Foo foo foo foo foo.") |
| 72 | foo, ok := s.Cmd.(*FooCmd) |
| 73 | assert.True(t, ok) |
| 74 | assert.True(t, foo.Fast) |
| 75 | |
| 76 | s.Cmd = nil |
| 77 | err = opt.CmdlineToStruct("pouet bar --ok", &s) |
| 78 | assert.ErrorContains(t, err, `--ok unknown flag. Usage: pouet bar <name>`) |
| 79 | |
| 80 | err = opt.CmdlineToStruct("pouet bar houilles", &s) |
| 81 | assert.Nil(t, err) |
| 82 | assert.NotNil(t, s.Cmd) |
| 83 | assert.Equal(t, s.Cmd.Desc(), "Bar bar bar bar bar.") |
| 84 | bar, ok := s.Cmd.(*BarCmd) |
| 85 | assert.True(t, ok) |
| 86 | assert.Equal(t, "houilles", bar.Location) |
| 87 | |
| 88 | err = opt.CmdlineToStruct("pouet baz -d 9000", &s) |
| 89 | assert.Nil(t, err) |
| 90 | assert.NotNil(t, s.Cmd) |
| 91 | assert.Equal(t, s.Cmd.Desc(), "Baz baz baz baz baz.") |
| 92 | baz, ok := s.Cmd.(*BazCmd) |
| 93 | assert.True(t, ok) |
| 94 | assert.Equal(t, uint(9000), baz.Debug) |
| 95 | } |
| 96 | |
| 97 | func TestCompleteSubCommand(t *testing.T) { |
| 98 | var s TopLevelStruct |
| 99 | spec := opt.NewCmdSpec("pouet", &s) |
| 100 | comps, _ := spec.GetCompletions(opt.LexArgs("pouet ")) |
| 101 | assert.Equal(t, 3, len(comps)) |
| 102 | assert.Equal(t, "foo", comps[0].Value) |
| 103 | assert.Equal(t, "Foo foo foo foo foo.", comps[0].Description) |
| 104 | assert.Equal(t, "bar", comps[1].Value) |
| 105 | assert.Equal(t, "Bar bar bar bar bar.", comps[1].Description) |
| 106 | assert.Equal(t, "baz", comps[2].Value) |
| 107 | assert.Equal(t, "Baz baz baz baz baz.", comps[2].Description) |
| 108 | |
| 109 | comps, _ = spec.GetCompletions(opt.LexArgs("pouet foo ")) |
| 110 | assert.Equal(t, 2, len(comps)) |
| 111 | assert.Equal(t, "-f ", comps[0].Value) |
| 112 | assert.Equal(t, "Foo faster.", comps[0].Description) |
| 113 | assert.Equal(t, "--fast ", comps[1].Value) |
| 114 | assert.Equal(t, "Foo faster.", comps[1].Description) |
| 115 | } |