Fix up commandline interface for repeat commands to go after the command
This commit is contained in:
parent
4e4af1a1be
commit
804f82dd20
2 changed files with 28 additions and 32 deletions
|
@ -35,7 +35,7 @@ func printUsage() {
|
|||
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 [REPEAT] CMD [VAL...] queues commands, accepts multiple in sequence for command values see below")
|
||||
fmt.Fprintln(os.Stderr, "\tcommand CMD [VAL...] [REPEAT] sets the command queue, accepts multiple in sequence")
|
||||
fmt.Fprintf(os.Stderr, "\n")
|
||||
fmt.Fprintln(os.Stderr, "Rover commands:")
|
||||
fmt.Fprintln(os.Stderr, "\ttoggle toggles the current sail mode")
|
||||
|
@ -247,16 +247,8 @@ 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 repeat number")
|
||||
}
|
||||
}
|
||||
|
||||
var cmd *roveapi.Command
|
||||
switch args[i] {
|
||||
case "turn":
|
||||
i++
|
||||
|
@ -267,13 +259,10 @@ func InnerMain(command string, args ...string) error {
|
|||
if b == roveapi.Bearing_BearingUnknown {
|
||||
return fmt.Errorf("turn command must be given a valid bearing %s", args[i])
|
||||
}
|
||||
commands = append(commands,
|
||||
&roveapi.Command{
|
||||
cmd = &roveapi.Command{
|
||||
Command: roveapi.CommandType_turn,
|
||||
Bearing: b,
|
||||
Repeat: int32(number),
|
||||
},
|
||||
)
|
||||
}
|
||||
case "broadcast":
|
||||
i++
|
||||
if len(args) == i {
|
||||
|
@ -281,24 +270,31 @@ func InnerMain(command string, args ...string) error {
|
|||
} else if len(args[i]) > 3 {
|
||||
return fmt.Errorf("broadcast command must be given ASCII triplet of 3 or less: %s", args[i])
|
||||
}
|
||||
commands = append(commands,
|
||||
&roveapi.Command{
|
||||
cmd = &roveapi.Command{
|
||||
Command: roveapi.CommandType_broadcast,
|
||||
Data: []byte(args[i]),
|
||||
Repeat: int32(number),
|
||||
},
|
||||
)
|
||||
}
|
||||
default:
|
||||
// By default just use the command literally
|
||||
commands = append(commands,
|
||||
&roveapi.Command{
|
||||
cmd = &roveapi.Command{
|
||||
Command: roveapi.CommandType(roveapi.CommandType_value[args[i]]),
|
||||
Repeat: int32(number),
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Try and convert the next command to a number
|
||||
number := 0
|
||||
if len(args) > i+1 {
|
||||
num, err := strconv.Atoi(args[i+1])
|
||||
if err == nil {
|
||||
number = num
|
||||
i++
|
||||
}
|
||||
}
|
||||
cmd.Repeat = int32(number)
|
||||
|
||||
commands = append(commands, cmd)
|
||||
}
|
||||
|
||||
_, err := client.Command(ctx, &roveapi.CommandRequest{
|
||||
Account: &roveapi.Account{
|
||||
Name: config.Account.Name,
|
||||
|
|
|
@ -55,8 +55,8 @@ 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"))
|
||||
assert.NoError(t, InnerMain("command", "wait", "10"))
|
||||
assert.NoError(t, InnerMain("command", "wait", "1", "turn", "NW", "toggle", "broadcast", "zyx"))
|
||||
|
||||
// Give it malformed commands
|
||||
assert.Error(t, InnerMain("command", "unknown"))
|
||||
|
|
Loading…
Add table
Reference in a new issue