Skip to content

Commit

Permalink
config.ParseFlags(): enforce type constraints via generics, not refle…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
Al2Klimov committed Sep 19, 2024
1 parent 1e2006e commit 10c2644
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/jessevdk/go-flags"
"github.com/pkg/errors"
"os"
"reflect"
)

// ErrInvalidArgument is the error returned by [ParseFlags] or [FromYAMLFile] if
Expand Down Expand Up @@ -58,10 +57,9 @@ func FromYAMLFile[T any, V validatorPtrTo[T]](name string, v V) error {
// ParseFlags prints the help message to [os.Stdout] and exits.
// Note that errors are not printed automatically,
// so error handling is the sole responsibility of the caller.
func ParseFlags(v any) error {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Pointer || rv.IsNil() {
return errors.Wrapf(ErrInvalidArgument, "non-nil pointer expected, got %T", v)
func ParseFlags[T any](v *T) error {
if v == nil {
return errors.Wrap(ErrInvalidArgument, "got nil pointer")
}

parser := flags.NewParser(v, flags.Default^flags.PrintErrors)
Expand Down

0 comments on commit 10c2644

Please sign in to comment.