diff --git a/example/main-commands.go b/example/main-commands.go index 884787c..56a5010 100644 --- a/example/main-commands.go +++ b/example/main-commands.go @@ -403,6 +403,8 @@ func mainMenuCommands(app *console.Console) console.Commands { rootCmd.AddCommand(searchCmd) + var mySlice = []string{"a", "b", "c"} + backupCmd := &cobra.Command{ Use: "backup [flags] SOURCE DESTINATION", Short: "Create a backup of a file or directory", @@ -412,7 +414,13 @@ func mainMenuCommands(app *console.Console) console.Commands { source := args[0] destination := args[1] + flagsVal, _ := cmd.Flags().GetStringSlice("test-slice") + // Implementation logic for backup command + fmt.Printf("mySlice: %v\n", mySlice) + fmt.Printf("mySlice flags: %v\n", flagsVal) + fmt.Printf("mySlice flags length: %v\n", len(flagsVal)) + fmt.Printf("mySlice length: %v\n", len(mySlice)) fmt.Printf("Creating backup of %s to %s\n", source, destination) }, @@ -421,6 +429,7 @@ func mainMenuCommands(app *console.Console) console.Commands { backupCmd.Flags().BoolP("incremental", "i", false, "Perform incremental backup") backupCmd.Flags().StringP("compression", "c", "gzip", "Specify the compression algorithm") backupCmd.Flags().Bool("dry-run", false, "Perform a dry run without actually creating the backup") + backupCmd.Flags().StringSliceVarP(&mySlice, "test-slice", "T", mySlice, "Testing the shit") rootCmd.AddCommand(backupCmd) renameCmd := &cobra.Command{ diff --git a/internal/completion/complete.go b/internal/completion/complete.go index 7f72841..7c27144 100644 --- a/internal/completion/complete.go +++ b/internal/completion/complete.go @@ -6,8 +6,6 @@ import ( "github.com/carapace-sh/carapace/pkg/style" "github.com/carapace-sh/carapace/pkg/xdg" - "github.com/spf13/cobra" - "github.com/spf13/pflag" ) // DefaultStyleConfig sets some default styles for completion. @@ -32,39 +30,3 @@ func DefaultStyleConfig() { style.Set("carapace.FlagNoArg", "bright-white") style.Set("carapace.FlagOptArg", "bright-white") } - -// ResetFlagsDefaults resets all flags to their default values. -// -// Slice flags accumulate per execution (and do not reset), -// -// so we must reset them manually. -// -// Example: -// -// Given cmd.Flags().StringSlice("comment", nil, "") -// If you run a command with --comment "a" --comment "b" you will get -// the expected [a, b] slice. -// -// If you run a command again with no --comment flags, you will get -// [a, b] again instead of an empty slice. -// -// If you run the command again with --comment "c" --comment "d" flags, -// you will get [a, b, c, d] instead of just [c, d]. -func ResetFlagsDefaults(target *cobra.Command) { - target.Flags().VisitAll(func(flag *pflag.Flag) { - flag.Changed = false - switch value := flag.Value.(type) { - case pflag.SliceValue: - var res []string - - if len(flag.DefValue) > 0 && flag.DefValue != "[]" { - res = append(res, flag.DefValue) - } - - value.Replace(res) - - default: - flag.Value.Set(flag.DefValue) - } - }) -} diff --git a/run.go b/run.go index 3ce5c19..a237449 100644 --- a/run.go +++ b/run.go @@ -11,7 +11,6 @@ import ( "github.com/kballard/go-shellquote" "github.com/spf13/cobra" - "github.com/reeflective/console/internal/completion" "github.com/reeflective/console/internal/line" ) @@ -159,9 +158,6 @@ func (c *Console) execute(ctx context.Context, menu *Menu, args []string, async return err } - // Reset all flags to their default values. - completion.ResetFlagsDefaults(target) - // Console-wide pre-run hooks, cannot. if err := c.runAllE(c.PreCmdRunHooks); err != nil { return fmt.Errorf("pre-run error: %s", err.Error())