Skip to content

Commit 4d38cb4

Browse files
authored
Merge pull request #73 from reeflective/dev
Remove useless flag reset before execution
2 parents 870dca5 + ce35541 commit 4d38cb4

File tree

3 files changed

+9
-42
lines changed

3 files changed

+9
-42
lines changed

example/main-commands.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,8 @@ func mainMenuCommands(app *console.Console) console.Commands {
403403

404404
rootCmd.AddCommand(searchCmd)
405405

406+
var mySlice = []string{"a", "b", "c"}
407+
406408
backupCmd := &cobra.Command{
407409
Use: "backup [flags] SOURCE DESTINATION",
408410
Short: "Create a backup of a file or directory",
@@ -412,7 +414,13 @@ func mainMenuCommands(app *console.Console) console.Commands {
412414
source := args[0]
413415
destination := args[1]
414416

417+
flagsVal, _ := cmd.Flags().GetStringSlice("test-slice")
418+
415419
// Implementation logic for backup command
420+
fmt.Printf("mySlice: %v\n", mySlice)
421+
fmt.Printf("mySlice flags: %v\n", flagsVal)
422+
fmt.Printf("mySlice flags length: %v\n", len(flagsVal))
423+
fmt.Printf("mySlice length: %v\n", len(mySlice))
416424

417425
fmt.Printf("Creating backup of %s to %s\n", source, destination)
418426
},
@@ -421,6 +429,7 @@ func mainMenuCommands(app *console.Console) console.Commands {
421429
backupCmd.Flags().BoolP("incremental", "i", false, "Perform incremental backup")
422430
backupCmd.Flags().StringP("compression", "c", "gzip", "Specify the compression algorithm")
423431
backupCmd.Flags().Bool("dry-run", false, "Perform a dry run without actually creating the backup")
432+
backupCmd.Flags().StringSliceVarP(&mySlice, "test-slice", "T", mySlice, "Testing the shit")
424433
rootCmd.AddCommand(backupCmd)
425434

426435
renameCmd := &cobra.Command{

internal/completion/complete.go

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66

77
"github.com/carapace-sh/carapace/pkg/style"
88
"github.com/carapace-sh/carapace/pkg/xdg"
9-
"github.com/spf13/cobra"
10-
"github.com/spf13/pflag"
119
)
1210

1311
// DefaultStyleConfig sets some default styles for completion.
@@ -32,39 +30,3 @@ func DefaultStyleConfig() {
3230
style.Set("carapace.FlagNoArg", "bright-white")
3331
style.Set("carapace.FlagOptArg", "bright-white")
3432
}
35-
36-
// ResetFlagsDefaults resets all flags to their default values.
37-
//
38-
// Slice flags accumulate per execution (and do not reset),
39-
//
40-
// so we must reset them manually.
41-
//
42-
// Example:
43-
//
44-
// Given cmd.Flags().StringSlice("comment", nil, "")
45-
// If you run a command with --comment "a" --comment "b" you will get
46-
// the expected [a, b] slice.
47-
//
48-
// If you run a command again with no --comment flags, you will get
49-
// [a, b] again instead of an empty slice.
50-
//
51-
// If you run the command again with --comment "c" --comment "d" flags,
52-
// you will get [a, b, c, d] instead of just [c, d].
53-
func ResetFlagsDefaults(target *cobra.Command) {
54-
target.Flags().VisitAll(func(flag *pflag.Flag) {
55-
flag.Changed = false
56-
switch value := flag.Value.(type) {
57-
case pflag.SliceValue:
58-
var res []string
59-
60-
if len(flag.DefValue) > 0 && flag.DefValue != "[]" {
61-
res = append(res, flag.DefValue)
62-
}
63-
64-
value.Replace(res)
65-
66-
default:
67-
flag.Value.Set(flag.DefValue)
68-
}
69-
})
70-
}

run.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/kballard/go-shellquote"
1212
"github.com/spf13/cobra"
1313

14-
"github.com/reeflective/console/internal/completion"
1514
"github.com/reeflective/console/internal/line"
1615
)
1716

@@ -159,9 +158,6 @@ func (c *Console) execute(ctx context.Context, menu *Menu, args []string, async
159158
return err
160159
}
161160

162-
// Reset all flags to their default values.
163-
completion.ResetFlagsDefaults(target)
164-
165161
// Console-wide pre-run hooks, cannot.
166162
if err := c.runAllE(c.PreCmdRunHooks); err != nil {
167163
return fmt.Errorf("pre-run error: %s", err.Error())

0 commit comments

Comments
 (0)