Standard library / flag

flag.FlagSet.TextVar()

Binds a type implementing encoding.TextUnmarshaler, with a compatible default implementing TextMarshaler. Run with arguments: ["-ip=192.0.2.7"]. Expected stdout: '192.0.2.7\n'.

Formal Syntax & Structure

go syntaxgo
func (f *FlagSet) TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, usage string)

Parameters & Attributes

NameTypeDescription
pencoding.TextUnmarshalerPointer to storage updated during parsing.
namestringName of the flag or flag set.
valueencoding.TextMarshalerDefault value for registration, or text to parse for Set.
usagestringHelp text describing the flag.

Working Example

Example Snippetgo
package main
import (  "flag"  "fmt"  "net/netip"  "os")
func check(e error) {  if e != nil {    panic(e)  }}
func main() {  f := flag.NewFlagSet("demo", flag.ContinueOnError)  var ip netip.Addr  f.TextVar(&ip, "ip", netip.MustParseAddr("192.0.2.1"), "address")  check(f.Parse(os.Args[1:]))  fmt.Println(ip)}
Go Reference