options: Fix sorting (and test)

This commit is contained in:
Alexander Neumann 2017-04-14 00:45:54 +02:00
parent 55bdc1fa16
commit be06983c80
2 changed files with 7 additions and 2 deletions

View file

@ -34,6 +34,8 @@ func appendAllOptions(opts []Help, ns string, cfg interface{}) []Help {
opt.Namespace = ns opt.Namespace = ns
opts = append(opts, opt) opts = append(opts, opt)
} }
sort.Sort(helpList(opts))
return opts return opts
} }
@ -57,7 +59,6 @@ func listOptions(cfg interface{}) (opts []Help) {
opts = append(opts, h) opts = append(opts, h)
} }
sort.Sort(helpList(opts))
return opts return opts
} }
@ -78,6 +79,10 @@ func (h helpList) Len() int {
// Less reports whether the element with // Less reports whether the element with
// index i should sort before the element with index j. // index i should sort before the element with index j.
func (h helpList) Less(i, j int) bool { func (h helpList) Less(i, j int) bool {
if h[i].Namespace == h[j].Namespace {
return h[i].Name < h[j].Name
}
return h[i].Namespace < h[j].Namespace return h[i].Namespace < h[j].Namespace
} }

View file

@ -291,8 +291,8 @@ func TestAppendAllOptions(t *testing.T) {
}, },
[]Help{ []Help{
Help{Namespace: "local", Name: "foo", Text: "bar text help"}, Help{Namespace: "local", Name: "foo", Text: "bar text help"},
Help{Namespace: "sftp", Name: "foo", Text: "bar text help2"},
Help{Namespace: "sftp", Name: "bar", Text: "bar text help"}, Help{Namespace: "sftp", Name: "bar", Text: "bar text help"},
Help{Namespace: "sftp", Name: "foo", Text: "bar text help2"},
}, },
}, },
} }