-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
199 lines (154 loc) · 5.6 KB
/
main.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"os"
"flag"
"fmt"
"strings"
"github.com/fatih/color"
cli "github.com/jawher/mow.cli"
)
var (
version string
)
func main() {
if version == "" {
version = "tip"
}
outerApp := cli.App("parsopt", shortDesc)
outerApp.Version("v version", version)
outerApp.Spec = "DIRECTIVES -- [ARGS...]"
outerApp.LongDesc = longHelp
outerApp.ErrorHandling = flag.ExitOnError
var (
directives = outerApp.StringArg("DIRECTIVES", "", "The accepted options and arguments. Run parsopt --help for the syntax reference")
args = outerApp.StringsArg("ARGS", nil, "The args to be passed to the generated CLI app")
)
outerApp.Action = func() {
appDecl, err := parse(*directives)
if err != nil {
fmt.Fprintln(os.Stderr, err)
printGuard()
cli.Exit(1)
}
app := genApp(appDecl)
appArgs := make([]string, len(*args)+1)
appArgs[0] = appDecl.Name
for i, a := range *args {
appArgs[i+1] = a
}
runApp(app, appArgs)
}
_ = outerApp.Run(os.Args)
}
var (
title = color.New(color.Bold).SprintfFunc()
title2 = color.New(color.FgBlue, color.Bold).SprintfFunc()
title3 = color.New(color.Underline).SprintfFunc()
code = color.New(color.FgGreen).SprintfFunc()
output = color.New(color.FgHiBlack).SprintfFunc()
shortDesc = `The parsopt utility is used to quickly and easily accept and validate options and arguments in shell procedures.
It's much more powerful compared to getopt[s] as it handles short and long options, arguments, default values, automatic help generation. etc.`
longHelp = shortDesc + "\n" +
title("How to use parsopt") + `
parsopt should be called from a shell function.
Place this code snippet in your .bashrc, .zshrc or whatever file is loaded by your shell:
` + code(`
awesome() {
eval "$(parsopt '
app AwesomeAppName awesome description
spec [-r] SRC... DST
opt recursive -r --recursive :bool Copy the entire src subtree
arg src :strings The source file to copy
arg dst The destination file or directory
' "$@")"
# Make use of the ${recursive}, ${src} and ${dst} variables
}`) + `
Make sure to open a new terminal window or tab (or to reload).
If you run:
` + code("$ awesome") + `
You'll get the following output:
` + output(`
Error: incorrect usage
Usage: AwesomeAppName [-r] SRC... DST
awesome description
Arguments:
SRC=[""] The source file to copy
DST="" The destination file or directory
Options:
-r, --recursive=false Copy the entire src subtree
`) + `
(Same with "awesome -h" or "fixer --help").
You just witnessed parsopt options/arguments validation and help message generation in action.
If you call it again with a valid syntax:
` + code("$ awesome file.png /tmp/dir") + `
The variables ${recursive}, ${src} and ${dst} will be populated with "", "file.png" and "/tmp/dir" respectively.
You can then use these variables to perform the desired operation.
` + title("Spec reference") + `
The first argument to parsopt is a multiline string, with one directive per-lien (blank lines are skipped).
A directive has the following syntax:
` + code("DIRECTIVE_NAME VALUE VALUES*") + `
parsopt supports the following directives:
` +
dirHelp(
"app",
"Optional. Let's you set the name of the utility and the description to be shown in the usage message.",
"app APP_NAME DESC?",
nil,
[]string{
"app awesome",
"app awesome and here is the description",
},
) +
dirHelp(
"opt",
"Optional. Let's you declare an option/flag.",
"opt VAR_NAME ('-'SHORT_NAME | '--'LONG_NAME)+ (':'TYPE) ('='DEFAULT_VALUE) ('$'ENV_VAR) DESC?",
[]string{
"VAR_NAME: the shell variable that will be populated from the option",
":TYPE: one of \":string\", \":int\", \":bool\", \":ints\", \":strings\" Default is \":string\"",
"=DEFAULT_VALUE: the default value if the argument is not set, e.g. \"=/tmp/out\", \"=true\"",
"'$'ENV_VAR: set the value from the specified env var if the option is not set, e.g. \"$HOME\"",
},
[]string{"opt debug -d --debug :bool $DEBUG_MODE =false Enable debug log"},
) +
dirHelp(
"arg",
"Optional. Let's you declare an argument.",
"arg VAR_NAME (':'TYPE) ('='DEFAULT_VALUE) ('$'ENV_VAR) DESC?",
[]string{
"VAR_NAME: the shell variable that will be populated from the argument",
":TYPE: one of \":string\", \":int\", \":bool\", \":ints\", \":strings\" Default is \":string\"",
"=DEFAULT_VALUE: the default value if the argument is not set, e.g. \"=/tmp/out\", \"=true\"",
"'$'ENV_VAR: set the value from the specified env var if the argument is not set, e.g. \"$HOME\"",
},
[]string{
"arg file_name $DEBUG_MODE Enable debug log",
"arg files_to_copy :strings The files to copy",
},
) +
dirHelp(
"spec",
"Optional. Let's you override the default spec string for the app generated by mow.cli (https://github.com/jawher/mow.cli#spec).\n\n"+
"You can refer to the:\n"+
"* options (declared using the opt directive) using their short or long names (e.g. -r, --verbose)\n"+
"* arguments (declared with the arg directive) by uppercasing their shell variable name",
"spec SPEC",
nil,
[]string{
"spec [ -f | -d ] [-RHL] SRC DST...",
},
)
)
func dirHelp(name, desc, syntax string, where []string, examples []string) string {
res := title2(name) + "\n\n" + desc + "\n\n" +
title3("Syntax") + "\n\n" + code(syntax) + "\n\n"
if len(where) > 0 {
res += "Where:\n"
for _, w := range where {
res += "* " + w + "\n"
}
res += "\n\n"
}
res += title3("Examples") + "\n\n" + code(strings.Join(examples, "\n\n")) + "\n\n"
return res
}