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

Assorted small fixups #380

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 0 additions & 2 deletions bool_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func newBoolSliceValue(val []bool, p *[]bool) *boolSliceValue {
// Set converts, and assigns, the comma-separated boolean argument string representation as the []bool value of this flag.
// If Set is called on a flag that already has a []bool assigned, the newly converted values will be appended.
func (s *boolSliceValue) Set(val string) error {

// remove all quote characters
rmQuote := strings.NewReplacer(`"`, "", `'`, "", "`", "")

Expand Down Expand Up @@ -60,7 +59,6 @@ func (s *boolSliceValue) Type() string {

// String defines a "native" format for this boolean slice flag value.
func (s *boolSliceValue) String() string {

boolStrSlice := make([]string, len(*s.value))
for i, b := range *s.value {
boolStrSlice[i] = strconv.FormatBool(b)
Expand Down
1 change: 0 additions & 1 deletion bool_slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ func TestBSAsSliceValue(t *testing.T) {
}

func TestBSBadQuoting(t *testing.T) {

tests := []struct {
Want []bool
FlagArg []string
Expand Down
6 changes: 0 additions & 6 deletions bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func (bytesHex bytesHexValue) String() string {
// Set implements pflag.Value.Set.
func (bytesHex *bytesHexValue) Set(value string) error {
bin, err := hex.DecodeString(strings.TrimSpace(value))

if err != nil {
return err
}
Expand All @@ -39,7 +38,6 @@ func newBytesHexValue(val []byte, p *[]byte) *bytesHexValue {
}

func bytesHexConv(sval string) (interface{}, error) {

bin, err := hex.DecodeString(sval)

if err == nil {
Expand All @@ -52,7 +50,6 @@ func bytesHexConv(sval string) (interface{}, error) {
// GetBytesHex return the []byte value of a flag with the given name
func (f *FlagSet) GetBytesHex(name string) ([]byte, error) {
val, err := f.getFlagType(name, "bytesHex", bytesHexConv)

if err != nil {
return []byte{}, err
}
Expand Down Expand Up @@ -119,7 +116,6 @@ func (bytesBase64 bytesBase64Value) String() string {
// Set implements pflag.Value.Set.
func (bytesBase64 *bytesBase64Value) Set(value string) error {
bin, err := base64.StdEncoding.DecodeString(strings.TrimSpace(value))

if err != nil {
return err
}
Expand All @@ -140,7 +136,6 @@ func newBytesBase64Value(val []byte, p *[]byte) *bytesBase64Value {
}

func bytesBase64ValueConv(sval string) (interface{}, error) {

bin, err := base64.StdEncoding.DecodeString(sval)
if err == nil {
return bin, nil
Expand All @@ -152,7 +147,6 @@ func bytesBase64ValueConv(sval string) (interface{}, error) {
// GetBytesBase64 return the []byte value of a flag with the given name
func (f *FlagSet) GetBytesBase64(name string) ([]byte, error) {
val, err := f.getFlagType(name, "bytesBase64", bytesBase64ValueConv)

if err != nil {
return []byte{}, err
}
Expand Down
28 changes: 20 additions & 8 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,32 @@ unaffected.
Define flags using flag.String(), Bool(), Int(), etc.

This declares an integer flag, -flagname, stored in the pointer ip, with type *int.

var ip = flag.Int("flagname", 1234, "help message for flagname")

If you like, you can bind the flag to a variable using the Var() functions.

var flagvar int
func init() {
flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
}

Or you can create custom flags that satisfy the Value interface (with
pointer receivers) and couple them to flag parsing by

flag.Var(&flagVal, "name", "help message for flagname")

For such flags, the default value is just the initial value of the variable.

After all flags are defined, call

flag.Parse()

to parse the command line into the defined flags.

Flags may then be used directly. If you're using the flags themselves,
they are all pointers; if you bind to variables, they're values.

fmt.Println("ip has value ", *ip)
fmt.Println("flagvar has value ", flagvar)

Expand All @@ -54,22 +63,26 @@ The arguments are indexed from 0 through flag.NArg()-1.
The pflag package also defines some new functions that are not in flag,
that give one-letter shorthands for flags. You can use these by appending
'P' to the name of any function that defines a flag.

var ip = flag.IntP("flagname", "f", 1234, "help message")
var flagvar bool
func init() {
flag.BoolVarP(&flagvar, "boolname", "b", true, "help message")
}
flag.VarP(&flagval, "varname", "v", "help message")

Shorthand letters can be used with single dashes on the command line.
Boolean shorthand flags can be combined with other shorthand flags.

Command line flag syntax:

--flag // boolean flags only
--flag=x

Unlike the flag package, a single dash before an option means something
different than a double dash. Single dashes signify a series of shorthand
letters for flags. All but the last shorthand letter must be boolean flags.

// boolean flags
-f
-abc
Expand Down Expand Up @@ -365,7 +378,7 @@ func (f *FlagSet) ShorthandLookup(name string) *Flag {
}
if len(name) > 1 {
msg := fmt.Sprintf("can not look up shorthand which is more than one ASCII character: %q", name)
fmt.Fprintf(f.Output(), msg)
fmt.Fprintln(f.Output(), msg)
panic(msg)
}
c := name[0]
Expand Down Expand Up @@ -675,7 +688,6 @@ func wrap(i, w int, s string) string {
}

return r

}

// FlagUsagesWrapped returns a string containing the usage information
Expand Down Expand Up @@ -867,7 +879,7 @@ func (f *FlagSet) AddFlag(flag *Flag) {
}
if len(flag.Shorthand) > 1 {
msg := fmt.Sprintf("%q shorthand is more than one ASCII character", flag.Shorthand)
fmt.Fprintf(f.Output(), msg)
fmt.Fprintln(f.Output(), msg)
panic(msg)
}
if f.shorthands == nil {
Expand All @@ -877,7 +889,7 @@ func (f *FlagSet) AddFlag(flag *Flag) {
used, alreadyThere := f.shorthands[c]
if alreadyThere {
msg := fmt.Sprintf("unable to redefine %q shorthand in %q flagset: it's already used for %q flag", c, f.name, used.Name)
fmt.Fprintf(f.Output(), msg)
fmt.Fprintln(f.Output(), msg)
panic(msg)
}
f.shorthands[c] = flag
Expand Down Expand Up @@ -934,9 +946,9 @@ func (f *FlagSet) usage() {
}
}

//--unknown (args will be empty)
//--unknown --next-flag ... (args will be --next-flag ...)
//--unknown arg ... (args will be arg ...)
// --unknown (args will be empty)
// --unknown --next-flag ... (args will be --next-flag ...)
// --unknown arg ... (args will be arg ...)
func stripUnknownFlagValue(args []string) []string {
if len(args) == 0 {
//--unknown
Expand Down Expand Up @@ -1135,7 +1147,7 @@ func (f *FlagSet) Parse(arguments []string) error {
}
f.parsed = true

if len(arguments) < 0 {
if len(arguments) == 0 {
return nil
}

Expand Down
11 changes: 4 additions & 7 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func testParseWithUnknownFlags(f *FlagSet, t *testing.T) {
"-u=unknown3Value",
"-p",
"unknown4Value",
"-q", //another unknown with bool value
"-q", // another unknown with bool value
"-y",
"ee",
"--unknown7=unknown7value",
Expand Down Expand Up @@ -899,7 +899,7 @@ func TestChangingArgs(t *testing.T) {

// Test that -help invokes the usage message and returns ErrHelp.
func TestHelp(t *testing.T) {
var helpCalled = false
helpCalled := false
fs := NewFlagSet("help test", ContinueOnError)
fs.Usage = func() { helpCalled = true }
var flag bool
Expand Down Expand Up @@ -998,6 +998,7 @@ func getDeprecatedFlagSet() *FlagSet {
f.MarkDeprecated("badflag", "use --good-flag instead")
return f
}

func TestDeprecatedFlagInDocs(t *testing.T) {
f := getDeprecatedFlagSet()

Expand Down Expand Up @@ -1134,7 +1135,6 @@ func TestMultipleNormalizeFlagNameInvocations(t *testing.T) {
}
}

//
func TestHiddenFlagInUsage(t *testing.T) {
f := NewFlagSet("bob", ContinueOnError)
f.Bool("secretFlag", true, "shhh")
Expand All @@ -1149,7 +1149,6 @@ func TestHiddenFlagInUsage(t *testing.T) {
}
}

//
func TestHiddenFlagUsage(t *testing.T) {
f := NewFlagSet("bob", ContinueOnError)
f.Bool("secretFlag", true, "shhh")
Expand Down Expand Up @@ -1238,9 +1237,7 @@ func TestPrintDefaults(t *testing.T) {
fs.PrintDefaults()
got := buf.String()
if got != defaultOutput {
fmt.Println("\n" + got)
fmt.Println("\n" + defaultOutput)
t.Errorf("got %q want %q\n", got, defaultOutput)
t.Errorf("got %q want %q", got, defaultOutput)
}
}

Expand Down
2 changes: 1 addition & 1 deletion golangflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func PFlagFromGoFlag(goflag *goflag.Flag) *Flag {
Usage: goflag.Usage,
Value: wrapFlagValue(goflag.Value),
// Looks like golang flags don't set DefValue correctly :-(
//DefValue: goflag.DefValue,
// DefValue: goflag.DefValue,
DefValue: goflag.Value.String(),
}
// Ex: if the golang flag was -v, allow both -v and --v to work
Expand Down
2 changes: 0 additions & 2 deletions ip_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func newIPSliceValue(val []net.IP, p *[]net.IP) *ipSliceValue {
// Set converts, and assigns, the comma-separated IP argument string representation as the []net.IP value of this flag.
// If Set is called on a flag that already has a []net.IP assigned, the newly converted values will be appended.
func (s *ipSliceValue) Set(val string) error {

// remove all quote characters
rmQuote := strings.NewReplacer(`"`, "", `'`, "", "`", "")

Expand Down Expand Up @@ -61,7 +60,6 @@ func (s *ipSliceValue) Type() string {

// String defines a "native" format for this net.IP slice flag value.
func (s *ipSliceValue) String() string {

ipStrSlice := make([]string, len(*s.value))
for i, ip := range *s.value {
ipStrSlice[i] = ip.String()
Expand Down
4 changes: 2 additions & 2 deletions ip_slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ func TestIPSAsSliceValue(t *testing.T) {
}

func TestIPSBadQuoting(t *testing.T) {

tests := []struct {
Want []net.IP
FlagArg []string
Expand Down Expand Up @@ -222,7 +221,8 @@ func TestIPSBadQuoting(t *testing.T) {
},
FlagArg: []string{
`"2e5e:66b2:6441:848:5b74:76ea:574c:3a7b, 2e5e:66b2:6441:848:5b74:76ea:574c:3a7b,2e5e:66b2:6441:848:5b74:76ea:574c:3a7b "`,
" 2e5e:66b2:6441:848:5b74:76ea:574c:3a7b"},
" 2e5e:66b2:6441:848:5b74:76ea:574c:3a7b",
},
},
}

Expand Down
2 changes: 0 additions & 2 deletions ipnet_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func newIPNetSliceValue(val []net.IPNet, p *[]net.IPNet) *ipNetSliceValue {
// Set converts, and assigns, the comma-separated IPNet argument string representation as the []net.IPNet value of this flag.
// If Set is called on a flag that already has a []net.IPNet assigned, the newly converted values will be appended.
func (s *ipNetSliceValue) Set(val string) error {

// remove all quote characters
rmQuote := strings.NewReplacer(`"`, "", `'`, "", "`", "")

Expand Down Expand Up @@ -61,7 +60,6 @@ func (s *ipNetSliceValue) Type() string {

// String defines a "native" format for this net.IPNet slice flag value.
func (s *ipNetSliceValue) String() string {

ipNetStrSlice := make([]string, len(*s.value))
for i, n := range *s.value {
ipNetStrSlice[i] = n.String()
Expand Down
4 changes: 2 additions & 2 deletions ipnet_slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ func TestIPNetCalledTwice(t *testing.T) {
}

func TestIPNetBadQuoting(t *testing.T) {

tests := []struct {
Want []net.IPNet
FlagArg []string
Expand Down Expand Up @@ -216,7 +215,8 @@ func TestIPNetBadQuoting(t *testing.T) {
},
FlagArg: []string{
`"2e5e:66b2:6441:848:5b74:76ea:574c:3a7b/128, 2e5e:66b2:6441:848:5b74:76ea:574c:3a7b/128,2e5e:66b2:6441:848:5b74:76ea:574c:3a7b/128 "`,
" 2e5e:66b2:6441:848:5b74:76ea:574c:3a7b/128"},
" 2e5e:66b2:6441:848:5b74:76ea:574c:3a7b/128",
},
},
}

Expand Down
1 change: 1 addition & 0 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func (s *stringValue) Set(val string) error {
*s = stringValue(val)
return nil
}

func (s *stringValue) Type() string {
return "string"
}
Expand Down
28 changes: 20 additions & 8 deletions string_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,12 @@ func (f *FlagSet) GetStringSlice(name string) ([]string, error) {
// The argument p points to a []string variable in which to store the value of the flag.
// Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
// For example:
// --ss="v1,v2" --ss="v3"
//
// --ss="v1,v2" --ss="v3"
//
// will result in
// []string{"v1", "v2", "v3"}
//
// []string{"v1", "v2", "v3"}
func (f *FlagSet) StringSliceVar(p *[]string, name string, value []string, usage string) {
f.VarP(newStringSliceValue(value, p), name, "", usage)
}
Expand All @@ -114,9 +117,12 @@ func (f *FlagSet) StringSliceVarP(p *[]string, name, shorthand string, value []s
// The argument p points to a []string variable in which to store the value of the flag.
// Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
// For example:
// --ss="v1,v2" --ss="v3"
//
// --ss="v1,v2" --ss="v3"
//
// will result in
// []string{"v1", "v2", "v3"}
//
// []string{"v1", "v2", "v3"}
func StringSliceVar(p *[]string, name string, value []string, usage string) {
CommandLine.VarP(newStringSliceValue(value, p), name, "", usage)
}
Expand All @@ -130,9 +136,12 @@ func StringSliceVarP(p *[]string, name, shorthand string, value []string, usage
// The return value is the address of a []string variable that stores the value of the flag.
// Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
// For example:
// --ss="v1,v2" --ss="v3"
//
// --ss="v1,v2" --ss="v3"
//
// will result in
// []string{"v1", "v2", "v3"}
//
// []string{"v1", "v2", "v3"}
func (f *FlagSet) StringSlice(name string, value []string, usage string) *[]string {
p := []string{}
f.StringSliceVarP(&p, name, "", value, usage)
Expand All @@ -150,9 +159,12 @@ func (f *FlagSet) StringSliceP(name, shorthand string, value []string, usage str
// The return value is the address of a []string variable that stores the value of the flag.
// Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
// For example:
// --ss="v1,v2" --ss="v3"
//
// --ss="v1,v2" --ss="v3"
//
// will result in
// []string{"v1", "v2", "v3"}
//
// []string{"v1", "v2", "v3"}
func StringSlice(name string, value []string, usage string) *[]string {
return CommandLine.StringSliceP(name, "", value, usage)
}
Expand Down