Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slag: support cobra's pflag interface #24

Merged
merged 1 commit into from
Aug 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" }
Loading