FAQ
Reviewers: golang-dev_googlegroups.com,

Message:
Hello golang-dev@googlegroups.com,

I'd like you to review this change to
https://code.google.com/p/go/


Description:
flag: add implicit boolFlag interface

Any flag.Value that has an IsBoolFlag method that returns true
will be treated as a bool flag type during parsing.

Fixes issue 4262.

Please review this at https://codereview.appspot.com/6944064/

Affected files:
M src/pkg/flag/flag.go
M src/pkg/flag/flag_test.go


Index: src/pkg/flag/flag.go
===================================================================
--- a/src/pkg/flag/flag.go
+++ b/src/pkg/flag/flag.go
@@ -91,6 +91,15 @@

func (b *boolValue) String() string { return fmt.Sprintf("%v", *b) }

+func (b *boolValue) IsBoolFlag() bool { return true }
+
+// optional interface to indicate boolean flags that can be
+// supplied without "=value" text
+type boolFlag interface {
+ Value
+ IsBoolFlag() bool
+}
+
// -- int Value
type intValue int

@@ -204,6 +213,10 @@

// Value is the interface to the dynamic value stored in a flag.
// (The default value is represented as a string.)
+//
+// If a Value has an IsBoolFlag() bool method returning true,
+// the command-line parser makes -name equivalent to -name=true
+// rather than using the next command-line argument.
type Value interface {
String() string
Set(string) error
@@ -704,7 +717,7 @@
}
return false, f.failf("flag provided but not defined: -%s", name)
}
- if fv, ok := flag.Value.(*boolValue); ok { // special case: doesn't need
an arg
+ if fv, ok := flag.Value.(boolFlag); ok &&
flag.Value.(boolFlag).IsBoolFlag() { // special case: doesn't need an arg
if has_value {
if err := fv.Set(value); err != nil {
return false, f.failf("invalid boolean value %q for -%s: %v", value,
name, err)
Index: src/pkg/flag/flag_test.go
===================================================================
--- a/src/pkg/flag/flag_test.go
+++ b/src/pkg/flag/flag_test.go
@@ -191,12 +191,34 @@
return nil
}

+// Declare a user-defined boolean flag type.
+type boolFlagVar struct {
+ count int
+}
+
+func (b *boolFlagVar) String() string {
+ return fmt.Sprintf("%d", b.count)
+}
+
+func (b *boolFlagVar) Set(value string) error {
+ if value == "true" {
+ b.count++
+ }
+ return nil
+}
+
+func (b *boolFlagVar) IsBoolFlag() bool {
+ return b.count < 4
+}
+
func TestUserDefined(t *testing.T) {
var flags FlagSet
flags.Init("test", ContinueOnError)
var v flagVar
+ var b boolFlagVar
flags.Var(&v, "v", "usage")
- if err := flags.Parse([]string{"-v", "1", "-v", "2", "-v=3"}); err != nil
{
+ flags.Var(&b, "b", "usage")
+ if err :=
flags.Parse([]string{"-v", "1", "-b", "-v", "2", "-v=3", "-b", "-b", "-b=true", "-b=false", "-b", "-b"});
err != nil && b.count < 4 {
t.Error(err)
}
if len(v) != 3 {
@@ -206,6 +228,10 @@
if v.String() != expect {
t.Errorf("expected value %q got %q", expect, v.String())
}
+
+ if b.count != 4 {
+ t.Errorf("want: %d; got: %d", 4, b.count)
+ }
}

func TestSetOutput(t *testing.T) {

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-dev @
categoriesgo
postedDec 18, '12 at 1:11a
activeDec 22, '12 at 6:34p
posts9
users3
websitegolang.org

3 users in discussion

Rickarnoldjr: 4 posts Rsc: 3 posts Bradfitz: 2 posts

People

Translate

site design / logo © 2022 Grokbase