當前位置: 首頁>>代碼示例>>Golang>>正文


Golang mflag.NewFlagSet函數代碼示例

本文整理匯總了Golang中github.com/dotcloud/docker/pkg/mflag.NewFlagSet函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewFlagSet函數的具體用法?Golang NewFlagSet怎麽用?Golang NewFlagSet使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewFlagSet函數的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Subcmd

func (cli *DockerCli) Subcmd(name, signature, description string) *flag.FlagSet {
	flags := flag.NewFlagSet(name, flag.ContinueOnError)
	flags.Usage = func() {
		fmt.Fprintf(cli.err, "\nUsage: docker %s %s\n\n%s\n\n", name, signature, description)
		flags.PrintDefaults()
		os.Exit(2)
	}
	return flags
}
開發者ID:ChaosCloud,項目名稱:docker,代碼行數:9,代碼來源:cli.go

示例2: Run

// Run invokes the CLI with the given arguments.
func (cli *CLI) Run(args []string) int {
	var githubAPIOpts GitHubAPIOpts
	var ghrOpts GhrOpts
	var stat bool
	var err error

	flags := flag.NewFlagSet(Name, flag.ContinueOnError)
	flags.SetOutput(cli.errStream)
	flags.Usage = func() {
		fmt.Fprint(cli.errStream, helpText)
	}

	// Options for GitHub API.
	flags.StringVar(&githubAPIOpts.OwnerName, []string{"u", "-username"}, "", "")
	flags.StringVar(&githubAPIOpts.RepoName, []string{"r", "-repository"}, "", "")
	flags.StringVar(&githubAPIOpts.Token, []string{"t", "-token"}, "", "")
	flags.StringVar(&githubAPIOpts.TargetCommitish, []string{"c", "-commitish"}, "", "")
	flags.BoolVar(&githubAPIOpts.Draft, []string{"-draft"}, false, "")
	flags.BoolVar(&githubAPIOpts.Prerelease, []string{"-prerelease"}, false, "")

	// Options to change ghr work.
	flags.IntVar(&ghrOpts.Parallel, []string{"p", "-parallel"}, -1, "")
	flags.BoolVar(&ghrOpts.Replace, []string{"-replace"}, false, "")
	flags.BoolVar(&ghrOpts.Delete, []string{"-delete"}, false, "")
	flags.BoolVar(&stat, []string{"-stat"}, false, "")

	// General options
	version := flags.Bool([]string{"v", "-version"}, false, "")
	debug := flags.Bool([]string{"-debug"}, false, "")

	// Parse all the flags
	if err := flags.Parse(args[1:]); err != nil {
		return ExitCodeParseFlagsError
	}

	// Show version. It also try to fetch latest version information from github
	if *version {
		fmt.Fprintf(cli.errStream, "ghr version %s, build %s \n", Version, GitCommit)

		select {
		case res := <-verCheckCh:
			if res != nil && res.Outdated {
				msg := fmt.Sprintf("Latest version of ghr is %s, please update it\n", res.Current)
				fmt.Fprint(cli.errStream, ColoredError(msg))
			}
		case <-time.After(CheckTimeout):
			// do nothing
		}

		return ExitCodeOK
	}

	// Run as DEBUG mode
	if *debug {
		os.Setenv("GHR_DEBUG", "1")
	}

	// Set BaseURL
	_ = setBaseURL(&githubAPIOpts)

	// Set Token
	err = setToken(&githubAPIOpts)
	if err != nil {
		errMsg := fmt.Sprintf("Could not retrieve GitHub API Token.\n" +
			"Please set your Github API Token in the GITHUB_TOKEN env var.\n" +
			"Or set one via `-t` option.\n" +
			"See about GitHub API Token on https://github.com/blog/1509-personal-api-tokens\n",
		)
		fmt.Fprint(cli.errStream, ColoredError(errMsg))
		return ExitCodeTokenNotFound
	}

	// Set repository owner name.
	err = setOwner(&githubAPIOpts)
	if err != nil {
		errMsg := fmt.Sprintf("Could not retrieve repository user name: %s\n"+
			"ghr try to retrieve git user name from `~/.gitcofig` file.\n"+
			"Please set one via -u option or `~/.gitconfig` file.\n",
			err)
		fmt.Fprintf(cli.errStream, ColoredError(errMsg))
		return ExitCodeOwnerNotFound
	}

	// Set repository owner name.
	err = setRepo(&githubAPIOpts)
	if err != nil {
		errMsg := fmt.Sprintf("Could not retrieve repository name: %s\n"+
			"ghr try to retrieve github repository name from `.git/config` file.\n"+
			"Please be sure you're in github repository. Or set one via `-r` options.\n",
			err)
		fmt.Fprintf(cli.errStream, ColoredError(errMsg))
		return ExitCodeRepoNotFound
	}

	// Display statical information.
	if stat {
		err = ShowStat(cli.outStream, &githubAPIOpts)
		if err != nil {
			fmt.Fprintf(cli.errStream, ColoredError(err.Error()))
//.........這裏部分代碼省略.........
開發者ID:djui,項目名稱:ghr,代碼行數:101,代碼來源:cli.go

示例3: Parse

//FIXME Only used in tests
func Parse(args []string, sysInfo *sysinfo.SysInfo) (*Config, *HostConfig, *flag.FlagSet, error) {
	cmd := flag.NewFlagSet("run", flag.ContinueOnError)
	cmd.SetOutput(ioutil.Discard)
	cmd.Usage = nil
	return parseRun(cmd, args, sysInfo)
}
開發者ID:BMC-Phurnace-DevOps01,項目名稱:docker,代碼行數:7,代碼來源:parse.go


注:本文中的github.com/dotcloud/docker/pkg/mflag.NewFlagSet函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。