Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on May 21, 2026. It is now read-only.
forked from urfave/cli
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.go
More file actions
Latest commit
136 lines (116 loc) · 3 KB
/
Copy pathcommand.go
File metadata and controls
136 lines (116 loc) · 3 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package cli
import (
"fmt"
"io/ioutil"
"strings"
)
// Command is a subcommand for a cli.App.
typeCommandstruct {
// The name of the command
Namestring
// short name of the command. Typically one character
ShortNamestring
// A short description of the usage of this command
Usagestring
// A longer explanation of how the command works
Descriptionstring
// The function to call when checking for bash command completions
BashCompletefunc(context*Context)
// An action to execute before any sub-subcommands are run, but after the context is ready
// If a non-nil error is returned, no sub-subcommands are run
Beforefunc(context*Context) error
// The function to call when this command is invoked
Actionfunc(context*Context)
// List of child commands
Subcommands []Command
// List of flags to parse
Flags []Flag
// Treat all flags as normal arguments if true
SkipFlagParsingbool
}
// Invokes the command given the context, parses ctx.Args() to generate command-specific flags
func (cCommand) Run(ctx*Context) error {
iflen(c.Subcommands) >0||c.Before!=nil {
returnc.startApp(ctx)
}
// append help to flags
c.Flags=append(
c.Flags,
HelpFlag,
)
ifctx.App.EnableBashCompletion {
c.Flags=append(c.Flags, BashCompletionFlag)
}
set:=flagSet(c.Name, c.Flags)
set.SetOutput(ioutil.Discard)
firstFlagIndex:=-1
forindex, arg:=rangectx.Args() {
ifstrings.HasPrefix(arg, "-") {
firstFlagIndex=index
break
}
}
varerrerror
iffirstFlagIndex>-1&&!c.SkipFlagParsing {
args:=ctx.Args()
regularArgs:=args[1:firstFlagIndex]
flagArgs:=args[firstFlagIndex:]
err=set.Parse(append(flagArgs, regularArgs...))
} else {
err=set.Parse(ctx.Args().Tail())
}
iferr!=nil {
fmt.Printf("Incorrect Usage.\n\n")
ShowCommandHelp(ctx, c.Name)
fmt.Println("")
returnerr
}
nerr:=normalizeFlags(c.Flags, set)
ifnerr!=nil {
fmt.Println(nerr)
fmt.Println("")
ShowCommandHelp(ctx, c.Name)
fmt.Println("")
returnnerr
}
context:=NewContext(ctx.App, set, ctx.globalSet)
ifcheckCommandCompletions(context, c.Name) {
returnnil
}
ifcheckCommandHelp(context, c.Name) {
returnnil
}
context.Command=c
c.Action(context)
returnnil
}
// Returns true if Command.Name or Command.ShortName matches given name
func (cCommand) HasName(namestring) bool {
returnc.Name==name||c.ShortName==name
}
func (cCommand) startApp(ctx*Context) error {
app:=NewApp()
// set the name and usage
app.Name=fmt.Sprintf("%s %s", ctx.App.Name, c.Name)
ifc.Description!="" {
app.Usage=c.Description
} else {
app.Usage=c.Usage
}
// set the flags and commands
app.Commands=c.Subcommands
app.Flags=c.Flags
// bash completion
app.EnableBashCompletion=ctx.App.EnableBashCompletion
ifc.BashComplete!=nil {
app.BashComplete=c.BashComplete
}
// set the actions
app.Before=c.Before
ifc.Action!=nil {
app.Action=c.Action
} else {
app.Action=helpSubcommand.Action
}
returnapp.RunAsSubcommand(ctx)
}