示例#1
0
func ExampleURL() {
	var url flagutil.URL
	flag.Var(&url, "u", "URL")

	os.Args = append(os.Args, "-u", "https://google.com")
	flag.Parse()

	fmt.Println(url.URL.Scheme)
	fmt.Println(url.URL.Host)
	fmt.Println(url.String())
	// Output:
	// https
	// google.com
	// https://google.com
}
示例#2
0
func TestURL(t *testing.T) {
	var url flagutil.URL
	fs.Var(&url, "u", "URL")

	s := []string{"-u", "https://google.com"}

	err := fs.Parse(s)
	require.Nil(t, err)
	require.True(t, url.IsValid())
	require.Equal(t, "google.com", url.URL.Host)
	require.Equal(t, "https", url.URL.Scheme)
	require.Equal(t, "https://google.com", url.String())

	s = []string{"-u", "://foobar"}
	err = fs.Parse(s)
	require.NotNil(t, err)
}