flag

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package flag provides a command-line flag parser. Unlike the Go standard library flag package, it permits flags to be interspersed with arguments.

Example
package main

import (
	"fmt"

	"gg-scm.io/tool/internal/flag"
)

func main() {
	fset := new(flag.FlagSet)
	x := fset.Bool("x", false, "boolean flag")
	output := fset.String("o", "", "output path (string flag)")
	if err := fset.Parse([]string{"-o", "/path/to/foo", "input", "-x"}); err != nil {
		panic(err)
	}
	fmt.Println("x =", *x)
	fmt.Println("output =", *output)
	fmt.Println("args =", fset.Args())

}
Output:

x = true
output = /path/to/foo
args = [input]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsHelp

func IsHelp(e error) bool

IsHelp reports true if e indicates that the -help or -h is invoked but no such flag is defined.

Types

type FlagSet

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

A FlagSet represents a set of defined flags. The zero value of a FlagSet is an empty set of flags, allows non-flag arguments to be interspersed with flags, and has no usage information.

func NewFlagSet

func NewFlagSet(intersperse bool, usage, description string) *FlagSet

NewFlagSet returns a new, empty flag set with the specified intersperse behavior and usage information.

func (*FlagSet) Alias

func (f *FlagSet) Alias(name string, aliases ...string)

Alias adds aliases for an already defined flag.

func (*FlagSet) Arg

func (f *FlagSet) Arg(i int) string

Arg returns the i'th argument. Arg(0) is the first non-flag argument. Arg returns an empty string if the requested element does not exist.

func (*FlagSet) Args

func (f *FlagSet) Args() []string

Args returns the non-flag arguments.

func (*FlagSet) Bool

func (f *FlagSet) Bool(name string, value bool, usage string) *bool

Bool defines a bool flag with specified name, default value, and usage string. The return value is the address of a bool variable that stores the value of the flag.

func (*FlagSet) BoolVar

func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string)

BoolVar defines a bool flag with specified name, default value, and usage string. The argument p points to a bool variable in which to store the value of the flag.

func (*FlagSet) Help

func (f *FlagSet) Help(w io.Writer)

Help prints the help.

func (*FlagSet) Init

func (f *FlagSet) Init(intersperse bool, usage, description string)

Init sets the argument intersperse behavior and usage information for a flag set. By default, the zero FlagSet allows non-flag arguments to be interspersed with flags.

func (*FlagSet) Int

func (f *FlagSet) Int(name string, value int, usage string) *int

Int defines an int flag with specified name, default value, and usage string. The return value is the address of an int variable that stores the value of the flag.

func (*FlagSet) IntVar

func (f *FlagSet) IntVar(p *int, name string, value int, usage string)

IntVar defines an int flag with specified name, default value, and usage string. The argument p points to an int variable in which to store the value of the flag.

func (*FlagSet) MultiString

func (f *FlagSet) MultiString(name string, usage string) *[]string

MultiString defines a string flag with specified name and usage string. The return value is the address of a string slice variable that stores the value of each passed flag.

func (*FlagSet) MultiStringVar

func (f *FlagSet) MultiStringVar(p *[]string, name string, usage string)

MultiStringVar defines a string flag with specified name and usage string. The argument p points to a []string variable in which to store the value of each passed flag.

func (*FlagSet) NArg

func (f *FlagSet) NArg() int

NArg returns the number of non-flag arguments.

func (*FlagSet) Parse

func (f *FlagSet) Parse(arguments []string) error

Parse parses flag definitions from the argument list, which should not include the command name. Must be called after all flags in the FlagSet are defined and before flags are accessed by the program. The returned error can be tested with IsHelpUndefined if -help or -h were set but not defined.

func (*FlagSet) Regexp added in v1.3.0

func (f *FlagSet) Regexp(name string, usage string) **regexp.Regexp

Regexp defines a regular expression flag with specified name and usage string. If the flag is given multiple times, then the resulting regular expression is equivalent to joining all the arguments with "|". The return value is the address of a *regexp.Regexp variable that will be nil if no arguments are given.

func (*FlagSet) RegexpVar added in v1.3.0

func (f *FlagSet) RegexpVar(p **regexp.Regexp, name string, usage string)

RegexpVar defines a regular expression flag with specified name and usage string. If the flag is given multiple times, then the resulting regular expression is equivalent to joining all the arguments with "|". The argument p points to a *regexp.Regexp variable in which to store the resulting regular expression.

func (*FlagSet) String

func (f *FlagSet) String(name string, value string, usage string) *string

String defines a string flag with specified name, default value, and usage string. The return value is the address of a string variable that stores the value of the flag.

func (*FlagSet) StringVar

func (f *FlagSet) StringVar(p *string, name string, value string, usage string)

StringVar defines a string flag with specified name, default value, and usage string. The argument p points to a string variable in which to store the value of the flag.

func (*FlagSet) Var

func (f *FlagSet) Var(value Value, name string, usage string)

Var defines a flag with the specified name and usage string.

type Value

type Value interface {
	// String presents the current value as a string.
	String() string

	// Set is called once, in command line order, for each flag present.
	Set(string) error

	// Get returns the contents of the Value.
	Get() interface{}

	// If IsBoolFlag returns true, then the command-line parser makes
	// -name equivalent to -name=true rather than using the next
	// command-line argument.
	IsBoolFlag() bool
}

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