-
Notifications
You must be signed in to change notification settings - Fork 16
/
command.go
139 lines (117 loc) · 3.81 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
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
137
138
139
package climax
// CmdHandler is a handling function type for functions.
//
// Returned integer would be used as application exit status.
type CmdHandler func(Context) int
// Command represents a top-level application subcommand.
type Command struct {
// Name is a [A-Za-z_0-9] identifier of up to 11 characters.
//
// Keep command names short, reasonable, catchy and
// easy to type. At best, keep it a single word.
//
// Examples: build, list, install
Name string
// Brief is a short annotation of action command is capable of.
//
// Climax doesn't provide any limitations on the brief string
// format, however it's highly recommended to keep it a single
// lowercase phrase of 3-5 words without any punctuation marks.
//
// Example: compile packages and dependencies
Brief string
// Usage is a generic command use case, suggested by help.
//
// This string gets displayed on the usage line of command
// help entry. You should NOT include command name itself.
//
// Example: [-o output] [-i] [build flags] [packages]
Usage string
// Help is a detailed command reference displayed after
// the usage line and before the available flags block
// of the help entry.
//
// Try to stick to the 80 character limit, so it looks fine
// in the split terminal window.
Help string
// The group name this command belongs to.
Group string
// Handling, I bet it's pretty straight-forward.
Handle CmdHandler
// Flags are command-line options.
Flags []Flag
// Examples are annotated tips on command usage.
Examples []Example
}
// AddFlag does literally what its name says.
func (c *Command) AddFlag(newFlag Flag) {
c.Flags = append(c.Flags, newFlag)
}
// AddExample does exactly what its name says.
func (c *Command) AddExample(newExample Example) {
c.Examples = append(c.Examples, newExample)
}
// Run executes a command handler and returns corresponding exitcode.
func (c Command) Run(context Context) int {
return c.Handle(context)
}
// Topic is some sort of a concise wiki page.
type Topic struct {
// Name is a [A-Za-z_0-9] identifier of up to 11 characters.
//
// Keep topic names short, reasonable, catchy and
// easy to type. At best, keep it a single word.
//
// Examples: buildmode, packages, filetype
Name string
// Brief is a short annotation of the topic.
//
// Climax doesn't provide any limitations on the brief string
// format, however it's highly recommended to keep it a single
// lowercase phrase of 3-5 words without any punctuation marks.
//
// Example: description of package lists
Brief string
// Text is the actual topic content.
//
// Try to stick to the 80 character limit, so it looks fine
// in the split terminal window.
Text string
}
// Flag is an optional command-line option.
type Flag struct {
// A flag label without the prefix (--, -, whatever).
//
// Flag names can't contain more than 11 alphanumeric characters.
Name string
// Usually the first letter of the name.
//
// Short names can't contain more than 3 alphanumeric characters.
Short string
// Suggested use case, a generic example, showing
// user how to use the flag.
//
// Example: --filter="token"
Usage string
// Help is displayed under the corresponding flag's
// usage in the available commands section of help entry.
//
// Example: Limit tool output to tokens given.
Help string
// Variable flags have a flag value, non-variable don't.
//
// Flag value is a string assigned to particular flag.
// For instance, --force is a non-variable flag and
// --filter="token" is a variable flag.
Variable bool
}
// Example is an annotated use case of the command.
type Example struct {
// Usecase is a typical use of command.
//
// Make sure to omit application and command name here,
// since Climax appends it by default.
Usecase string
// Be descriptive, but keep it under 3-5 sentences.
Description string
}