forked from urfave/cli-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.go
More file actions
134 lines (116 loc) · 3.36 KB
/
Copy pathjson.go
File metadata and controls
134 lines (116 loc) · 3.36 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
package docs
import (
"encoding/json"
"strings"
"github.com/urfave/cli/v3"
)
type CLICommand struct {
Name string `json:"name"`
Aliases []string `json:"aliases,omitempty"`
Usage string `json:"usage,omitempty"`
UsageText []string `json:"usageText,omitempty"`
Description string `json:"description,omitempty"`
ArgsUsage string `json:"argsUsage,omitempty"`
Category string `json:"category,omitempty"`
Version string `json:"version,omitempty"`
Hidden bool `json:"hidden,omitempty"`
Flags []CLIFlag `json:"flags,omitempty"`
Commands []CLICommand `json:"commands,omitempty"`
}
type CLIFlag struct {
Name string `json:"name"`
Aliases []string `json:"aliases,omitempty"`
Usage string `json:"usage,omitempty"`
Type string `json:"type,omitempty"`
Default string `json:"default,omitempty"`
TakesValue bool `json:"takesValue"`
EnvVars []string `json:"envVars,omitempty"`
Hidden bool `json:"hidden,omitempty"`
Required bool `json:"required,omitempty"`
}
func ToJSON(cmd *cli.Command) ([]byte, error) {
out := prepareJSONCommand(cmd)
return json.MarshalIndent(out, "", " ")
}
func prepareJSONCommand(cmd *cli.Command) CLICommand {
tt := tabularTemplate{}
return CLICommand{
Name: cmd.Name,
Aliases: cmd.Aliases,
Usage: tt.PrepareMultilineString(cmd.Usage),
UsageText: prepareUsageTextLines(cmd.UsageText),
Description: tt.PrepareMultilineString(cmd.Description),
ArgsUsage: tt.PrepareMultilineString(cmd.ArgsUsage),
Category: cmd.Category,
Version: cmd.Version,
Hidden: cmd.Hidden,
Flags: prepareJSONFlags(cmd.Flags),
Commands: prepareJSONCommands(cmd.Commands),
}
}
func prepareJSONCommands(commands []*cli.Command) []CLICommand {
result := make([]CLICommand, 0, len(commands))
for _, cmd := range commands {
result = append(result, prepareJSONCommand(cmd))
}
return result
}
func prepareJSONFlags(flags []cli.Flag) []CLIFlag {
result := make([]CLIFlag, 0, len(flags))
tt := tabularTemplate{}
for _, f := range flags {
flag, ok := f.(cli.DocGenerationFlag)
if !ok {
continue
}
value, defaultText := getFlagDefaultValue(flag)
defaultValue := ""
if defaultText != "" {
defaultValue = defaultText
} else if value != "" {
defaultValue = value
}
jf := CLIFlag{
Usage: tt.PrepareMultilineString(flag.GetUsage()),
EnvVars: flag.GetEnvVars(),
TakesValue: flag.TakesValue(),
Default: defaultValue,
Type: flag.TypeName(),
}
names := f.Names()
for i, name := range names {
name = strings.TrimSpace(name)
if i == 0 {
jf.Name = "--" + name
continue
}
if len(name) > 1 {
name = "--" + name
} else {
name = "-" + name
}
jf.Aliases = append(jf.Aliases, name)
}
if vf, ok := f.(cli.VisibleFlag); ok {
jf.Hidden = !vf.IsVisible()
}
if rf, ok := f.(interface{ IsRequired() bool }); ok {
jf.Required = rf.IsRequired()
}
result = append(result, jf)
}
return result
}
func prepareUsageTextLines(s string) []string {
if s == "" {
return nil
}
s = strings.Trim(s, "\n")
lines := strings.Split(s, "\n")
filtered := make([]string, 0, len(lines))
for _, line := range lines {
trimmed := strings.TrimRight(line, "\r")
filtered = append(filtered, trimmed)
}
return filtered
}