Browse Source

part way through Command validator

master
Loki Verloren 7 months ago
parent
commit
1e9e3e31c2
4 changed files with 60 additions and 8 deletions
  1. 7
    3
      checklist.md
  2. 0
    3
      types.go
  3. 26
    2
      validators.go
  4. 27
    0
      validators_test.go

+ 7
- 3
checklist.md View File

@@ -42,9 +42,13 @@ Each validator has several error conditions, so each of them are elaborated unde
42 42
 
43 43
       - [x] string in index 0
44 44
       - [x] string is valid name (letters only)
45
-      - [ ] more than one brief not allowed
46
-      - [ ] more than one handler not allowed
47
-      - [ ] handler not nil
45
+      - [x] more than one brief not allowed
46
+      - [x] more than one handler not allowed
47
+      - [x] handler not nil
48
+      - [ ] no more than one Usage
49
+      - [ ] no more than one Short
50
+      - [ ] no more than one Help
51
+      - [ ] no more than one Examples
48 52
       - [ ] Brief field present
49 53
       - [ ] Handler present
50 54
 

+ 0
- 3
types.go View File

@@ -81,9 +81,6 @@ type Examples Tri
81 81
 // Group is a single string tag with the same format as name fields that functions as a tag to gather related items in the help output.
82 82
 type Group Tri
83 83
 
84
-// Handler is the function signature of a subcommand, and is modeled after the parameterisation of a CLI command. The return value is passed through by the real main function of che handler back to the commandline as a return value. Zero indicates success, nonzero is error, with the possibility of arbitrary attribution of meaning to the number, and is returned to the shell application that launches the command.
85
-type Handler func(Tri) int
86
-
87 84
 // Help is a free-form text that is interpreted as markdown syntax and may optionally be formatted using ANSI codes by a preprocessor to represent the structured text that a markdown parser will produce, by default all markdown annotations will be removed.
88 85
 type Help Tri
89 86
 

+ 26
- 2
validators.go View File

@@ -45,9 +45,16 @@ func (r *Command) Validate() error {
45 45
 	// validSet is an array of 4 elements that represent the presence of the 4 mandatory parts.
46 46
 	var validSet [2]bool
47 47
 	brief, handler := 0, 1
48
+	var singleSet [3]bool
49
+	usage, short, help, examples := 0, 1, 2, 3
48 50
 	for i, x := range R[1:] {
49 51
 		switch c := x.(type) {
50 52
 		case Short:
53
+			if singleSet[short] {
54
+				return fmt.Errorf("only one Short field allowed in Command")
55
+			}
56
+			singleSet[short] = true
57
+
51 58
 			e := c.Validate()
52 59
 			if e != nil {
53 60
 				return fmt.Errorf("error in Command at index %d: %v", i, e)
@@ -56,21 +63,36 @@ func (r *Command) Validate() error {
56 63
 			if validSet[brief] {
57 64
 				return fmt.Errorf("only one Brief permitted in a Command, second found at index %d", i)
58 65
 			}
66
+			validSet[brief] = true
59 67
 			e := c.Validate()
60 68
 			if e != nil {
61 69
 				return fmt.Errorf("error in Command at index %d: %v", i, e)
62 70
 			}
63 71
 		case Usage:
72
+			if singleSet[usage] {
73
+				return fmt.Errorf("only one Usage field allowed in Command")
74
+			}
75
+			singleSet[usage] = true
64 76
 			e := c.Validate()
65 77
 			if e != nil {
66 78
 				return fmt.Errorf("error in Command at index %d: %v", i, e)
67 79
 			}
68 80
 		case Help:
81
+			if singleSet[help] {
82
+				return fmt.Errorf("only one Help field allowed in Command")
83
+			}
84
+			singleSet[help] = true
85
+
69 86
 			e := c.Validate()
70 87
 			if e != nil {
71 88
 				return fmt.Errorf("error in Command at index %d: %v", i, e)
72 89
 			}
73 90
 		case Examples:
91
+			if singleSet[examples] {
92
+				return fmt.Errorf("only one Examples field allowed in Command")
93
+			}
94
+			singleSet[examples] = true
95
+
74 96
 			e := c.Validate()
75 97
 			if e != nil {
76 98
 				return fmt.Errorf("error in Command at index %d: %v", i, e)
@@ -85,14 +107,16 @@ func (r *Command) Validate() error {
85 107
 			if e != nil {
86 108
 				return fmt.Errorf("error in Command at index %d: %v", i, e)
87 109
 			}
88
-		case Handler:
110
+		case func(Tri) int:
89 111
 			if validSet[handler] {
90 112
 				return fmt.Errorf("only one Handler permitted in a Command, second found at index %d", i)
91 113
 			}
114
+			validSet[handler] = true
92 115
 			if c == nil {
93 116
 				return fmt.Errorf("nil handler in Command found at index %d", i)
94 117
 			}
95 118
 		default:
119
+			return fmt.Errorf("invalid type present in Command: %v", reflect.TypeOf(c))
96 120
 		}
97 121
 	}
98 122
 	if !validSet[brief] {
@@ -433,7 +457,7 @@ func (r *Trigger) Validate() error {
433 457
 				validSet[brief] = true
434 458
 			}
435 459
 
436
-		case Handler:
460
+		case func(Tri) int:
437 461
 			if y == nil {
438 462
 				return fmt.Errorf("Handler at index %d may not be nil", i)
439 463
 			}

+ 27
- 0
validators_test.go View File

@@ -77,10 +77,37 @@ func TestCommand(t *testing.T) {
77 77
 	}
78 78
 
79 79
 	//more than one brief not allowed
80
+	tc3 := Command{"name", Brief{""}, Brief{""}}
81
+	e = tc3.Validate()
82
+	if e == nil {
83
+		t.Error("validator accepted more than one Brief")
84
+	}
80 85
 
81 86
 	//more than one handler not allowed
87
+	tc4 := Command{"name", Brief{""},
88
+		func(Tri) int { return 0 }, func(Tri) int { return 0 }}
89
+	e = tc4.Validate()
90
+	if e == nil {
91
+		t.Error("validator accepted more than one handler")
92
+	}
82 93
 
83 94
 	//handler not nil
95
+	isnil := func(Tri) int { return 1 }
96
+	_ = isnil
97
+	isnil = nil
98
+	tc5 := Command{"name", isnil}
99
+	e = tc5.Validate()
100
+	if e == nil {
101
+		t.Error("validator accepted nil handler")
102
+	}
103
+
104
+	// no more than one Usage
105
+
106
+	// no more than one Short
107
+
108
+	// no more than one Help
109
+
110
+	// no more than one Examples
84 111
 
85 112
 	//Brief field present
86 113
 

Loading…
Cancel
Save