Skip to content

Commit 4daf344

Browse files
committed
- [+] add go-flags wireframing support
1 parent 0cc6d0b commit 4daf344

File tree

5 files changed

+447
-0
lines changed

5 files changed

+447
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{{- define "header" -}}
2+
////////////////////////////////////////////////////////////////////////////
3+
// Program: {{.Name}}
4+
// Purpose: {{.Desc}}
5+
// Authors: {{or .Authors "Author"}} (c) {{date "Y4"}}, All rights reserved
6+
////////////////////////////////////////////////////////////////////////////
7+
{{- end}}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{{- define "type_struct" -}}
2+
////////////////////////////////////////////////////////////////////////////
3+
// Constant and data type/structure definitions
4+
5+
// The {{.typeName}} type defines all the configurable options from cli.
6+
// type {{.typeName}} struct { {{- range .Options}}
7+
{{- if eq .Name "Args" }}
8+
//
9+
// {{.Args}}
10+
{{- else }}{{$f := stringsSplit .Flag ","}}{{ $flen := len $f }}
11+
// {{.Name}} {{.Type}} `
12+
{{- if gt $flen 1}}short:"{{index $f 0}}" long:"{{index $f 1}}"
13+
{{- else}}
14+
{{- if le ((index $f 0) | len) 1 }}short:"{{index $f 0}}
15+
{{- else}}long:"{{index $f 0}}"{{end}}
16+
{{- end}}
17+
{{- if .EnvV}} env:"{{printf "%s_%s" (clk2ss $.ProgramName) (clk2ss .Name)}}"{{end}} description:"{{.Usage}}"
18+
{{- if .Value}} default:"{{.Value}}"{{end}}
19+
{{- if .Required}} required:"true"{{end}}`{{end}}
20+
{{- end}}
21+
{{- if .Verbose}}
22+
// Verbflg func() `short:"v" long:"verbose" description:"Verbose mode (Multiple -v options increase the verbosity)"`
23+
// Verbose uint
24+
{{end}}
25+
// }
26+
{{- end}}

test/commandlineGoFlags.ref

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
2+
3+
////////////////////////////////////////////////////////////////////////////
4+
// Program: redo
5+
// Purpose: global option redo
6+
// Authors: Myself <[email protected]> (c) 2022, All rights reserved
7+
////////////////////////////////////////////////////////////////////////////
8+
9+
package main
10+
11+
import (
12+
// "fmt"
13+
// "os"
14+
15+
// "github.com/jessevdk/go-flags"
16+
)
17+
18+
////////////////////////////////////////////////////////////////////////////
19+
// Constant and data type/structure definitions
20+
21+
// The OptsT type defines all the configurable options from cli.
22+
// type OptsT struct {
23+
// Host string `short:"H" long:"host" env:"REDO_HOST" description:"host address" default:"localhost"`
24+
// Port int `short:"p" long:"port" env:"REDO_PORT" description:"listening port" default:"80"`
25+
// Force bool `short:"f" long:"force" env:"REDO_FORCE" description:"force start"`
26+
// Verbflg func() `short:"v" long:"verbose" description:"Verbose mode (Multiple -v options increase the verbosity)"`
27+
// Verbose uint
28+
29+
// }
30+
31+
32+
// Template for main starts here
33+
34+
////////////////////////////////////////////////////////////////////////////
35+
// Global variables definitions
36+
37+
// var (
38+
// progname = "redo"
39+
// version = "0.1.0"
40+
// date = "2022-01-17"
41+
42+
// // Opts store all the configurable options
43+
// Opts OptsT
44+
// )
45+
//
46+
// var parser = flags.NewParser(&Opts, flags.Default)
47+
48+
////////////////////////////////////////////////////////////////////////////
49+
// Function definitions
50+
51+
// Function main
52+
// func main() {
53+
// Opts.Verbflg = func() {
54+
// Opts.Verbose++
55+
// }
56+
//
57+
// if _, err := parser.Parse(); err != nil {
58+
// switch flagsErr := err.(type) {
59+
// case flags.ErrorType:
60+
// if flagsErr == flags.ErrHelp {
61+
// os.Exit(0)
62+
// }
63+
// os.Exit(1)
64+
// default:
65+
// fmt.Println()
66+
// parser.WriteHelp(os.Stdout)
67+
// os.Exit(1)
68+
// }
69+
// }
70+
// fmt.Println("")
71+
// }
72+
// Template for main ends here
73+
74+
75+
// Template for "build" CLI handling starts here
76+
////////////////////////////////////////////////////////////////////////////
77+
// Program: redo
78+
// Purpose: global option redo
79+
// Authors: Myself <[email protected]> (c) 2022, All rights reserved
80+
////////////////////////////////////////////////////////////////////////////
81+
82+
// package main
83+
84+
// import (
85+
// "fmt"
86+
// "os"
87+
// )
88+
89+
// *** Sub-command: build ***
90+
91+
////////////////////////////////////////////////////////////////////////////
92+
// Constant and data type/structure definitions
93+
94+
// The BuildCommand type defines all the configurable options from cli.
95+
// type BuildCommand struct {
96+
// Dir string `long:"dir" description:"source code root dir" default:"./"`
97+
// }
98+
99+
//
100+
// var buildCommand BuildCommand
101+
//
102+
// BuildCommand implements the business logic of command `build`
103+
// func (x *BuildCommand) Execute(args []string) error {
104+
// fmt.Fprintf(os.Stderr, "Build the network application\n")
105+
// // fmt.Fprintf(os.Stderr, "Copyright (C) 2022, Myself <[email protected]>\n\n")
106+
// // fmt.Printf("Doing Build, with %#v\n", args)
107+
// // fmt.Println(x.Dir)
108+
// // err := ...
109+
// return nil
110+
// }
111+
//
112+
// func init() {
113+
// parser.AddCommand("build",
114+
// "Build the network application",
115+
// "Usage:\n redo build [Options] Arch(i386|amd64)",
116+
// &buildCommand)
117+
// }
118+
// Template for "build" CLI handling ends here
119+
120+
// Template for "install" CLI handling starts here
121+
////////////////////////////////////////////////////////////////////////////
122+
// Program: redo
123+
// Purpose: global option redo
124+
// Authors: Myself <[email protected]> (c) 2022, All rights reserved
125+
////////////////////////////////////////////////////////////////////////////
126+
127+
// package main
128+
129+
// import (
130+
// "fmt"
131+
// "os"
132+
// )
133+
134+
// *** Sub-command: install ***
135+
136+
////////////////////////////////////////////////////////////////////////////
137+
// Constant and data type/structure definitions
138+
139+
// The InstallCommand type defines all the configurable options from cli.
140+
// type InstallCommand struct {
141+
// Dir string `short:"d description:"source code root dir" default:"./"`
142+
// Suffix string `long:"suffix" description:"source file suffix" default:".go,.c,.s"`
143+
// }
144+
145+
//
146+
// var installCommand InstallCommand
147+
//
148+
// InstallCommand implements the business logic of command `install`
149+
// func (x *InstallCommand) Execute(args []string) error {
150+
// fmt.Fprintf(os.Stderr, "Install the network application\n")
151+
// // fmt.Fprintf(os.Stderr, "Copyright (C) 2022, Myself <[email protected]>\n\n")
152+
// // fmt.Printf("Doing Install, with %#v\n", args)
153+
// // fmt.Println(x.Dir, x.Suffix)
154+
// // err := ...
155+
// return nil
156+
// }
157+
//
158+
// func init() {
159+
// parser.AddCommand("install",
160+
// "Install the network application",
161+
// "The add command adds a file to the repository. Use -a to add all files",
162+
// &installCommand)
163+
// }
164+
// Template for "install" CLI handling ends here
165+
166+
// Template for "publish" CLI handling starts here
167+
////////////////////////////////////////////////////////////////////////////
168+
// Program: redo
169+
// Purpose: global option redo
170+
// Authors: Myself <[email protected]> (c) 2022, All rights reserved
171+
////////////////////////////////////////////////////////////////////////////
172+
173+
// package main
174+
175+
// import (
176+
// "fmt"
177+
// "os"
178+
// )
179+
180+
// *** Sub-command: publish ***
181+
182+
////////////////////////////////////////////////////////////////////////////
183+
// Constant and data type/structure definitions
184+
185+
// The PublishCommand type defines all the configurable options from cli.
186+
// type PublishCommand struct {
187+
// Dir string `short:"d" long:"dir" description:"publish dir" required:"true"`
188+
// Suffix string `long:"suffix" description:"source file suffix" default:".go,.c,.s"`
189+
// Out string `short:"o" long:"out" description:"output filename"`
190+
//
191+
// // Example of positional arguments
192+
// Args struct {
193+
// ID string
194+
// Num int
195+
// Rest []string
196+
// } `positional-args:"yes" required:"yes"`
197+
198+
// }
199+
200+
//
201+
// var publishCommand PublishCommand
202+
//
203+
// PublishCommand implements the business logic of command `publish`
204+
// func (x *PublishCommand) Execute(args []string) error {
205+
// fmt.Fprintf(os.Stderr, "Publish the network application\n")
206+
// // fmt.Fprintf(os.Stderr, "Copyright (C) 2022, Myself <[email protected]>\n\n")
207+
// // fmt.Printf("Doing Publish, with %#v\n", args)
208+
// // fmt.Println(x.Dir, x.Suffix, x.Out, x.Args)
209+
// // err := ...
210+
// return nil
211+
// }
212+
//
213+
// func init() {
214+
// parser.AddCommand("publish",
215+
// "Publish the network application",
216+
// "Publish the built network application to central repo",
217+
// &publishCommand)
218+
// }
219+
// Template for "publish" CLI handling ends here
220+
221+

test/commandlineGoFlags.tmpl

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
{{- template "header" $ }}
2+
3+
package {{$.PackageName}}
4+
5+
import (
6+
// "fmt"
7+
// "os"
8+
9+
// "github.com/jessevdk/go-flags"
10+
)
11+
12+
{{template "type_struct" argsm "ProgramName" .ProgramName "Options" .Options "Verbose" .Verbose "typeName" "OptsT" }}
13+
14+
15+
// Template for main starts here
16+
17+
////////////////////////////////////////////////////////////////////////////
18+
// Global variables definitions
19+
20+
// var (
21+
// progname = "{{.Name}}"
22+
// version = "0.1.0"
23+
// date = "{{ date "I" }}"
24+
25+
// // Opts store all the configurable options
26+
// Opts OptsT
27+
// )
28+
//
29+
// var parser = flags.NewParser(&Opts, flags.Default)
30+
31+
////////////////////////////////////////////////////////////////////////////
32+
// Function definitions
33+
34+
// Function main
35+
// func main() {
36+
{{- if .Verbose }}
37+
// Opts.Verbflg = func() {
38+
// Opts.Verbose++
39+
// }
40+
{{- end }}
41+
//
42+
// if _, err := parser.Parse(); err != nil {
43+
// switch flagsErr := err.(type) {
44+
// case flags.ErrorType:
45+
// if flagsErr == flags.ErrHelp {
46+
// os.Exit(0)
47+
// }
48+
// os.Exit(1)
49+
// default:
50+
// fmt.Println()
51+
// parser.WriteHelp(os.Stdout)
52+
// os.Exit(1)
53+
// }
54+
// }
55+
// fmt.Println("")
56+
// }
57+
// Template for main ends here
58+
59+
{{range .Command}}
60+
// Template for "{{.Name}}" CLI handling starts here
61+
{{template "header" $ }}
62+
63+
// package {{$.PackageName}}
64+
65+
// import (
66+
// "fmt"
67+
// "os"
68+
// )
69+
70+
// *** Sub-command: {{.Name}} ***
71+
72+
{{template "type_struct" argsm "ProgramName" $.ProgramName "Options" .Options "typeName" (print (stringsTitle .Name) "Command") }}
73+
74+
//
75+
// var {{.Name}}Command {{stringsTitle .Name}}Command
76+
//
77+
// {{stringsTitle .Name}}Command implements the business logic of command `{{.Name}}`
78+
// func (x *{{stringsTitle .Name}}Command) Execute(args []string) error {
79+
// fmt.Fprintf(os.Stderr, "{{.Desc}}\n")
80+
// // fmt.Fprintf(os.Stderr, "Copyright (C) {{ date "Y4" }}, {{or $.Authors "The Author(s) <[email protected]>"}}\n\n")
81+
// // fmt.Printf("Doing {{stringsTitle .Name}}, with %#v\n", args)
82+
// // {{$opts := .Options}}fmt.Println({{range $i, $opt := .Options}}x.{{$opt.Name}}{{if lt $i ($opts | len | minus1)}}, {{end}}{{end}})
83+
// // err := ...
84+
// return nil
85+
// }
86+
//
87+
// func init() {
88+
// parser.AddCommand("{{.Name}}",
89+
// "{{.Desc}}",
90+
// "{{.Text}}",
91+
// &{{.Name}}Command)
92+
// }
93+
// Template for "{{.Name}}" CLI handling ends here
94+
{{end}}
95+

0 commit comments

Comments
 (0)