Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var DefaultLog = Log{
Level: "info",
Format: "text",
}
View Source
var DefaultProm = Prom{
Addr: ":9100",
Path: "/metrics",
}
Functions ¶
func SetFlags ¶
SetFlags configures flags as per the configuration struct. Each exported field creates a flag by the field's name, in lowercase, updating the value of field, If a field is a struct, SetFlags recursively creates flags for that struct's fields.
SetFlags panics if configuration is not a pointer.
Example ¶
package main
import (
"bytes"
"flag"
"fmt"
"time"
"codeberg.org/clambin/go-common/flagger"
)
func main() {
type config struct {
String string `flagger.usage:"a string"` // flagger.usage sets usage text
Int int `flagger.usage:"an int"`
Float float64 `flagger.usage:"a float"`
Bool bool `flagger.name:"boolean" flagger.usage:"a bool"` // flagger.name overrides the name
Duration time.Duration `flagger.usage:"a duration"`
Skipped bool `flagger.skip:"true"`
SubStruct struct {
String string `flagger.usage:"a substring"`
Int int `flagger.name:"int" flagger.usage:"another int"`
} `flagger.name:"sub"`
}
// in a real application, we would use flag.CommandLine directly
f := flag.NewFlagSet("", flag.PanicOnError)
flagger.SetFlags(f, &config{String: "foo", Int: 42, Float: 3.14, Bool: true, Duration: 1 * time.Second})
var output bytes.Buffer
f.SetOutput(&output)
f.Usage()
fmt.Println(output.String())
}
Output: Usage: -boolean a bool (default true) -duration duration a duration (default 1s) -float float a float (default 3.14) -int int an int (default 42) -string string a string (default "foo") -sub.int int another int -sub.string string a substring
Types ¶
Click to show internal directories.
Click to hide internal directories.