Commit 6dc03e9

Richard Luby <richluby@gmail.com>
2016-11-17 10:03:40
commands sorted lexically
commands implement the sort interface to allow sorting
1 parent 031e994
command.go
@@ -8,13 +8,14 @@ import (
 	"log"
 	"net/http"
 	"os"
+	"sort"
 	"strconv"
 	"strings"
 )
 
 // commandArray contains the full list of commands available to the application
 // note that subcommands are not currently supported
-var commandArray = []Command{Command{Command: "exit",
+var commandArray = Commands{Command{Command: "exit",
 	Description: "Exit the self-assessment application and close the connection.",
 	Run:         exitApplication},
 	Command{Command: "score",
@@ -122,6 +123,7 @@ func displayHelp(command Command) error {
 // init initializes the commandArray to provide a single interface
 func init() {
 	commandArray = append(commandArray, helpCommand)
+	sort.Sort(commandArray)
 }
 
 // displayTests displays the previous tests to the user
commandParser.go
@@ -15,6 +15,8 @@ type Command struct {
 	Run                  func(Command) error
 }
 
+type Commands []Command
+
 // Flag servers to store command flags for the program commands
 // if HasArgs is true, then the flag MUST have an argument
 type Flag struct {
@@ -26,6 +28,22 @@ type Flag struct {
 	Args    []string
 }
 
+// Swap swaps i and j
+func (commands Commands) Swap(i, j int) {
+	commands[i], commands[j] = commands[j], commands[i]
+}
+
+// Less returns true if i is less than j
+func (a Commands) Less(i, j int) bool {
+	return strings.Compare(a[i].Command, a[j].Command) < 0
+}
+
+// Len returns the number of commands contained
+// in this struct
+func (a Commands) Len() int {
+	return len(a)
+}
+
 // selectCommand takes a string and converts it to the correct command
 func SelectCommand(str string, commandArray []Command) (Command, error) {
 	for _, value := range commandArray {