From 23ae6fbc60637b6e64e2890fe12d6211e3d6013b Mon Sep 17 00:00:00 2001 From: Alexandru-Claudius Virtopeanu Date: Fri, 24 Nov 2023 13:45:27 +0200 Subject: [PATCH] fix: Improve flag value resets between command executions (#3) * fix: Improve flag value resets between command executions * doc: changelog --- CHANGELOG.md | 24 ++++++++++++++++++++++++ comptplus.go | 22 +++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a002c09..0f6b29a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,30 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.0.2] - 2023-11-24 + +### Fixed +* fixed flag reset behaviour for slice/array flags by using casting to `SliceValue` and using `SliceValue.Reset()` by @avirtopeanu-ionos in https://github.com/ionoscloudsdk/comptplus/pull/3 + * In v1.0.1 and before, including in the original cobra-prompt repository, the defaults would be appended to the values of the previous execution + +## [1.0.1] - 2023-11-23 + +### Added +* added the option to set custom flag reset behaviours by @avirtopeanu-ionos in https://github.com/ionoscloudsdk/comptplus/pull/2 + +## [1.0.0] - 2023-11-22 + +### Added +* added completions for flag values. by @avirtopeanu-ionos in https://github.com/ionoscloudsdk/comptplus/pull/1 + * default cache duration for responses set to 500ms - to prevent laggy user interaction + * support flag descriptions by splitting on `\t` +* added `HookBefore` and `HookAfter` for additional actions before and after command execution. + +### Changed +* `PersistFlagValues` behavior: + * instead of adding a flag, setting PersistFlagValues to true will directly influence persistance throughout the entire shell session. + * instead of resetting flags to their default value every time a new character is typed, flag defaults are set after a command execution. + ## [0.5.0] - 2023-01-28 ### Added diff --git a/comptplus.go b/comptplus.go index 8f5255f..4462196 100644 --- a/comptplus.go +++ b/comptplus.go @@ -92,7 +92,27 @@ func (co *CobraPrompt) RunContext(ctx context.Context) { if co.CustomFlagResetBehaviour == nil { co.CustomFlagResetBehaviour = func(flag *pflag.Flag) { - flag.Value.Set(flag.DefValue) + sliceValue, ok := flag.Value.(pflag.SliceValue) + if !ok { + // For non-slice flags, just set to the default value + flag.Value.Set(flag.DefValue) + return + } + + defValue := strings.Trim(flag.DefValue, "[]") + defaultSlice := strings.Split(defValue, ",") + err := sliceValue.Replace(defaultSlice) + + if err != nil { + // If there's an error parsing defaultSlice as a slice, try this workaround + errShouldNeverHappenButWeAreProfessionals := sliceValue.Replace([]string{}) + if errShouldNeverHappenButWeAreProfessionals == nil { + // If this check wouldn't exist and we would have some error parsing the nil value, + // it would actually append the default value to the previous user's value + flag.Value.Set(flag.DefValue) + } + return + } } }