func main() { flag.Usage = usage flag.Parse() log.SetFlags(0) args := flag.Args() if len(args) < 1 { usage() } if args[0] == "help" { help(args[1:]) return } for _, cmd := range commands { if cmd.Name() == args[0] && cmd.Run != nil { cmd.Flag.Usage = func() { cmd.Usage() } if cmd.CustomFlags { args = args[1:] } else { cmd.Flag.Parse(args[1:]) args = cmd.Flag.Args() } os.Exit(cmd.Run(cmd, args)) } } }
func help(args []string) { if len(args) == 0 { usage() return } if len(args) != 1 { fmt.Fprintf(os.Stdout, "usage: hg help command\n\nToo many arguments given.\n") os.Exit(0) } arg := args[0] for _, cmd := range commands { if cmd.Name() == arg { utils.Tmpl(os.Stdout, template.HelpTemplate, cmd) return } } fmt.Fprintf(os.Stdout, "Unknown help topic %#q. Run 'hg help'.\n", arg) os.Exit(0) }