Skip to content

Commit

Permalink
flag: roll back 156390043 (flag setting)
Browse files Browse the repository at this point in the history
Shell scripts depend on the old behavior too often.
It's too late to make this change.

LGTM=bradfitz
R=rsc, bradfitz
CC=golang-codereviews
https://golang.org/cl/161890044
  • Loading branch information
robpike committed Oct 20, 2014
1 parent c57cb78 commit 9070afb
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 89 deletions.
29 changes: 0 additions & 29 deletions src/flag/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,6 @@ func (d *durationValue) String() string { return (*time.Duration)(d).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.
//
// It is the implementer's responsibility to verify, if required, that the
// flag has been set only once.
type Value interface {
String() string
Set(string) error
Expand Down Expand Up @@ -273,7 +270,6 @@ type FlagSet struct {
parsed bool
actual map[string]*Flag
formal map[string]*Flag
set map[*Flag]bool
args []string // arguments after flags
errorHandling ErrorHandling
output io.Writer // nil means stderr; use out() accessor
Expand Down Expand Up @@ -721,25 +717,6 @@ func (f *FlagSet) usage() {
}
}

// alreadySet reports whether the flag has already been set during parsing.
// Because user-defined flags may legally be set multiple times, it only
// complains about flags defined in this package.
func (f *FlagSet) alreadySet(flag *Flag) bool {
if f.set == nil {
f.set = make(map[*Flag]bool)
}
switch flag.Value.(type) {
case *boolValue, *intValue, *int64Value, *uintValue, *uint64Value, *stringValue, *float64Value, *durationValue:
default:
return false // Not one of ours.
}
if f.set[flag] {
return true
}
f.set[flag] = true
return false
}

// parseOne parses one flag. It reports whether a flag was seen.
func (f *FlagSet) parseOne() (bool, error) {
if len(f.args) == 0 {
Expand Down Expand Up @@ -784,11 +761,6 @@ func (f *FlagSet) parseOne() (bool, error) {
return false, f.failf("flag provided but not defined: -%s", name)
}

// has it already been set?
if f.alreadySet(flag) {
return false, f.failf("flag set multiple times: -%s", name)
}

if fv, ok := flag.Value.(boolFlag); ok && fv.IsBoolFlag() { // special case: doesn't need an arg
if has_value {
if err := fv.Set(value); err != nil {
Expand Down Expand Up @@ -824,7 +796,6 @@ func (f *FlagSet) parseOne() (bool, error) {
// The return value will be ErrHelp if -help or -h were set but not defined.
func (f *FlagSet) Parse(arguments []string) error {
f.parsed = true
f.set = nil
f.args = arguments
for {
seen, err := f.parseOne()
Expand Down
60 changes: 0 additions & 60 deletions src/flag/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,63 +377,3 @@ func TestHelp(t *testing.T) {
t.Fatal("help was called; should not have been for defined help flag")
}
}

// Test that a standard flag can be set only once. Need to verify every flag type.
// User-defined flags can be set multiple times, and this is verified in the tests above.
func TestErrorOnMultipleSettings(t *testing.T) {
check := func(typ string, err error) {
if err == nil {
t.Errorf("%s flag can be set multiple times")
} else if !strings.Contains(err.Error(), "flag set multiple") {
t.Fatalf("expected multiple setting error, got %q", err)
}
}
{
var flags FlagSet
flags.Init("test", ContinueOnError)
flags.Bool("v", false, "usage")
check("bool", flags.Parse([]string{"-v", "-v"}))
}
{
var flags FlagSet
flags.Init("test", ContinueOnError)
flags.Int("v", 0, "usage")
check("int", flags.Parse([]string{"-v", "1", "-v", "2"}))
}
{
var flags FlagSet
flags.Init("test", ContinueOnError)
flags.Int64("v", 0, "usage")
check("int64", flags.Parse([]string{"-v", "1", "-v", "2"}))
}
{
var flags FlagSet
flags.Init("test", ContinueOnError)
flags.Uint("v", 0, "usage")
check("uint", flags.Parse([]string{"-v", "1", "-v", "2"}))
}
{
var flags FlagSet
flags.Init("test", ContinueOnError)
flags.Uint64("v", 0, "usage")
check("uint64", flags.Parse([]string{"-v", "1", "-v", "2"}))
}
{
var flags FlagSet
flags.Init("test", ContinueOnError)
flags.String("v", "", "usage")
check("string", flags.Parse([]string{"-v", "1", "-v", "2"}))
}
{
var flags FlagSet
flags.Init("test", ContinueOnError)
flags.Float64("v", 0, "usage")
check("float64", flags.Parse([]string{"-v", "1", "-v", "2"}))
}
{
var flags FlagSet
flags.Init("test", ContinueOnError)
flags.Duration("v", 0, "usage")
check("duration", flags.Parse([]string{"-v", "1s", "-v", "2s"}))
}
}

0 comments on commit 9070afb

Please sign in to comment.