manifoldco / promptui

Interactive prompt for command-line applications
https://www.manifold.co
BSD 3-Clause "New" or "Revised" License
6.07k stars 336 forks source link

A after hook to change the received value #85

Open Ilyes512 opened 5 years ago

Ilyes512 commented 5 years ago

It would be nice to have some sort of an after hook added to promptui.Prompt{} struct so we can for example cast a received string to a number. So some sort of value a transformer:

AfterHook: func(input string) (error, uint64) {
    transformed, err := strconv.ParseUint(input, 10, 64)
    if err != nil {
        return fmt.Errorf("failed transforming received input to uint64: %v", input), 0
    }
    return nil, transformed
}

Or does this not fall into what promptui should do?

jspc commented 2 years ago

I'd quite like this; I'm facing a scenario where I'd like to downcase strings. Right now I need to do this in my validate function and in whatever receives this input.