Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Oct 17, 2021. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrun.go
More file actions
Latest commit
119 lines (98 loc) · 2.63 KB
/
Copy pathrun.go
File metadata and controls
119 lines (98 loc) · 2.63 KB
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
118
119
package cli
import (
"os"
"strings"
"github.com/spf13/pflag"
)
typealiasMapmap[Command][]string
funcappendParent(parentstring, addstring) string {
returnparent+add+" "
}
// splitArgs tries to split the args between the parent command's flags/args, and the subcommand's
// flags/args. If a subcommand is found, the parent args, subcommand args, and the subcommand will
// be returned. If a subcommand isn't found, the args will be returned as is, the subcommand args
// will be empty, and the subcommand will be nil.
funcsplitArgs(subCmds []Command, args []string, aliasesaliasMap) (cmdArgs, subArgs []string, subCmdCommand) {
fori, arg:=rangeargs {
ifstrings.HasPrefix(arg, "-") {
continue
}
for_, subCommand:=rangesubCmds {
spec:=subCommand.Spec()
ifspec.HasAliases() {
for_, alias:=rangespec.Aliases {
ifarg==alias {
returnargs[:i], args[i+1:], subCommand
}
}
}
ifspec.Name==arg {
returnargs[:i], args[i+1:], subCommand
}
}
}
returnargs, []string{}, nil
}
// Run sets up flags, helps, and executes the command with the provided
// arguments.
//
// parents is the list of parent commands.
// E.g the parent for `sail run hello` would be `sail run`.
//
// Use RunRoot if this package is managing the entire CLI.
funcRun(cmdCommand, args []string, parentstring) {
name:=parent+cmd.Spec().Name
fl:=pflag.NewFlagSet(name, pflag.ContinueOnError)
// Ensure pflag library doesn't print usage for us automatically,
// we'll override this below.
fl.Usage=func() {}
iffc, ok:=cmd.(FlaggedCommand); ok {
fc.RegisterFlags(fl)
}
ifcmd.Spec().RawArgs {
// Use `--` to return immediately when parsing the flags.
args=append([]string{"--"}, args...)
}
var (
cmdArgs, subArgs []string
subCmdCommand
)
pc, isParentCmd:=cmd.(ParentCommand)
ifisParentCmd {
aliases:=make(aliasMap)
subCmds:=pc.Subcommands()
for_, sc:=rangesubCmds {
spec:=sc.Spec()
ifspec.HasAliases() {
aliases[sc] =spec.Aliases
}
}
cmdArgs, subArgs, subCmd=splitArgs(subCmds, args, aliases)
ifsubCmd!=nil {
args=cmdArgs
}
}
err:=fl.Parse(args)
// Reassign the usage now that we've parsed the args
// so that we can render it manually.
fl.Usage=func() {
renderHelp(os.Stderr, name, cmd, fl)
}
iferr!=nil {
fl.Usage()
os.Exit(2)
}
// Route to subcommand.
ifisParentCmd&&subCmd!=nil {
Run(
subCmd, subArgs,
appendParent(parent, cmd.Spec().Name),
)
return
}
cmd.Run(fl)
}
// RunRoot calls Run with the process's arguments.
funcRunRoot(cmdCommand) {
Run(cmd, os.Args[1:], "")
}