Update command line client to allow specifying command number
This commit is contained in:
parent
1514603517
commit
c0d4a809c9
2 changed files with 33 additions and 14 deletions
|
@ -8,6 +8,7 @@ import (
|
|||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/mdiluz/rove/cmd/rove/internal"
|
||||
|
@ -25,22 +26,23 @@ func printUsage() {
|
|||
fmt.Fprintln(os.Stderr, "Usage: rove ARG [OPT...]")
|
||||
fmt.Fprintf(os.Stderr, "\n")
|
||||
fmt.Fprintln(os.Stderr, "Arguments:")
|
||||
fmt.Fprintln(os.Stderr, "\tversion outputs version")
|
||||
fmt.Fprintln(os.Stderr, "\thelp outputs this usage text")
|
||||
fmt.Fprintln(os.Stderr, "\tconfig [HOST] outputs the local config, optionally sets host")
|
||||
fmt.Fprintln(os.Stderr, "\tserver-status prints the server status")
|
||||
fmt.Fprintln(os.Stderr, "\tregister NAME registers an account and spawns a rover")
|
||||
fmt.Fprintln(os.Stderr, "\tradar prints radar data in ASCII form")
|
||||
fmt.Fprintln(os.Stderr, "\tstatus gets rover status")
|
||||
fmt.Fprintln(os.Stderr, "\tcommand CMD [VAL...] queues commands, accepts multiple, see below")
|
||||
fmt.Fprintln(os.Stderr, "\tversion outputs version")
|
||||
fmt.Fprintln(os.Stderr, "\thelp outputs this usage text")
|
||||
fmt.Fprintln(os.Stderr, "\tconfig [HOST] outputs the local config, optionally sets host")
|
||||
fmt.Fprintln(os.Stderr, "\tserver-status prints the server status")
|
||||
fmt.Fprintln(os.Stderr, "\tregister NAME registers an account and spawns a rover")
|
||||
fmt.Fprintln(os.Stderr, "\tradar prints radar data in ASCII form")
|
||||
fmt.Fprintln(os.Stderr, "\tstatus gets rover status")
|
||||
fmt.Fprintln(os.Stderr, "\tcommand [NUM] CMD [VAL...] queues commands, accepts multiple, see below")
|
||||
fmt.Fprintf(os.Stderr, "\n")
|
||||
fmt.Fprintln(os.Stderr, "Rover commands:")
|
||||
fmt.Fprintln(os.Stderr, "\ttoggle toggles the current sail mode")
|
||||
fmt.Fprintln(os.Stderr, "\tstash stores the object at the rover location in the inventory")
|
||||
fmt.Fprintln(os.Stderr, "\trepair repairs the rover using inventory item")
|
||||
fmt.Fprintln(os.Stderr, "\tbroadcast MSG broadcast a simple ASCII triplet to nearby rovers")
|
||||
fmt.Fprintln(os.Stderr, "\tsalvage salvages a dormant rover for parts")
|
||||
fmt.Fprintln(os.Stderr, "\ttransfer transfer's control into a dormant rover")
|
||||
fmt.Fprintln(os.Stderr, "\ttoggle toggles the current sail mode")
|
||||
fmt.Fprintln(os.Stderr, "\tstash stores the object at the rover location in the inventory")
|
||||
fmt.Fprintln(os.Stderr, "\trepair repairs the rover using inventory item")
|
||||
fmt.Fprintln(os.Stderr, "\tbroadcast MSG broadcast a simple ASCII triplet to nearby rovers")
|
||||
fmt.Fprintln(os.Stderr, "\tsalvage salvages a dormant rover for parts")
|
||||
fmt.Fprintln(os.Stderr, "\ttransfer transfer's control into a dormant rover")
|
||||
fmt.Fprintln(os.Stderr, "\twait waits before performing the next command")
|
||||
fmt.Fprintf(os.Stderr, "\n")
|
||||
fmt.Fprintln(os.Stderr, "Environment")
|
||||
fmt.Fprintln(os.Stderr, "\tROVE_USER_DATA path to user data, defaults to "+defaultDataPath)
|
||||
|
@ -236,6 +238,16 @@ func InnerMain(command string, args ...string) error {
|
|||
// Iterate through each command
|
||||
var commands []*roveapi.Command
|
||||
for i := 0; i < len(args); i++ {
|
||||
number := 0
|
||||
num, err := strconv.Atoi(args[i])
|
||||
if err == nil {
|
||||
number = num
|
||||
i++
|
||||
if i >= len(args) {
|
||||
return fmt.Errorf("must pass command after number")
|
||||
}
|
||||
}
|
||||
|
||||
switch args[i] {
|
||||
case "turn":
|
||||
i++
|
||||
|
@ -250,6 +262,7 @@ func InnerMain(command string, args ...string) error {
|
|||
&roveapi.Command{
|
||||
Command: roveapi.CommandType_turn,
|
||||
Bearing: b,
|
||||
Number: int32(number),
|
||||
},
|
||||
)
|
||||
case "broadcast":
|
||||
|
@ -263,6 +276,7 @@ func InnerMain(command string, args ...string) error {
|
|||
&roveapi.Command{
|
||||
Command: roveapi.CommandType_broadcast,
|
||||
Data: []byte(args[i]),
|
||||
Number: int32(number),
|
||||
},
|
||||
)
|
||||
default:
|
||||
|
@ -270,6 +284,7 @@ func InnerMain(command string, args ...string) error {
|
|||
commands = append(commands,
|
||||
&roveapi.Command{
|
||||
Command: roveapi.CommandType(roveapi.CommandType_value[args[i]]),
|
||||
Number: int32(number),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
|
@ -54,8 +54,12 @@ func Test_InnerMain(t *testing.T) {
|
|||
assert.NoError(t, InnerMain("command", "stash"))
|
||||
assert.NoError(t, InnerMain("command", "repair"))
|
||||
assert.NoError(t, InnerMain("command", "broadcast", "abc"))
|
||||
assert.NoError(t, InnerMain("command", "10", "wait"))
|
||||
assert.NoError(t, InnerMain("command", "1", "wait", "turn", "NW", "toggle", "broadcast", "zyx"))
|
||||
|
||||
// Give it malformed commands
|
||||
assert.Error(t, InnerMain("command", "unknown"))
|
||||
assert.Error(t, InnerMain("command", "broadcast"))
|
||||
assert.Error(t, InnerMain("command", "0", "wait"))
|
||||
assert.Error(t, InnerMain("command", "1"))
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue