Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added custom reset behaviours #2

Merged
merged 1 commit into from
Nov 23, 2023
Merged
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
17 changes: 10 additions & 7 deletions comptplus.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ type CobraPrompt struct {
// PersistFlagValues will persist flags. For example have verbose turned on every command.
PersistFlagValues bool

// CustomFlagResetBehaviour allows you to specify custom behaviour which will be ran after each command, if PersistFlagValues is false
CustomFlagResetBehaviour func(pflag *pflag.Flag)

// ShowHelpCommandAndFlags will make help command and flag for every command available.
ShowHelpCommandAndFlags bool

Expand Down Expand Up @@ -87,6 +90,12 @@ func (co *CobraPrompt) RunContext(ctx context.Context) {
co.HookAfter = func(_ string) {}
}

if co.CustomFlagResetBehaviour == nil {
co.CustomFlagResetBehaviour = func(flag *pflag.Flag) {
flag.Value.Set(flag.DefValue)
}
}

co.prepareCommands()

p := prompt.New(
Expand All @@ -100,14 +109,8 @@ func (co *CobraPrompt) RunContext(ctx context.Context) {
func (co *CobraPrompt) resetFlagsToDefault(cmd *cobra.Command) {
// Define the resetFlags function within resetFlagsToDefault
resetFlags := func(c *cobra.Command) {
c.LocalFlags().VisitAll(func(flag *pflag.Flag) {
flag.Value.Set(flag.DefValue)
})
c.InheritedFlags().VisitAll(func(flag *pflag.Flag) {
flag.Value.Set(flag.DefValue)
})
c.Flags().VisitAll(func(flag *pflag.Flag) {
flag.Value.Set(flag.DefValue)
co.CustomFlagResetBehaviour(flag)
})
}

Expand Down