示例#1
0
文件: cli.go 项目: yuuki/go-cli
// 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()
}
示例#2
0
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
}
示例#3
0
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)
}
示例#4
0
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()
}
示例#5
0
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>
`)
}
示例#6
0
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 
`)
}
示例#7
0
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.
`)
}
示例#8
0
文件: main.go 项目: alimy/caddydev
// 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
}
示例#9
0
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)
	}
}
示例#10
0
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>")
}
示例#11
0
func flagSetPrintUsage(fl flag.FlagSet, w io.Writer, progname string) {
	fmt.Fprintf(w, "Usage of %s:\n", progname)
	fl.SetOutput(w)
	fl.PrintDefaults()
}