Documentation
¶
Index ¶
- func Counter(val int, p *int) *counterValue
- func Duration(val time.Duration, p *time.Duration) *durationValue
- func Enum[T comparable](allowed []T, val T, p *T) *enumValue[T]
- func Simple[T SimpleValue](val T, p *T) *simpleValue[T]
- func SimpleMap[K, V SimpleValue](val map[K]V, p *map[K]V) *simpleMapValue[K, V]
- func SimpleSlice[T SimpleValue](val []T, p *[]T) *simpleSliceValue[T]
- type SimpleValue
- type Value
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Counter ¶
Simple returns a pflags.Value that sets the value at p with the default val or the value provided via a flag.
If the type of the value is a boolean, set the NoOptDefVal to "true", on the Flag. Otherwise the flag will have to have a value set to be parsed. As an example if the boolean flag had the name "force" and NoOptDefVal is not set, the flag will have to be set as --force=true.
func Duration ¶
Example ¶
package main
import (
"time"
"github.com/hashicorp/hcp/internal/pkg/flagvalue"
"github.com/spf13/pflag"
)
func main() {
var sleep time.Duration
f := pflag.NewFlagSet("example", pflag.ContinueOnError)
f.AddFlag(&pflag.Flag{
Name: "wait",
Usage: "wait specifies the time to sleep before taking an action",
DefValue: "5s",
Value: flagvalue.Duration(5*time.Second, &sleep),
})
time.Sleep(sleep)
// ... Take an action
}
func Enum ¶
func Enum[T comparable](allowed []T, val T, p *T) *enumValue[T]
Enum returns a pflags.Value that sets the value at p with the default val or the value provided via a flag. The provided value must be in the allowed list or an error is returned.
Example ¶
package main
import (
"github.com/hashicorp/hcp/internal/pkg/flagvalue"
"github.com/spf13/pflag"
)
func main() {
var logLevel string
f := pflag.NewFlagSet("example", pflag.ContinueOnError)
f.AddFlag(&pflag.Flag{
Name: "log-level",
Usage: "log-level specifies the verbosity to log with.",
DefValue: "warn",
Value: flagvalue.Enum([]string{"trace", "debug", "info", "warn", "error"}, "warn", &logLevel),
})
// Setup logger
// logger := hclog.Default().SetLevel(hclog.LevelFromString(logLevel))
// logger.Warn("we are using flags!")
}
func Simple ¶
func Simple[T SimpleValue](val T, p *T) *simpleValue[T]
Simple returns a pflags.Value that sets the value at p with the default val or the value provided via a flag.
If the type of the value is a boolean, set the NoOptDefVal to "true", on the Flag. Otherwise the flag will have to have a value set to be parsed. As an example if the boolean flag had the name "force" and NoOptDefVal is not set, the flag will have to be set as --force=true.
Example ¶
package main
import (
"github.com/hashicorp/hcp/internal/pkg/flagvalue"
"github.com/spf13/pflag"
)
func main() {
var projectID string
f := pflag.NewFlagSet("example", pflag.ContinueOnError)
f.AddFlag(&pflag.Flag{
Name: "project",
Usage: "project specifies the HCP Project ID to use.",
Value: flagvalue.Simple[string]("", &projectID),
})
}
Example (Boolean) ¶
package main
import (
"github.com/hashicorp/hcp/internal/pkg/flagvalue"
"github.com/spf13/pflag"
)
func main() {
var force bool
f := pflag.NewFlagSet("example", pflag.ContinueOnError)
f.AddFlag(&pflag.Flag{
Name: "force",
Shorthand: "f",
Usage: "force force deletes without confirmation.",
Value: flagvalue.Simple[bool](false, &force),
// Critical to set for boolean values. Otherwise -f, --force will not
// set force to true. Instead the flag parsing will error expecting a
// value to be set for the flag.
NoOptDefVal: "true",
})
}
func SimpleMap ¶
func SimpleMap[K, V SimpleValue](val map[K]V, p *map[K]V) *simpleMapValue[K, V]
SimpleMap returns a pflags.Value that sets the map at p with the default val or the value(s) provided via a flag.
Example ¶
package main
import (
"github.com/hashicorp/hcp/internal/pkg/flagvalue"
"github.com/spf13/pflag"
)
func main() {
var headers map[string]string
f := pflag.NewFlagSet("example", pflag.ContinueOnError)
f.AddFlag(&pflag.Flag{
Name: "headers",
Usage: "headers is a set of headers to send with the request. May be specified multiple times in the form of KEY=VALUE.",
Value: flagvalue.SimpleMap(nil, &headers),
})
// Make the request
}
func SimpleSlice ¶
func SimpleSlice[T SimpleValue](val []T, p *[]T) *simpleSliceValue[T]
SimpleSlice returns a pflags.Value that sets the slice at p with the default val or the value(s) provided via a flag.
Example ¶
package main
import (
"github.com/hashicorp/hcp/internal/pkg/flagvalue"
"github.com/spf13/pflag"
)
func main() {
var secrets []string
f := pflag.NewFlagSet("example", pflag.ContinueOnError)
f.AddFlag(&pflag.Flag{
Name: "secret",
Usage: "secret is a secret to read. Multiple values may be specified.",
Value: flagvalue.SimpleSlice[string]([]string{}, &secrets),
})
// Fetch the secrets
}
Types ¶
type SimpleValue ¶
type SimpleValue interface {
constraints.Float | constraints.Integer | ~string | ~bool |
*string | *bool
}