|
| 1 | +package base |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +const CmdName = "leetcode" |
| 10 | + |
| 11 | +var Commands []*Command |
| 12 | + |
| 13 | +type Command struct { |
| 14 | + Run func(cmd *Command, args []string) |
| 15 | + UsageLine string |
| 16 | + Short string |
| 17 | + Long string |
| 18 | +} |
| 19 | + |
| 20 | +func (c *Command) Name() string { |
| 21 | + name := c.UsageLine |
| 22 | + if i := strings.Index(name, " "); i > 0 { |
| 23 | + name = name[0 : i+1] |
| 24 | + } |
| 25 | + return name |
| 26 | +} |
| 27 | + |
| 28 | +func (c *Command) Usage() { |
| 29 | + fmt.Printf("usage: %s %s\n", CmdName, c.UsageLine) |
| 30 | + fmt.Printf("Run '%s help %s' for details.\n", CmdName, c.Name()) |
| 31 | + os.Exit(0) |
| 32 | +} |
| 33 | + |
| 34 | +func Usage() { |
| 35 | + fmt.Printf("%s is a tool for managing leetcode source code.\n\n", CmdName) |
| 36 | + fmt.Println("Usage:") |
| 37 | + fmt.Printf("\t%s <command> [arguments]\n", CmdName) |
| 38 | + fmt.Println("The commands are:") |
| 39 | + for _, cmd := range Commands { |
| 40 | + fmt.Printf("\t%s \t%s\n", cmd.Name(), cmd.Short) |
| 41 | + } |
| 42 | + fmt.Println() |
| 43 | + fmt.Printf(`Use "%s help <command>" for more information about a command.`, CmdName) |
| 44 | + fmt.Println() |
| 45 | +} |
0 commit comments