Skip to content

Commit

Permalink
slag: support cobra's pflag interface (#24)
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Hall <[email protected]>
  • Loading branch information
imjasonh authored Aug 11, 2024
1 parent d36bc96 commit 4c523ae
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion slag/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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" }

0 comments on commit 4c523ae

Please sign in to comment.