Added the argcheck method the the devicecmd struct

This commit is contained in:
2025-12-31 17:02:36 +00:00
parent 58c9412590
commit fe2a83b21e
2 changed files with 28 additions and 3 deletions
+23 -3
View File
@@ -21,17 +21,28 @@ var CDashDisplay = Device{
Identifier: newWindowCMDID,
Name: "new-window",
Desc: "Creates a new window - pass the window name and dimensions",
ArgCheck: createWindowArgCheck,
Fn: createWindow,
},
"destroy-window": {
Identifier: destroyWindowCMDID,
Name: "destroy-window",
Desc: "Destroys a window by its ID",
ArgCheck: destroyWindowArgCheck,
Fn: destroyWindow,
},
},
}
func createWindowArgCheck(args []string) error {
if len(args) != 5 {
return fmt.Errorf("wrong parameters, got %d, want %d. "+
"Command asks for: x0 y0 width height title", len(args), 5)
}
return nil
}
func createWindow(dCMD *DeviceCMD, args []string) (types.Command, []byte, error) {
// Parse the command
// x0, y0, width, height, title # add other decorations later on
@@ -45,9 +56,9 @@ func createWindow(dCMD *DeviceCMD, args []string) (types.Command, []byte, error)
Title [32]byte
}
if len(args) != 5 {
return 0, []byte{}, fmt.Errorf("wrong parameters, got %d, want %d. "+
"Command asks for: x0 y0 width height title", len(args), 5)
err := dCMD.ArgCheck(args)
if err != nil {
return 0, []byte{}, err
}
x0, err := strconv.ParseInt(args[0], 10, 0)
@@ -83,6 +94,15 @@ func createWindow(dCMD *DeviceCMD, args []string) (types.Command, []byte, error)
return dCMD.GetIdentifier(), bytes, nil
}
func destroyWindowArgCheck(args []string) error {
if len(args) != 1 {
return fmt.Errorf("wrong parameters, got %d, want %d. "+
"Command asks for: winID", len(args), 1)
}
return nil
}
func destroyWindow(dCMD *DeviceCMD, args []string) (types.Command, []byte, error) {
// Parse the command
fmt.Printf("%s command called: %+v\n", dCMD.GetName(), args)
+5
View File
@@ -44,12 +44,17 @@ type DeviceCMDPayload struct {
// types in the REPL - or however this will be used
type DeviceCMDFn func(dCMD *DeviceCMD, args []string) (types.Command, []byte, error)
// ArgCheckFn is a function to check the arguments passed. Returns nil or the error
// in the passed arguments
type ArgCheckFn func(args []string) error
type DeviceCMD struct {
Identifier types.Command
Name string
Desc string
Header DeviceCMDHeader
Data DeviceCMDPayload
ArgCheck ArgCheckFn
Fn DeviceCMDFn
}