-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
65 lines (52 loc) · 1.19 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"fmt"
"os"
"strings"
flag "github.com/bgentry/pflag"
)
type Command struct {
Run func(cmd *Command, args []string)
Flag flag.FlagSet
NeedsServer bool
Usage string // first word must be command name
Category string
Short string
Long string
}
func (c *Command) PrintUsage() {
if c.Runnable() {
fmt.Fprintf(os.Stderr, "Usage: mc %s\n", c.FullUsage())
}
fmt.Fprintf(os.Stderr, "Use 'mc help %s' for more information.\n", c.Name())
}
func (c *Command) PrintLongUsage() {
if c.Runnable() {
fmt.Fprintf(os.Stderr, "Usage: mc %s\n", c.FullUsage())
}
fmt.Println(strings.Trim(c.Long, "\n"))
}
func (c *Command) FullUsage() string {
return c.Usage
}
func (c *Command) Name() string {
name := c.Usage
i := strings.Index(name, " ")
if i >= 0 {
name = name[:i]
}
return name
}
func (c *Command) Runnable() bool {
return c.Run != nil
}
const extra = " (extra)"
func (c *Command) List() bool {
return c.Short != "" && !strings.HasSuffix(c.Short, extra)
}
func (c *Command) ListAsExtra() bool {
return c.Short != "" && strings.HasSuffix(c.Short, extra)
}
func (c *Command) ShortExtra() string {
return c.Short[:len(c.Short)-len(extra)]
}