Skip to content

Commit

Permalink
fix: Improve flag value resets between command executions (#3)
Browse files Browse the repository at this point in the history
* fix: Improve flag value resets between command executions

* doc: changelog
  • Loading branch information
avirtopeanu-ionos authored Nov 24, 2023
1 parent 91b8171 commit 23ae6fb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion comptplus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down

0 comments on commit 23ae6fb

Please sign in to comment.