1package kingpin
2
3import (
4	"fmt"
5	"net/http"
6	"strings"
7)
8
9type HTTPHeaderValue http.Header
10
11func (h *HTTPHeaderValue) Set(value string) error {
12	parts := strings.SplitN(value, ":", 2)
13	if len(parts) != 2 {
14		return fmt.Errorf("expected HEADER:VALUE got '%s'", value)
15	}
16	(*http.Header)(h).Add(parts[0], parts[1])
17	return nil
18}
19
20func (h *HTTPHeaderValue) Get() interface{} {
21	return (http.Header)(*h)
22}
23
24func (h *HTTPHeaderValue) String() string {
25	return ""
26}
27
28func HTTPHeader(s Settings) (target *http.Header) {
29	target = new(http.Header)
30	s.SetValue((*HTTPHeaderValue)(target))
31	return
32}
33
34// This example ilustrates how to define custom parsers. HTTPHeader
35// cumulatively parses each encountered --header flag into a http.Header struct.
36func ExampleValue() {
37	var (
38		curl    = New("curl", "transfer a URL")
39		headers = HTTPHeader(curl.Flag("headers", "Add HTTP headers to the request.").Short('H').PlaceHolder("HEADER:VALUE"))
40	)
41
42	curl.Parse([]string{"-H Content-Type:application/octet-stream"})
43	for key, value := range *headers {
44		fmt.Printf("%s = %s\n", key, value)
45	}
46}
47