diff --git a/slag/flag.go b/slag/flag.go index 790175b..316e17e 100644 --- a/slag/flag.go +++ b/slag/flag.go @@ -13,7 +13,18 @@ // // $ ./myprogram -log-level=debug // -// The slag.Level type is a wrapper around slog.Level that implements the flag.Value interface. +// The slag.Level type is a wrapper around slog.Level that implements the flag.Value interface, +// as well as Cobra's pflag.Value interface. +// +// func main() { +// var level slag.Level +// cmd := &cobra.Command{ +// Use: "myprogram", +// ... +// } +// cmd.PersistentFlags().Var(&level, "log-level", "log level") +// cmd.Execute() +// } package slag import "log/slog" @@ -30,3 +41,6 @@ func (l *Level) Set(s string) error { } func (l *Level) String() string { return slog.Level(*l).String() } func (l *Level) Level() slog.Level { return slog.Level(*l) } + +// Implements https://pkg.go.dev/github.com/spf13/pflag#Value +func (l *Level) Type() string { return "string" }