-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommand.go
86 lines (74 loc) · 1.87 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"flag"
"fmt"
"log"
"os"
"runtime"
"strings"
"github.com/TuftsBCB/tools/util"
)
var (
flagCpuProfile = ""
flagCpu = runtime.NumCPU()
flagOverwrite = false
)
func init() {
log.SetFlags(0)
}
type command struct {
name string
positionalUsage string
shortHelp string
help string
flags *flag.FlagSet
addFlags func(*command)
run func(*command)
}
func (c *command) showUsage() {
log.Printf("Usage: flib %s [flags] %s\n", c.name, c.positionalUsage)
c.showFlags()
os.Exit(1)
}
func (c *command) showHelp() {
log.Printf("Usage: flib %s [flags] %s\n\n", c.name, c.positionalUsage)
log.Println(strings.TrimSpace(c.help))
log.Printf("\nThe flags are:\n\n")
c.showFlags()
log.Println("")
os.Exit(1)
}
func (c *command) showFlags() {
c.flags.VisitAll(func(fl *flag.Flag) {
var def string
if len(fl.DefValue) > 0 {
def = fmt.Sprintf(" (default: %s)", fl.DefValue)
}
usage := strings.Replace(fl.Usage, "\n", "\n ", -1)
log.Printf("-%s%s\n", fl.Name, def)
log.Printf(" %s\n", usage)
})
}
func (c *command) setCommonFlags() {
c.flags.StringVar(&flagCpuProfile, "cpu-prof", flagCpuProfile,
"When set, a CPU profile will be written to the file path provided.")
c.flags.IntVar(&flagCpu, "cpu", flagCpu,
"Sets the maximum number of CPUs that can be executing simultaneously.")
c.flags.BoolVar(&util.FlagQuiet, "quiet", util.FlagQuiet,
"When set, progress information and other status messages will\n"+
"not be printed to stderr.")
}
func (c *command) setOverwriteFlag() {
c.flags.BoolVar(&flagOverwrite, "overwrite", flagOverwrite,
"When set, the output file will be overwritten if it already exists.")
}
func (c *command) assertNArg(n int) {
if c.flags.NArg() != n {
c.showUsage()
}
}
func (c *command) assertLeastNArg(n int) {
if c.flags.NArg() < n {
c.showUsage()
}
}