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


Golang FlagSet.SetOutput方法代碼示例

本文整理匯總了Golang中flag.FlagSet.SetOutput方法的典型用法代碼示例。如果您正苦於以下問題:Golang FlagSet.SetOutput方法的具體用法?Golang FlagSet.SetOutput怎麽用?Golang FlagSet.SetOutput使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在flag.FlagSet的用法示例。


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

示例1: Usage

// Usage returns a usage documentation of a command.
func (c Command) Usage(flags *flag.FlagSet) string {
	usage := fmt.Sprintf("Usage: %s", c.Long)

	if flags == nil {
		return usage
	}

	var hasFlag bool
	flags.VisitAll(func(_ *flag.Flag) {
		hasFlag = true
	})

	if hasFlag == false {
		return usage
	}

	buf := bytes.NewBufferString(usage)
	buf.WriteString("\n\nOptions:\n")

	defer flags.SetOutput(nil)
	flags.SetOutput(buf)

	flags.PrintDefaults()

	return buf.String()
}
開發者ID:yuuki,項目名稱:go-cli,代碼行數:27,代碼來源:cli.go

示例2: parseArgs

func (a *app) parseArgs(fs *flag.FlagSet, args []string) error {
	fs.SetOutput(a.out)
	fs.Usage = func() {
		fmt.Fprintf(a.out, help, args[0], args[0])
		fs.PrintDefaults()
	}

	fs.Var(&a.typeNames, "type",
		"A generated proto.Message type to generate stubs for (required, repeatable)")
	fs.StringVar(&a.outFile, "out", "proto_gae.gen.go",
		"The name of the output file")
	fs.StringVar(&a.header, "header", copyright, "Header text to put at the top of "+
		"the generated file. Defaults to the Chromium Authors copyright.")

	if err := fs.Parse(args[1:]); err != nil {
		return err
	}
	fail := errors.MultiError(nil)
	if a.typeNames.Data == nil || a.typeNames.Data.Len() == 0 {
		fail = append(fail, errors.New("must specify one or more -type"))
	}
	if !strings.HasSuffix(a.outFile, ".go") {
		fail = append(fail, errors.New("-output must end with '.go'"))
	}
	if len(fail) > 0 {
		for _, e := range fail {
			fmt.Fprintln(a.out, "error:", e)
		}
		fmt.Fprintln(a.out)
		fs.Usage()
		return fail
	}
	return nil
}
開發者ID:tetrafolium,項目名稱:gae,代碼行數:34,代碼來源:proto_gae.go

示例3: commandWorker

func commandWorker(wi *Instance, wr *wrangler.Wrangler, args []string, cell string, runFromCli bool) (Worker, error) {
	action := args[0]

	actionLowerCase := strings.ToLower(action)
	for _, group := range commands {
		for _, cmd := range group.Commands {
			if strings.ToLower(cmd.Name) == actionLowerCase {
				var subFlags *flag.FlagSet
				if runFromCli {
					subFlags = flag.NewFlagSet(action, flag.ExitOnError)
				} else {
					subFlags = flag.NewFlagSet(action, flag.ContinueOnError)
				}
				// The command may be run from an RPC and may not log to the console.
				// The Wrangler logger defines where the output has to go.
				subFlags.SetOutput(logutil.NewLoggerWriter(wr.Logger()))
				subFlags.Usage = func() {
					wr.Logger().Printf("Usage: %s %s %s\n\n", os.Args[0], cmd.Name, cmd.Params)
					wr.Logger().Printf("%s\n\n", cmd.Help)
					subFlags.PrintDefaults()
				}
				return cmd.Method(wi, wr, subFlags, args[1:])
			}
		}
	}
	if runFromCli {
		flag.Usage()
	} else {
		PrintAllCommands(wr.Logger())
	}
	return nil, fmt.Errorf("unknown command: %v", action)
}
開發者ID:jmptrader,項目名稱:vitess,代碼行數:32,代碼來源:command.go

示例4: runAddBeer

func runAddBeer(command string, flags *flag.FlagSet, id string, date string, size string, days string, years string, count string, cellar *BeerCellar) {
	if command == "add" {
		if id != "" {
			if date == "" {
				date = time.Now().Format("02/01/06")
			}

			log.Printf("HERE %v and %v\n", days, years)

			if days != "" {
				cellar.AddBeerByDays(id, date, size, days, count)
				cellar.PrintCellar(&StdOutPrint{})
			} else if years != "" {
				log.Printf("ADDING BY YEARS\n")
				cellar.AddBeerByYears(id, date, size, years, count)
				cellar.PrintCellar(&StdOutPrint{})
			} else {
				box := cellar.AddBeer(id, date, size)
				print := &StdOutPrint{}
				box.PrintCellar(print)
			}
		} else {
			flags.SetOutput(os.Stdout)
			flags.PrintDefaults()
		}
	}

	cellar.printDiff()
}
開發者ID:brotherlogic,項目名稱:beer,代碼行數:29,代碼來源:BeerCellar.go

示例5: initUsage

func initUsage(fs *flag.FlagSet) {
	fmt.Println("usage: git-bzr-bridge init [-h] <path>")
	fmt.Println("\nflags:")
	fs.SetOutput(os.Stdout)
	fs.PrintDefaults()

	fmt.Print(`
init will initialize a new repository at <path>
`)
}
開發者ID:usovalx,項目名稱:git-bzr-bridge,代碼行數:10,代碼來源:initCmd.go

示例6: updateHookUsage

func updateHookUsage(fs *flag.FlagSet) {
	fmt.Println("usage: git-bzr-bridge update-hook [-h] <ref name> <old obj> <new obj>")
	fmt.Println("\nflags:")
	fs.SetOutput(os.Stdout)
	fs.PrintDefaults()

	fmt.Print(`
update 
`)
}
開發者ID:usovalx,項目名稱:git-bzr-bridge,代碼行數:10,代碼來源:updateHookCmd.go

示例7: branchesUsage

func branchesUsage(fs *flag.FlagSet) {
	fmt.Println("usage: git-bzr-bridge branches [-h] [-v]")
	fmt.Println("\nflags:")
	fs.SetOutput(os.Stdout)
	fs.PrintDefaults()

	fmt.Print(`
branches will list all branches known to git-bzr-bridge. Default output
format will list git branch references one per line. If -v is specified
it will also show Bzr urls and names of the (hidden) bzr branches.
`)
}
開發者ID:usovalx,項目名稱:git-bzr-bridge,代碼行數:12,代碼來源:branchesCmd.go

示例8: parseArgs

// parseArgs parses cli arguments. This caters for parsing extra flags to caddy.
func parseArgs() (cliArgs, error) {
	args := cliArgs{conf: configFileName, source: "."}

	fs := flag.FlagSet{}
	fs.SetOutput(ioutil.Discard)
	h := false

	fs.StringVar(&args.conf, "c", args.conf, "")
	fs.StringVar(&args.conf, "conf", args.conf, "")
	fs.StringVar(&args.after, "a", args.after, "")
	fs.StringVar(&args.after, "after", args.after, "")
	fs.StringVar(&args.source, "s", args.source, "")
	fs.StringVar(&args.source, "source", args.source, "")
	fs.StringVar(&args.output, "o", args.output, "")
	fs.StringVar(&args.output, "output", args.output, "")
	fs.BoolVar(&args.update, "u", args.update, "")
	fs.BoolVar(&args.update, "update", args.update, "")
	fs.BoolVar(&h, "h", h, "")
	fs.BoolVar(&h, "help", h, "")

	err := fs.Parse(os.Args[1:])
	if h || err != nil {
		return args, fmt.Errorf(usage)
	}

	if fs.NArg() > 0 {
		args.directive = fs.Arg(0)
	}

	// extract caddy and go args
	if fs.NArg() > 1 {
		remArgs := fs.Args()[1:]
		for i, arg := range remArgs {
			if arg == "go" {
				if len(remArgs) > i+1 {
					args.goArgs = remArgs[i+1:]
				}
				break
			}
			args.caddyArgs = append(args.caddyArgs, arg)
		}
	}

	if err := addJSONConf(&args); err != nil {
		return args, err
	}

	if args.directive == "" {
		return args, usageError(fmt.Errorf("directive not set."))
	}
	return args, err
}
開發者ID:alimy,項目名稱:caddydev,代碼行數:53,代碼來源:main.go

示例9: PrintDefaults

func PrintDefaults(flag *Flag.FlagSet) {
	var into bytes.Buffer
	flag.SetOutput(&into)
	flag.PrintDefaults()
	outfrom := bufio.NewReader(&into)
	for {
		line, err := outfrom.ReadString('\n')
		if err != nil {
			break
		}
		if strings.HasSuffix(line, ": \x00\n") {
			continue
		}
		fmt.Fprint(os.Stderr, line)
	}
}
開發者ID:robertkrimen,項目名稱:dogbag,代碼行數:16,代碼來源:kilt.go

示例10: showUsage

func showUsage(fs *flag.FlagSet) {
	fmt.Println("usage: git-bzr-bridge [flags] <command> [<args>]")
	fmt.Println("\nflags:")
	fs.SetOutput(os.Stdout)
	fs.PrintDefaults()

	// get a list of all commands and sort them
	cmds := make([]string, 0, len(commands))
	for k := range commands {
		cmds = append(cmds, k)
	}
	sort.Strings(cmds)

	fmt.Println("\ncommands:")
	for _, k := range cmds {
		fmt.Printf("  %-12s  %s\n", k, commands[k].description)
	}

	fmt.Println("\nRun 'git-bzr-bridge <command> -h' to get usage message for <command>")
}
開發者ID:usovalx,項目名稱:git-bzr-bridge,代碼行數:20,代碼來源:main.go

示例11: flagSetPrintUsage

func flagSetPrintUsage(fl flag.FlagSet, w io.Writer, progname string) {
	fmt.Fprintf(w, "Usage of %s:\n", progname)
	fl.SetOutput(w)
	fl.PrintDefaults()
}
開發者ID:jeremy-w,項目名稱:ttrss-tool,代碼行數:5,代碼來源:ttrss-tool.go


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