mirror of
https://github.com/spf13/cobra
synced 2025-04-27 17:17:20 +00:00
Avoid redundant string splits
There likely isn't actually more than once to split in the source strings in these cases, but avoid doing so anyway as we're only interested in the first.
This commit is contained in:
parent
bd4d1655f6
commit
1f495bb3c8
3 changed files with 4 additions and 4 deletions
2
args.go
2
args.go
|
@ -54,7 +54,7 @@ func OnlyValidArgs(cmd *Command, args []string) error {
|
||||||
// A description is following a tab character.
|
// A description is following a tab character.
|
||||||
var validArgs []string
|
var validArgs []string
|
||||||
for _, v := range cmd.ValidArgs {
|
for _, v := range cmd.ValidArgs {
|
||||||
validArgs = append(validArgs, strings.Split(v, "\t")[0])
|
validArgs = append(validArgs, strings.SplitN(v, "\t", 2)[0])
|
||||||
}
|
}
|
||||||
for _, v := range args {
|
for _, v := range args {
|
||||||
if !stringInSlice(v, validArgs) {
|
if !stringInSlice(v, validArgs) {
|
||||||
|
|
|
@ -621,7 +621,7 @@ func writeRequiredNouns(buf io.StringWriter, cmd *Command) {
|
||||||
for _, value := range cmd.ValidArgs {
|
for _, value := range cmd.ValidArgs {
|
||||||
// Remove any description that may be included following a tab character.
|
// Remove any description that may be included following a tab character.
|
||||||
// Descriptions are not supported by bash completion.
|
// Descriptions are not supported by bash completion.
|
||||||
value = strings.Split(value, "\t")[0]
|
value = strings.SplitN(value, "\t", 2)[0]
|
||||||
WriteStringAndCheck(buf, fmt.Sprintf(" must_have_one_noun+=(%q)\n", value))
|
WriteStringAndCheck(buf, fmt.Sprintf(" must_have_one_noun+=(%q)\n", value))
|
||||||
}
|
}
|
||||||
if cmd.ValidArgsFunction != nil {
|
if cmd.ValidArgsFunction != nil {
|
||||||
|
|
|
@ -226,14 +226,14 @@ func (c *Command) initCompleteCmd(args []string) {
|
||||||
}
|
}
|
||||||
if noDescriptions {
|
if noDescriptions {
|
||||||
// Remove any description that may be included following a tab character.
|
// Remove any description that may be included following a tab character.
|
||||||
comp = strings.Split(comp, "\t")[0]
|
comp = strings.SplitN(comp, "\t", 2)[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure we only write the first line to the output.
|
// Make sure we only write the first line to the output.
|
||||||
// This is needed if a description contains a linebreak.
|
// This is needed if a description contains a linebreak.
|
||||||
// Otherwise the shell scripts will interpret the other lines as new flags
|
// Otherwise the shell scripts will interpret the other lines as new flags
|
||||||
// and could therefore provide a wrong completion.
|
// and could therefore provide a wrong completion.
|
||||||
comp = strings.Split(comp, "\n")[0]
|
comp = strings.SplitN(comp, "\n", 2)[0]
|
||||||
|
|
||||||
// Finally trim the completion. This is especially important to get rid
|
// Finally trim the completion. This is especially important to get rid
|
||||||
// of a trailing tab when there are no description following it.
|
// of a trailing tab when there are no description following it.
|
||||||
|
|
Loading…
Add table
Reference in a new issue