Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions example/main-commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
},
Expand All @@ -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{
Expand Down
38 changes: 0 additions & 38 deletions internal/completion/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
})
}
4 changes: 0 additions & 4 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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())
Expand Down
Loading