Add function "AddToCommandFlags" to FlagValidator interface
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 33s

Signed-off-by: Jan Tytgat <jan.tytgat@corelayer.eu>
This commit is contained in:
Jan Tytgat
2025-06-12 17:01:05 +02:00
parent 457a6c8742
commit 13831390bd
2 changed files with 16 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/Oudwins/zog"
"github.com/spf13/pflag"
)
func NewBoolFlag(name string, schema *zog.BoolSchema[bool], usage string) BoolFlag {
@ -40,6 +41,10 @@ func (f BoolFlag) Validate() ([]string, error) {
return messages, nil
}
func (f BoolFlag) AddToCommandFlags(flagset *pflag.FlagSet, shorthand string, value interface{}) {
flagset.BoolVarP(&f.Value, f.Name(), shorthand, value.(bool), f.usage)
}
func NewInt64Flag(name string, schema *zog.NumberSchema[int64], usage string) Int64Flag {
return Int64Flag{
name: name,
@ -74,6 +79,10 @@ func (f Int64Flag) Validate() ([]string, error) {
return messages, nil
}
func (f Int64Flag) AddToCommandFlags(flagset *pflag.FlagSet, shorthand string, value interface{}) {
flagset.Int64VarP(&f.Value, f.Name(), shorthand, value.(int64), f.usage)
}
func NewStringFlag(name string, schema *zog.StringSchema[string], usage string) StringFlag {
return StringFlag{
name: name,
@ -107,3 +116,7 @@ func (f StringFlag) Validate() ([]string, error) {
}
return messages, nil
}
func (f StringFlag) AddToCommandFlags(flagset *pflag.FlagSet, shorthand string, value interface{}) {
flagset.StringVarP(&f.Value, f.Name(), shorthand, value.(string), f.usage)
}

View File

@ -3,12 +3,15 @@ package flagzog
import (
"context"
"log/slog"
"github.com/spf13/pflag"
)
type FlagValidator interface {
Name() string
Validate() ([]string, error)
Usage() string
AddToCommandFlags(flagset *pflag.FlagSet, shorthand string, value interface{})
}
func ValidateFlags(ctx context.Context, logger *slog.Logger, flags []FlagValidator) ([]string, error) {