/
application_descriptor.go
64 lines (48 loc) · 1.6 KB
/
application_descriptor.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
package console
import (
"fmt"
"github.com/eidolon/console/parameters"
"github.com/eidolon/wordwrap"
)
// DescribeApplication describes an Application to provide usage information.
func DescribeApplication(app *Application) string {
var help string
if app.Logo != "" {
help += fmt.Sprintf("%s\n", app.Logo)
}
help += fmt.Sprintf("%s version %s\n\n", app.Name, app.Version)
help += fmt.Sprintf("%s\n", describeApplicationUsage(app))
options := findApplicationOptions(app)
if len(options) > 0 {
help += fmt.Sprintf("\n%s", parameters.DescribeOptions(options))
}
if len(app.commands) > 0 {
help += fmt.Sprintf("\n%s", DescribeCommands(app.commands))
help += fmt.Sprintf(
"\n Run `$ %s COMMAND --help` for more information about a command.\n",
app.UsageName,
)
}
if len(app.Help) > 0 {
help += "\nHELP:\n"
help += wordwrap.Indent(app.Help, " ", true) + "\n"
}
return help
}
// describeApplicationUsage describes the application's usage, mainly based on whether or not the
// application has commands and if it does, whether you must run a command for anything to happen.
func describeApplicationUsage(app *Application) string {
desc := "USAGE:\n"
desc += fmt.Sprintf(" %s COMMAND [OPTIONS...] [ARGUMENTS...]", app.UsageName)
return desc
}
// findApplicationOptions finds all of the defined options on the given application.
func findApplicationOptions(app *Application) []parameters.Option {
definition := NewDefinition()
app.configure(definition)
var options []parameters.Option
for _, opt := range definition.Options() {
options = append(options, opt)
}
return options
}