@@ -188,7 +188,7 @@ func Get[T any](c *Command, id string) T {
188188
189189// GetOr looks for a parsed input value with the given id in the given Command and
190190// converts the value to the given type T through an untested type assertion (so this
191- // will panic if the value is found and can't be converted to type T). If the value
191+ // will panic if the value is found but can't be converted to type T). If the value
192192// isn't found, the given fallback value will be returned. To check whether the value
193193// is found instead of using a fallback value, see [Lookup].
194194func GetOr [T any ](c * Command , id string , fallback T ) T {
@@ -198,6 +198,19 @@ func GetOr[T any](c *Command, id string, fallback T) T {
198198 return fallback
199199}
200200
201+ // GetOrFunc is like [GetOr] in that it looks for a parsed input value with the given id
202+ // in the given Command and converts the value to the given type T through an untested
203+ // type assertion (so this will panic if the value is found but can't be converted to
204+ // type T). However, if the value isn't found, it will run the provided function fn in
205+ // order to create and return the fallback value. To check whether the value is found
206+ // instead of using a fallback, see [Lookup].
207+ func GetOrFunc [T any ](c * Command , id string , fn func () T ) T {
208+ if v , ok := Lookup [T ](c , id ); ok {
209+ return v
210+ }
211+ return fn ()
212+ }
213+
201214// ParseOrExit will parse input based on this CommandInfo. If help was requested, it
202215// will print the help message and exit the program successfully (status code 0). If
203216// there is any other error, it will print the error and exit the program with failure
0 commit comments