Skip to content
Merged
Show file tree
Hide file tree
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
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import "github.com/itzg/go-flagsfiller"
- Allows defaults to be given via struct tag `default`
- Falls back to using instance field values as declared default
- Declare flag usage via struct tag `usage`
- Mark flags as required via struct tag `required` and validate with `Verify()` method (cannot be combined with `default` tag)
- Can be combined with other modules, such as [google/subcommands](https://github.com/google/subcommands) for sub-command processing. Can also be integrated with [spf13/cobra](https://github.com/spf13/cobra) by using pflag's [AddGoFlagSet](https://godoc.org/github.com/spf13/pflag#FlagSet.AddGoFlagSet)
- Beyond the standard types supported by flag.FlagSet also includes support for:
- `[]string` where repetition of the argument appends to the slice and/or an argument value can contain a comma or newline-separated list of values. For example: `--arg one --arg two,three`
Expand Down Expand Up @@ -93,6 +94,36 @@ The following shows an example of the usage provided when passing `--help`:
How long to wait (default 5s)
```

## Required flags

Flags can be marked as required using the `required:"true"` struct tag. After parsing command-line arguments, call the `Verify()` method to ensure all required flags have been provided:

```go
type Config struct {
Host string `required:"true" usage:"The remote host"`
Port int `default:"8080" usage:"The port"`
Username string `required:"true" usage:"Username for authentication"`
}

var config Config

filler := flagsfiller.New()
err := filler.Fill(flag.CommandLine, &config)
if err != nil {
log.Fatal(err)
}

flag.Parse()

// Verify all required fields are set
err = filler.Verify()
if err != nil {
log.Fatal(err) // Will fail if Host or Username not provided
}
```

**Note:** A field cannot be both required and have a default value. Attempting to use both tags will result in an error during `Fill()`.

## Real world example

[saml-auth-proxy](https://github.com/itzg/saml-auth-proxy) shows an end-to-end usage of flagsfiller where the main function fills the flags, maps those to environment variables with [envy](https://github.com/jamiealquiza/envy), and parses the command line:
Expand Down
40 changes: 38 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package flagsfiller_test
import (
"flag"
"fmt"
"github.com/itzg/go-flagsfiller"
"log"
"time"

"github.com/itzg/go-flagsfiller"
)

func Example() {
type Config struct {
Host string `default:"localhost" usage:"The remote host"`
Enabled bool `default:"true" usage:"Turn it on"`
Automatic bool `default:"false" usage:"Make it automatic" aliases:"a"`
Retries int `default:"1" usage:"Retry" aliases:"r,t"`
Retries int `default:"1" usage:"Retry" aliases:"r,t"`
Timeout time.Duration `default:"5s" usage:"How long to wait"`
}

Expand All @@ -36,3 +37,38 @@ func Example() {
// Output:
// {Host:external.svc Enabled:true Automatic:true Retries:2 Timeout:10m0s}
}

func ExampleFlagSetFiller_Verify() {
type Config struct {
Host string `required:"true" usage:"The remote host"`
Port int `default:"8080" usage:"The port"`
Username string `required:"true" usage:"Username for authentication"`
}

var config Config

flagset := flag.NewFlagSet("ExampleVerify", flag.ContinueOnError)

filler := flagsfiller.New()
err := filler.Fill(flagset, &config)
if err != nil {
log.Fatal(err)
}

// Parse with required fields provided
err = flagset.Parse([]string{"--host", "example.com", "--username", "admin"})
if err != nil {
log.Fatal(err)
}

// Verify all required fields are set
err = filler.Verify()
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}

fmt.Printf("Config validated: Host=%s, Port=%d, Username=%s\n", config.Host, config.Port, config.Username)
// Output:
// Config validated: Host=example.com, Port=8080, Username=admin
}
Loading