Ejemplo n.º 1
0
// Help renders i's content, along with documentation for any
// flags defined in f. It calls f.SetOutput(ioutil.Discard).
func (i *Info) Help(f *gnuflag.FlagSet) []byte {
	buf := &bytes.Buffer{}
	fmt.Fprintf(buf, "usage: %s", i.Name)
	hasOptions := false
	f.VisitAll(func(f *gnuflag.Flag) { hasOptions = true })
	if hasOptions {
		fmt.Fprintf(buf, " [options]")
	}
	if i.Args != "" {
		fmt.Fprintf(buf, " %s", i.Args)
	}
	fmt.Fprintf(buf, "\n")
	if i.Purpose != "" {
		fmt.Fprintf(buf, "purpose: %s\n", i.Purpose)
	}
	if hasOptions {
		fmt.Fprintf(buf, "\noptions:\n")
		f.SetOutput(buf)
		f.PrintDefaults()
	}
	f.SetOutput(ioutil.Discard)
	if i.Doc != "" {
		fmt.Fprintf(buf, "\n%s\n", strings.TrimSpace(i.Doc))
	}
	if len(i.Aliases) > 0 {
		fmt.Fprintf(buf, "\naliases: %s\n", strings.Join(i.Aliases, ", "))
	}
	return buf.Bytes()
}
Ejemplo n.º 2
0
Archivo: http.go Proyecto: nathj07/http
func parseArgs(fset *flag.FlagSet, args []string) (*params, error) {
	var p params
	var printHeaders, noBody, noCookies bool
	fset.BoolVar(&p.json, "j", false, "serialize  data  items  as a JSON object")
	fset.BoolVar(&p.json, "json", false, "")

	fset.BoolVar(&p.form, "f", false, "serialize data items as form values")
	fset.BoolVar(&p.form, "form", false, "")

	fset.BoolVar(&printHeaders, "h", false, "print the response headers")
	fset.BoolVar(&printHeaders, "headers", false, "")

	fset.BoolVar(&noBody, "B", false, "do not print response body")
	fset.BoolVar(&noBody, "body", false, "")

	fset.BoolVar(&p.debug, "debug", false, "print debugging messages")

	fset.BoolVar(&p.noBrowser, "W", false, "do not open macaroon-login URLs in web browser")
	fset.BoolVar(&p.noBrowser, "no-browser", false, "")

	fset.BoolVar(&p.raw, "raw", false, "print response body without any JSON post-processing")

	fset.StringVar(&p.basicAuth, "a", "", "http basic auth (username:password)")
	fset.StringVar(&p.basicAuth, "auth", "", "")

	fset.BoolVar(&p.insecure, "insecure", false, "skip HTTPS certificate checking")

	fset.StringVar(&p.cookieFile, "cookiefile", filepath.Join(os.Getenv("HOME"), ".go-cookies"), "file to store persistent cookies in")

	fset.BoolVar(&noCookies, "C", false, "disable cookie storage")
	fset.BoolVar(&noCookies, "no-cookies", false, "")

	fset.BoolVar(&p.useStdin, "stdin", false, "read request body from standard input")

	// TODO --file (multipart upload)
	// TODO --timeout
	// TODO --proxy
	// TODO (??) --verify

	fset.Usage = func() {
		fmt.Fprint(os.Stderr, helpMessage)
		fset.PrintDefaults()
	}
	if err := fset.Parse(true, args); err != nil {
		return nil, err
	}
	if noCookies {
		p.cookieFile = ""
	}
	p.headers = printHeaders
	p.body = !noBody
	args = fset.Args()
	if len(args) == 0 {
		return nil, errUsage
	}
	if isMethod(args[0]) {
		p.method, args = strings.ToUpper(args[0]), args[1:]
		if len(args) == 0 {
			return nil, errUsage
		}
	}
	urlStr := args[0]
	if strings.HasPrefix(urlStr, ":") {
		// shorthand for localhost.
		if strings.HasPrefix(urlStr, ":/") {
			urlStr = "http://localhost" + urlStr[1:]
		} else {
			urlStr = "http://localhost" + urlStr
		}
	}
	if !strings.HasPrefix(urlStr, "http:") && !strings.HasPrefix(urlStr, "https:") {
		urlStr = "http://" + urlStr
	}
	u, err := url.Parse(urlStr)
	if err != nil {
		return nil, fmt.Errorf("invalid URL %q: %v", args[0], err)
	}
	if u.Host == "" {
		u.Host = "localhost"
	}
	p.url, args = u, args[1:]
	p.keyVals = make([]keyVal, len(args))
	for i, arg := range args {
		kv, err := parseKeyVal(arg)
		if err != nil {
			return nil, fmt.Errorf("cannot parse %q: %v", arg, err)
		}
		if kv.sep == "=" && p.method == "" {
			p.method = "POST"
		}
		p.keyVals[i] = kv
	}
	if p.method == "" {
		p.method = "GET"
	}
	return &p, nil
}