Skip to content

Commit b5d9d48

Browse files
committed
Get rid of reset functions, not needed anymore
1 parent ba70f41 commit b5d9d48

File tree

3 files changed

+22
-61
lines changed

3 files changed

+22
-61
lines changed

gen/completions/command.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,12 @@ import (
55
"reflect"
66

77
"github.com/reeflective/flags"
8-
genflags "github.com/reeflective/flags/gen/flags"
98
"github.com/reeflective/flags/internal/scan"
109
"github.com/reeflective/flags/internal/tag"
1110
comp "github.com/rsteube/carapace"
1211
"github.com/spf13/cobra"
1312
)
1413

15-
var onFinalize func()
16-
17-
// WithReset has a similar role to flags.WithReset(), but for completions:
18-
// when invoked, this function will create a new instance of the command-tree
19-
// struct passed to Generate(), will rescan it and bind new completers with
20-
// their blank/default state.
21-
// In the carapace library, this function is called after each completer invocation.
22-
func WithReset() func() {
23-
return onFinalize
24-
}
25-
2614
// Gen uses a carapace completion builder to register various completions to its underlying
2715
// cobra command, parsing again the native struct for type and struct tags' information.
2816
// Arguments:
@@ -38,21 +26,6 @@ func Generate(cmd *cobra.Command, data interface{}, comps *comp.Carapace) (*comp
3826
return completions, err
3927
}
4028

41-
// And make a handler to be ran after each completion routine,
42-
// so that commands/flags are reset to their blank/default state.
43-
onFinalize = func() {
44-
resetCommands := genflags.WithReset()
45-
resetCommands()
46-
47-
// Instantiate a new command struct
48-
val := reflect.ValueOf(data).Elem()
49-
data = val.Addr().Interface()
50-
51-
if _, err := generate(cmd.Root(), data, comps); err != nil {
52-
return
53-
}
54-
}
55-
5629
return completions, nil
5730
}
5831

gen/flags/command.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,6 @@ import (
1212
"github.com/spf13/cobra"
1313
)
1414

15-
var onFinalize func()
16-
17-
// WithReset registers a function to cobra finalizers: when invoked, this function
18-
// will create a new instance of the command-tree struct passed to Generate(), will
19-
// rescan it and bind new commands with their blank/default state.
20-
// This should normally only be used if you are using commands within a closed
21-
// loop Go program/shell, where commands will be invoked more than once.
22-
//
23-
// The associated function is returned, so that programs can reuse it at other times.
24-
func WithReset() func() {
25-
cobra.OnFinalize(onFinalize)
26-
return onFinalize
27-
}
28-
2915
// Generate returns a root cobra Command to be used directly as an entry-point.
3016
// The data interface parameter can be nil, or arbitrarily:
3117
// - A simple group of options to bind at the local, root level
@@ -40,24 +26,6 @@ func Generate(data interface{}, opts ...flags.OptFunc) *cobra.Command {
4026
// Scan the struct and bind all commands to this root.
4127
generate(cmd, data, opts...)
4228

43-
// Make a handler to be used when this command
44-
// tree is used in a closed-loop Go program/shell.
45-
onFinalize = func() {
46-
cmd.ResetCommands()
47-
48-
// Instantiate a new command struct
49-
val := reflect.ValueOf(data)
50-
if val.Kind() == reflect.Ptr {
51-
val = reflect.Indirect(val)
52-
}
53-
54-
data := reflect.New(val.Type()).Interface()
55-
56-
// And scan again to rebind all commands
57-
// to their blank/default state.
58-
generate(cmd, data, opts...)
59-
}
60-
6129
return cmd
6230
}
6331

tag.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ func parseFlagTag(field reflect.StructField, options opts) (*Flag, *tag.MultiTag
3131
return nil, flagTags, nil
3232
}
3333

34-
flag.DefValue = flagTags.GetMany("default")
34+
setFlagDefaultValues(flag, flagTags.GetMany("default"))
3535
setFlagChoices(flag, flagTags.GetMany("choice"))
36-
flag.OptionalValue = flagTags.GetMany("optional-value")
36+
setFlagOptionalValues(flag, flagTags.GetMany("optional-value"))
3737

3838
if options.Prefix != "" && !ignorePrefix {
3939
flag.Name = options.Prefix + flag.Name
@@ -186,6 +186,16 @@ func parseEnvTag(flagName string, field reflect.StructField, options opts) strin
186186
return envVar
187187
}
188188

189+
func setFlagDefaultValues(flag *Flag, choices []string) {
190+
var allChoices []string
191+
192+
for _, choice := range choices {
193+
allChoices = append(allChoices, strings.Split(choice, " ")...)
194+
}
195+
196+
flag.DefValue = allChoices
197+
}
198+
189199
func setFlagChoices(flag *Flag, choices []string) {
190200
var allChoices []string
191201

@@ -196,6 +206,16 @@ func setFlagChoices(flag *Flag, choices []string) {
196206
flag.Choices = allChoices
197207
}
198208

209+
func setFlagOptionalValues(flag *Flag, choices []string) {
210+
var allChoices []string
211+
212+
for _, choice := range choices {
213+
allChoices = append(allChoices, strings.Split(choice, " ")...)
214+
}
215+
216+
flag.OptionalValue = allChoices
217+
}
218+
199219
func hasOption(options []string, option string) bool {
200220
for _, opt := range options {
201221
if opt == option {

0 commit comments

Comments
 (0)