flag

package
v0.0.0-...-110cdec Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2024 License: Apache-2.0 Imports: 14 Imported by: 45

Documentation

Overview

Package flag provides the utilities for implementing command line flags.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolSlice

func BoolSlice(b *[]bool) flag.Getter

BoolSlice returns a flag.Getter which reads flags into the given []bool pointer.

func CommaList

func CommaList(s *[]string) flag.Getter

CommaList returns a flag.Getter for parsing a comma separated flag argument into a string slice.

func Date

func Date(t *time.Time) flag.Getter

Date returns a flag.Getter which parses the flag value as a UTC date. The string must be a time with layout "2006-01-24".

func FieldSlice

func FieldSlice(f *[]googleapi.Field) flag.Getter

FieldSlice returns a flag.Getter which reads flags into the given []googleapi.Field pointer.

func GRPCMetadata

func GRPCMetadata(md metadata.MD) flag.Getter

GRPCMetadata returns a flag.Getter for parsing gRPC metadata from a a set of colon-separated strings. Example: -f a:1 -f a:2 -f b:3 Panics if md is nil.

func Int32

func Int32(i *int32) flag.Getter

Int32 returns a flag.Getter which reads flags into the given int32 pointer.

func Int64Slice

func Int64Slice(i *[]int64) flag.Getter

Int64Slice returns a flag.Getter which reads flags into the given []int64 pointer.

func JSONMap

func JSONMap(m *map[string]string) flag.Value

JSONMap returns a flag.Value that can be passed to flag.Var to parse a JSON string into a map.

func MessageSliceFlag

func MessageSliceFlag(msgSlicePtr any) flag.Getter

MessageSliceFlag returns a new flag for a slice of pointer to concrete proto message struct which implements the proto.Message interface. Expect input to be of type *[]*B where B is concrete struct of proto message like pb.Build. A flag value must be a JSON string of the provided concrete proto message.

Example
var builds []*pb.Build
fs := flag.NewFlagSet("test", flag.ContinueOnError)
flag := MessageSliceFlag(&builds)
fs.Var(flag, "build", "Test Proto Message")
fs.Parse([]string{"-build",
	"{\"id\": 111111, \"status\": \"SUCCESS\"}"})
marshaler := &jsonpb.Marshaler{Indent: "  "}
jsonMsg, _ := marshaler.MarshalToString(builds[0])
fmt.Println(jsonMsg)
Output:

{
  "id": "111111",
  "status": "SUCCESS"
}

func StringMap

func StringMap(m map[string]string) flag.Getter

StringMap returns a flag.Getter for parsing map[string]string from a a set of colon-separated strings. Example:

-f a:1 -f b:3

The flag.Getter.Set implementation returns an error if the key is already in the map. Panics if m is nil.

func StringPairs

func StringPairs(m strpair.Map) flag.Getter

StringPairs returns a flag.Getter for parsing strpair.Map from a a set of colon-separated strings. Example:

"-f a:1"
"-f a:2"
"-f b:3:3" (The value contains a colon ":")

Panics if m is nil.

func StringSlice

func StringSlice(s *[]string) flag.Getter

StringSlice returns a flag.Getter which reads flags into the given []string pointer.

func Time

func Time(t *time.Time) flag.Getter

Time returns a flag.Getter which parses a string into a time.Time pointer.

The timestamp to parse must be formatted as stiptime: https://chromium.googlesource.com/infra/infra/infra_libs/+/b2e2c9948c327b88b138d8bd60ec4bb3a957be78/time_functions/README.md

Caveat: Leap seconds are not supported due to a limitation in go's time library: https://github.com/golang/go/issues/15247

func TimeLayout

func TimeLayout(t *time.Time, layout string) flag.Getter

TimeLayout returns a flag.Getter which parses the flag value as a UTC time, based on the given layout.

Types

type Choice

type Choice struct {
	// contains filtered or unexported fields
}

Choice is an implementation of flag.Value for parsing a multiple-choice string.

func NewChoice

func NewChoice(output *string, choices ...string) Choice

NewChoice creates a Choice value

func (Choice) Set

func (f Choice) Set(s string) error

Set implements the flag.Value interface.

func (Choice) String

func (f Choice) String() string

String implements the flag.Value interface.

type RelativeTime

type RelativeTime struct {
	T *time.Time
	// contains filtered or unexported fields
}

RelativeTime is an implementation of flag.Value for parsing a time by a relative day offset.

func (RelativeTime) Set

func (f RelativeTime) Set(s string) error

Set implements the flag.Value interface.

func (RelativeTime) String

func (f RelativeTime) String() string

String implements the flag.Value interface.

Directories

Path Synopsis
Package flagenum is a utility package which facilitates implementation of flag.Value, json.Marshaler, and json.Unmarshaler interfaces via a string-to- value mapping.
Package flagenum is a utility package which facilitates implementation of flag.Value, json.Marshaler, and json.Unmarshaler interfaces via a string-to- value mapping.
Package multiflag is a package providing a flag.Value implementation capable of switching between multiple registered sub-flags, each of which have their own set of parameter flags.
Package multiflag is a package providing a flag.Value implementation capable of switching between multiple registered sub-flags, each of which have their own set of parameter flags.
Package stringlistflag provides a flag.Value implementation which resolves multiple args into a []string.
Package stringlistflag provides a flag.Value implementation which resolves multiple args into a []string.
Package stringmapflag provides a flag.Value that, when parsed, augments a map[string]string with the supplied parameter.
Package stringmapflag provides a flag.Value that, when parsed, augments a map[string]string with the supplied parameter.
Package stringsetflag provides a flag.Value implementation which resolves multiple args into a stringset.
Package stringsetflag provides a flag.Value implementation which resolves multiple args into a stringset.

Jump to

Keyboard shortcuts

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