当前位置: 首页>>代码示例>>Golang>>正文


Golang FlagSet.Usage方法代码示例

本文整理汇总了Golang中launchpad/net/gnuflag.FlagSet.Usage方法的典型用法代码示例。如果您正苦于以下问题:Golang FlagSet.Usage方法的具体用法?Golang FlagSet.Usage怎么用?Golang FlagSet.Usage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在launchpad/net/gnuflag.FlagSet的用法示例。


在下文中一共展示了FlagSet.Usage方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: parseArgs

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
}
开发者ID:nathj07,项目名称:http,代码行数:97,代码来源:http.go


注:本文中的launchpad/net/gnuflag.FlagSet.Usage方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。