diff --git a/peripheral/devices/cdash_display.go b/peripheral/devices/cdash_display.go index c6f82dd..d2c1cae 100644 --- a/peripheral/devices/cdash_display.go +++ b/peripheral/devices/cdash_display.go @@ -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) diff --git a/peripheral/devices/devices.go b/peripheral/devices/devices.go index c95ba11..e32cbf7 100644 --- a/peripheral/devices/devices.go +++ b/peripheral/devices/devices.go @@ -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 }