Skip to content

Commit

Permalink
Add support for pflag.FlagSet.IPVar and IPSliceVar
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamSLevy committed Jul 25, 2020
1 parent b9d43a2 commit 4d2be03
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import (
"encoding/json"
"flag"
"fmt"
"net"
"net/url"
"reflect"
"strings"
Expand Down Expand Up @@ -588,6 +589,12 @@ func bindPFlag(fs PFlagSet, tag flagTag, p interface{}, typeName string) bool {
f = fs.VarPF((*JSONRawMessage)(p), tag.Name, tag.ShortName, tag.Usage)
case *url.URL:
f = fs.VarPF((*URL)(p), tag.Name, tag.ShortName, tag.Usage)
case *net.IP:
val := *p
fs.IPVarP(p, tag.Name, tag.ShortName, val, tag.Usage)
case *[]net.IP:
val := *p
fs.IPSliceVarP(p, tag.Name, tag.ShortName, val, tag.Usage)
case *bool:
val := *p
fs.BoolVarP(p, tag.Name, tag.ShortName, val, tag.Usage)
Expand Down
4 changes: 4 additions & 0 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"flag"
"fmt"
"io"
"net"
"net/http"
"net/url"
"testing"
Expand Down Expand Up @@ -190,6 +191,9 @@ type ValidTestFlags struct {
Value TestValue
ValueDefault TestValue `flag:";true;"`

IP net.IP
IPs []net.IP

BoolS []bool
IntS []int
Int64S []int64
Expand Down
4 changes: 4 additions & 0 deletions flagset.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package flagbind

import (
"flag"
"net"
"time"

"github.com/spf13/pflag"
Expand Down Expand Up @@ -86,6 +87,9 @@ type PFlagSet interface {
UintVarP(p *uint, name, short string, value uint, usage string)
UintSliceVarP(p *[]uint, name, short string, value []uint, usage string)

IPVarP(p *net.IP, name, shorthand string, value net.IP, usage string)
IPSliceVarP(p *[]net.IP, name, shorthand string, value []net.IP, usage string)

VarPF(value pflag.Value, name, short string, usage string) *pflag.Flag

Visit(func(*pflag.Flag))
Expand Down

0 comments on commit 4d2be03

Please sign in to comment.