flagvalue

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 3, 2024 License: MPL-2.0 Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Counter

func Counter(val int, p *int) *counterValue

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

func Duration(val time.Duration, p *time.Duration) *durationValue
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
}
Output:

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!")
}
Output:

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),
	})
}
Output:

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",
	})
}
Output:

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
}
Output:

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
}
Output:

Types

type SimpleValue

type SimpleValue interface {
	constraints.Float | constraints.Integer | ~string | ~bool |
		*string | *bool
}

type Value

type Value = pflag.Value

Value is the interface to the dynamic value stored in a flag.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL