Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created August 3, 2022 08:43
Show Gist options
  • Save podhmo/a8d10454bdc3960d8f15f0fa72696829 to your computer and use it in GitHub Desktop.
Save podhmo/a8d10454bdc3960d8f15f0fa72696829 to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"fmt"
"net"
"os"
)
func main() {
fs := flag.NewFlagSet("ExampleTextVar", flag.ContinueOnError)
fs.SetOutput(os.Stdout)
var ip net.IP
fs.TextVar(&ip, "ip", net.IPv4(192, 168, 0, 100), "`IP address` to parse")
fs.Parse([]string{"-ip", "127.0.0.1"})
fmt.Printf("{ip: %v}\n\n", ip)
// 256 is not a valid IPv4 component
ip = nil
fs.Parse([]string{"-ip", "256.0.0.1"})
fmt.Printf("{ip: %v}\n\n", ip)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment