/
main.go
117 lines (99 loc) · 2.09 KB
/
main.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Run a command line helper for various Fyne tools.
package main
import (
"flag"
"fmt"
"os"
"fyne.io/fyne"
"fyne.io/fyne/cmd/fyne/commands"
)
var commandList []idCommandPair
var provider commands.Command
type idCommandPair struct {
id string
provider commands.Command
}
func getCommand(id string) commands.Command {
for _, c := range commandList {
if c.id == id {
return c.provider
}
}
return nil
}
func help() {
printUsage()
os.Exit(2) // consistent with flag.Parse() with -help
}
func loadCommands() {
commandList = []idCommandPair{
{"bundle", commands.NewBundler()},
{"get", commands.NewGetter()},
{"env", &env{}},
{"package", commands.NewPackager()},
{"install", commands.NewInstaller()},
{"release", commands.NewReleaser()},
{"vendor", &vendor{}},
{"version", &version{}},
}
}
func main() {
loadCommands()
flag.Usage = printUsage
// first let's extract the first "command"
args := os.Args[1:]
if len(args) < 1 {
help()
}
command := args[0]
if command[0] == '-' { // there was a parameter instead of a command
help()
}
if command == "help" {
if len(args) >= 2 {
if provider = getCommand(args[1]); provider != nil {
provider.AddFlags()
}
}
help()
} else {
provider = getCommand(command)
if provider == nil {
fmt.Fprintln(os.Stderr, "Unsupported command", command)
return
}
provider.AddFlags()
// then parse the remaining args
err := flag.CommandLine.Parse(args[1:])
if err != nil {
fyne.LogError("Failed to parse flags", err)
return
}
provider.Run(flag.Args())
}
}
func printUsage() {
fmt.Println("Usage: fyne [command] [parameters], where command is one of:")
fmt.Print(" ")
i := 0
for _, c := range commandList {
fmt.Print(c.id)
if i < len(commandList)-1 {
fmt.Print(", ")
}
i++
}
fmt.Println(" or help")
fmt.Println()
if provider != nil {
provider.PrintHelp(" ")
} else {
for _, c := range commandList {
fmt.Printf(" %s\n", c.id)
c.provider.PrintHelp(" ")
fmt.Printf(" For more information run \"fyne help %s\"\n", c.id)
fmt.Println("")
}
}
flag.PrintDefaults()
}