-
Notifications
You must be signed in to change notification settings - Fork 167
improve commandline processing #1091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,15 +2,16 @@ package cmd | |
|
|
||
| import ( | ||
| "fmt" | ||
| "github.com/df-mc/dragonfly/server/internal/sliceutil" | ||
| "github.com/df-mc/dragonfly/server/world" | ||
| "github.com/go-gl/mathgl/mgl64" | ||
| "math/rand/v2" | ||
| "reflect" | ||
| "slices" | ||
| "sort" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/df-mc/dragonfly/server/internal/sliceutil" | ||
| "github.com/df-mc/dragonfly/server/world" | ||
| "github.com/go-gl/mathgl/mgl64" | ||
| ) | ||
|
|
||
| // Line represents a command line holding command arguments that were passed upon the execution of the | ||
|
|
@@ -46,7 +47,12 @@ func (line *Line) Next() (string, bool) { | |
| if !ok { | ||
| return "", false | ||
| } | ||
| return v[0], true | ||
| val := v[0] | ||
| if val == "" { | ||
| line.RemoveNext() | ||
| return line.Next() | ||
| } | ||
| return val, true | ||
| } | ||
|
|
||
| // NextN reads the next N arguments from the command line and returns them. If there were not enough arguments | ||
|
|
@@ -83,13 +89,20 @@ func (line *Line) Leftover() []string { | |
|
|
||
| // Len returns the leftover length of the arguments in the command line. | ||
| func (line *Line) Len() int { | ||
| return len(line.args) | ||
| count := 0 | ||
| for _, arg := range line.args { | ||
| if arg != "" { | ||
| count++ | ||
| } | ||
| } | ||
| return count | ||
| } | ||
|
|
||
| // parser manages the parsing of a Line, turning the raw arguments into values which are then stored in the | ||
| // struct fields. | ||
| type parser struct { | ||
| currentField string | ||
| fields int | ||
| } | ||
|
|
||
| // parseArgument parses the next argument from the command line passed and sets it to value v passed. If | ||
|
|
@@ -185,6 +198,10 @@ func (p parser) float(line *Line, v reflect.Value) error { | |
|
|
||
| // string ... | ||
| func (p parser) string(line *Line, v reflect.Value) error { | ||
| if p.fields == 1 { | ||
| return p.restAsString(line, v) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this what the Varargs parameter type is for? |
||
| } | ||
|
|
||
| arg, ok := line.Next() | ||
| if !ok { | ||
| return line.UsageError() | ||
|
|
@@ -193,6 +210,18 @@ func (p parser) string(line *Line, v reflect.Value) error { | |
| return nil | ||
| } | ||
|
|
||
| // restAsString ... | ||
| func (p parser) restAsString(line *Line, v reflect.Value) error { | ||
| args := line.Leftover() | ||
| val := strings.Join(args, " ") | ||
| // check if value is empty. | ||
| if strings.TrimSpace(val) == "" { | ||
| return line.UsageError() | ||
| } | ||
| v.SetString(val) | ||
| return nil | ||
| } | ||
|
|
||
| // bool ... | ||
| func (p parser) bool(line *Line, v reflect.Value) error { | ||
| arg, ok := line.Next() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,8 +2,6 @@ package player | |
|
|
||
| import ( | ||
| "fmt" | ||
| "github.com/df-mc/dragonfly/server/player/debug" | ||
| "github.com/df-mc/dragonfly/server/player/hud" | ||
| "math" | ||
| "math/rand/v2" | ||
| "net" | ||
|
|
@@ -12,6 +10,9 @@ import ( | |
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/df-mc/dragonfly/server/player/debug" | ||
| "github.com/df-mc/dragonfly/server/player/hud" | ||
|
|
||
| "github.com/df-mc/dragonfly/server/block" | ||
| "github.com/df-mc/dragonfly/server/block/cube" | ||
| "github.com/df-mc/dragonfly/server/block/model" | ||
|
|
@@ -332,13 +333,24 @@ func (p *Player) ExecuteCommand(commandLine string) { | |
| if p.Dead() { | ||
| return | ||
| } | ||
| args := strings.Split(commandLine, " ") | ||
|
|
||
| name, ok := strings.CutPrefix(args[0], "/") | ||
| commandLine, ok := strings.CutPrefix(commandLine, "/") | ||
| if !ok { | ||
| return | ||
| } | ||
|
|
||
| args := strings.Split(commandLine, " ") | ||
|
|
||
| var ( | ||
| name string | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be good to leave a comment here explaining this is to allow something like |
||
| index int | ||
| ) | ||
| for index, name = range args { | ||
| if name != "" { | ||
| break | ||
| } | ||
| } | ||
| index++ | ||
|
|
||
| command, ok := cmd.ByAlias(name) | ||
| if !ok { | ||
| o := &cmd.Output{} | ||
|
|
@@ -347,10 +359,10 @@ func (p *Player) ExecuteCommand(commandLine string) { | |
| return | ||
| } | ||
| ctx := event.C(p) | ||
| if p.Handler().HandleCommandExecution(ctx, command, args[1:]); ctx.Cancelled() { | ||
| if p.Handler().HandleCommandExecution(ctx, command, args[index:]); ctx.Cancelled() { | ||
| return | ||
| } | ||
| command.Execute(strings.Join(args[1:], " "), p, p.tx) | ||
| command.Execute(strings.Join(args[index:], " "), p, p.tx) | ||
| } | ||
|
|
||
| // Transfer transfers the player to a server at the address passed. If the address could not be resolved, an | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ignoring the empty arg should probably be done in
NextNfor more consistent behavior? It's a bit odd that one would skip over empty args and the other wouldn'tThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would go a step further and do the same for
RemoveN