Files
opencloud/vendor/github.com/open-policy-agent/opa/v1/util/enumflag.go
dependabot[bot] e47f9d5fc9 Bump github.com/open-policy-agent/opa from 0.70.0 to 1.1.0
Bumps [github.com/open-policy-agent/opa](https://github.com/open-policy-agent/opa) from 0.70.0 to 1.1.0.
- [Release notes](https://github.com/open-policy-agent/opa/releases)
- [Changelog](https://github.com/open-policy-agent/opa/blob/main/CHANGELOG.md)
- [Commits](https://github.com/open-policy-agent/opa/compare/v0.70.0...v1.1.0)

---
updated-dependencies:
- dependency-name: github.com/open-policy-agent/opa
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-02-12 14:26:12 +00:00

60 lines
1.3 KiB
Go

// Copyright 2017 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package util
import (
"fmt"
"strings"
)
// EnumFlag implements the pflag.Value interface to provide enumerated command
// line parameter values.
type EnumFlag struct {
defaultValue string
vs []string
i int
}
// NewEnumFlag returns a new EnumFlag that has a defaultValue and vs enumerated
// values.
func NewEnumFlag(defaultValue string, vs []string) *EnumFlag {
f := &EnumFlag{
i: -1,
vs: vs,
defaultValue: defaultValue,
}
return f
}
// Type returns the valid enumeration values.
func (f *EnumFlag) Type() string {
return "{" + strings.Join(f.vs, ",") + "}"
}
// String returns the EnumValue's value as string.
func (f *EnumFlag) String() string {
if f.i == -1 {
return f.defaultValue
}
return f.vs[f.i]
}
// IsSet will return true if the EnumFlag has been set.
func (f *EnumFlag) IsSet() bool {
return f.i != -1
}
// Set sets the enum value. If s is not a valid enum value, an error is
// returned.
func (f *EnumFlag) Set(s string) error {
for i := range f.vs {
if f.vs[i] == s {
f.i = i
return nil
}
}
return fmt.Errorf("must be one of %v", f.Type())
}