Commit 93ab003

Richard Luby <richluby@gmail.com>
2016-11-04 07:37:02
commands support flags
commands support providing flags with arguments. commands must parse args from strings into correct type if necessasry
1 parent 3cde72f
command.go
@@ -15,10 +15,19 @@ import (
 
 // Command stores the information for running a command
 type Command struct {
-	Command     string
-	Description string
-	Usage       string
-	Run         func(args []string) error
+	Command,
+	Description,
+	Usage string
+	Flags []Flag
+	Run   func(args []string) error
+}
+
+// Flag servers to store command flags for the program commands
+type Flag struct {
+	Flag,
+	Description,
+	Usage,
+	Value string
 }
 
 // selectCommand takes a string and converts it to the correct command
@@ -48,10 +57,19 @@ var commandArray = []Command{
 			return nil
 		}},
 	Command{Command: "test",
-		Usage: "test [--blueprint] [number]",
+		Usage: "test [flags]",
+		Flags: []Flag{Flag{
+			Flag:        "blueprint",
+			Usage:       "--blueprint",
+			Description: "Allows to define a list of categories to use",
+			Value:       "NONE"},
+			Flag{
+				Flag:        "questions",
+				Usage:       "--questions [n]",
+				Description: "Specify the number of questions to pull. Default is 20.",
+				Value:       ""}},
 		Description: "Execute a test. Given a number, will go through a test with [n] questions." +
-			" If " + COLOR_GREEN + "--blueprint" + COLOR_RESET + " is specified, test will be generated according to the test generation" +
-			" outline specified. Type 'exit' to stop test.",
+			" Type 'exit' to stop test.",
 		Run: executeTest},
 	Command{
 		Command: "user",
@@ -79,11 +97,21 @@ var helpCommand = Command{Command: "help",
 			if len(args) > 0 && args[0] != command.Command { // only print help for desired command
 				continue
 			}
-			if strings.Compare(command.Usage, "") == 0 {
+			if strings.Compare(command.Usage, "") == 0 { // print command help
 				fmt.Printf("> %s\n\t%s\n", COLOR_GREEN+command.Command+COLOR_RESET, command.Description)
 			} else {
 				fmt.Printf("> %s\n\t%s\n", COLOR_GREEN+command.Usage+COLOR_RESET, command.Description)
 			}
+			if len(command.Flags) > 0 { // print flag help
+				fmt.Print(COLOR_BOLD + "\tFLAGS\n" + COLOR_RESET)
+				for _, commandFlag := range command.Flags {
+					if commandFlag.Usage != "" {
+						fmt.Printf("\t%s\n\t\t%s\n", COLOR_BLUE+commandFlag.Usage+COLOR_RESET, commandFlag.Description)
+					} else {
+						fmt.Printf("\t%s\n\t\t%s\n", COLOR_BLUE+commandFlag.Flag+COLOR_RESET, commandFlag.Description)
+					}
+				}
+			}
 		}
 		return nil
 	}}
@@ -177,6 +205,11 @@ func getCategoriesFromServer(args []string) error {
 	return nil
 }
 
+// parseTestCommandFlags parses the flags for the test command
+func parseTestCommandFlags(args []string) {
+
+}
+
 // executeTest runs a user through a test
 // from retrieving the records to returning the answers
 func executeTest(args []string) error {
structures.go
@@ -20,6 +20,7 @@ const (
 	COLOR_GREEN = "\x1b[32m"
 	COLOR_BLUE  = "\x1b[34m"
 	COLOR_RED   = "\x1b[31m"
+	COLOR_BOLD  = "\x1b[1m"
 	COLOR_RESET = "\x1b[0m"
 )