Skip to content

Commit 1e485c1

Browse files
committed
- [#] for cmd/ refactoring
1 parent 1f7661c commit 1e485c1

3 files changed

Lines changed: 41 additions & 33 deletions

File tree

test/commandlineGoFlags.ref

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package main
99
////////////////////////////////////////////////////////////////////////////
1010
// Program: redo
1111
// Purpose: global option redo
12-
// Authors: Myself <me@mine.org> (c) 2022-2023, All rights reserved
12+
// Authors: Myself <me@mine.org> (c) 2022-2025, All rights reserved
1313
////////////////////////////////////////////////////////////////////////////
1414

1515
import (
@@ -21,6 +21,9 @@ import (
2121

2222
// Template for main starts here
2323

24+
// // for `go generate -x`
25+
// //go:generate sh redo_cliGen.sh
26+
2427
//////////////////////////////////////////////////////////////////////////
2528
// Constant and data type/structure definitions
2629

@@ -30,23 +33,23 @@ import (
3033
// var (
3134
// progname = "redo"
3235
// version = "0.1.0"
33-
// date = "2023-01-23"
36+
// date = "2025-04-13"
3437

35-
// // opts store all the configurable options
36-
// opts optsT
38+
// // Opts store all the configurable options
39+
// Opts OptsT
3740
// )
3841
//
39-
// var gfParser = flags.NewParser(&opts, flags.Default)
42+
// var gfParser = flags.NewParser(&Opts, flags.Default)
4043

4144
////////////////////////////////////////////////////////////////////////////
4245
// Function definitions
4346

4447
//==========================================================================
4548
// Function main
4649
// func main() {
47-
// opts.Version = showVersion
48-
// opts.Verbflg = func() {
49-
// opts.Verbose++
50+
// Opts.Version = showVersion
51+
// Opts.Verbflg = func() {
52+
// Opts.Verbose++
5053
// }
5154
//
5255
// if _, err := gfParser.Parse(); err != nil {
@@ -64,7 +67,7 @@ import (
6467
// func showVersion() {
6568
// fmt.Fprintf(os.Stderr, "redo - global option redo, version %s\n", version)
6669
// fmt.Fprintf(os.Stderr, "Built on %s\n", date)
67-
// fmt.Fprintf(os.Stderr, "Copyright (C) 2022-2023, Myself <me@mine.org>\n\n")
70+
// fmt.Fprintf(os.Stderr, "Copyright (C) 2022-2025, Myself <me@mine.org>\n\n")
6871
// fmt.Fprintf(os.Stderr, "Redo global option via automatic code-gen\n")
6972
// os.Exit(0)
7073
// }
@@ -77,8 +80,8 @@ import (
7780

7881
// Template for type define starts here
7982

80-
// The optsT type defines all the configurable options from cli.
81-
type optsT struct {
83+
// The OptsT type defines all the configurable options from cli.
84+
type OptsT struct {
8285
Host string `short:"H" long:"host" env:"REDO_HOST" description:"Host address" default:"localhost"`
8386
Port int `short:"p" long:"port" env:"REDO_PORT" description:"Listening port" default:"80"`
8487
Force bool `short:"f" long:"force" env:"REDO_FORCE" description:"Force start"`
@@ -93,7 +96,7 @@ type optsT struct {
9396
////////////////////////////////////////////////////////////////////////////
9497
// Program: redo
9598
// Purpose: global option redo
96-
// Authors: Myself <me@mine.org> (c) 2022-2023, All rights reserved
99+
// Authors: Myself <me@mine.org> (c) 2022-2025, All rights reserved
97100
////////////////////////////////////////////////////////////////////////////
98101

99102
// package main
@@ -130,9 +133,9 @@ type optsT struct {
130133
//
131134
// func (x *BuildCommand) Execute(args []string) error {
132135
// fmt.Fprintf(os.Stderr, "Build the network application\n")
133-
// // fmt.Fprintf(os.Stderr, "Copyright (C) 2022-2023, Myself <me@mine.org>\n\n")
134-
// clis.Setup("redo::build", opts.Verbose)
135-
// clis.Verbose(1, "Doing Build, with %+v, %+v", opts, args)
136+
// // fmt.Fprintf(os.Stderr, "Copyright (C) 2022-2025, Myself <me@mine.org>\n\n")
137+
// clis.Setup("redo::build", Opts.Verbose)
138+
// clis.Verbose(1, "Doing Build, with %+v, %+v", Opts, args)
136139
// // fmt.Println(x.Dir)
137140
// return x.Exec(args)
138141
// }
@@ -151,7 +154,7 @@ type optsT struct {
151154
////////////////////////////////////////////////////////////////////////////
152155
// Program: redo
153156
// Purpose: global option redo
154-
// Authors: Myself <me@mine.org> (c) 2022-2023, All rights reserved
157+
// Authors: Myself <me@mine.org> (c) 2022-2025, All rights reserved
155158
////////////////////////////////////////////////////////////////////////////
156159

157160
// package main
@@ -189,9 +192,9 @@ type optsT struct {
189192
//
190193
// func (x *InstallCommand) Execute(args []string) error {
191194
// fmt.Fprintf(os.Stderr, "Install the network application\n")
192-
// // fmt.Fprintf(os.Stderr, "Copyright (C) 2022-2023, Myself <me@mine.org>\n\n")
193-
// clis.Setup("redo::install", opts.Verbose)
194-
// clis.Verbose(1, "Doing Install, with %+v, %+v", opts, args)
195+
// // fmt.Fprintf(os.Stderr, "Copyright (C) 2022-2025, Myself <me@mine.org>\n\n")
196+
// clis.Setup("redo::install", Opts.Verbose)
197+
// clis.Verbose(1, "Doing Install, with %+v, %+v", Opts, args)
195198
// // fmt.Println(x.Dir, x.Suffix)
196199
// return x.Exec(args)
197200
// }
@@ -210,7 +213,7 @@ type optsT struct {
210213
////////////////////////////////////////////////////////////////////////////
211214
// Program: redo
212215
// Purpose: global option redo
213-
// Authors: Myself <me@mine.org> (c) 2022-2023, All rights reserved
216+
// Authors: Myself <me@mine.org> (c) 2022-2025, All rights reserved
214217
////////////////////////////////////////////////////////////////////////////
215218

216219
// package main
@@ -256,9 +259,9 @@ type optsT struct {
256259
//
257260
// func (x *PublishCommand) Execute(args []string) error {
258261
// fmt.Fprintf(os.Stderr, "Publish the network application\n")
259-
// // fmt.Fprintf(os.Stderr, "Copyright (C) 2022-2023, Myself <me@mine.org>\n\n")
260-
// clis.Setup("redo::publish", opts.Verbose)
261-
// clis.Verbose(1, "Doing Publish, with %+v, %+v", opts, args)
262+
// // fmt.Fprintf(os.Stderr, "Copyright (C) 2022-2025, Myself <me@mine.org>\n\n")
263+
// clis.Setup("redo::publish", Opts.Verbose)
264+
// clis.Verbose(1, "Doing Publish, with %+v, %+v", Opts, args)
262265
// // fmt.Println(x.Dir, x.Suffix, x.Out, x.Args)
263266
// return x.Exec(args)
264267
// }

test/commandlineGoFlags.tmpl

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import (
1515

1616
// Template for main starts here
1717

18+
// // for `go generate -x`
19+
// //go:generate sh {{$.ProgramName}}_cliGen.sh
20+
1821
//////////////////////////////////////////////////////////////////////////
1922
// Constant and data type/structure definitions
2023

@@ -26,11 +29,11 @@ import (
2629
// version = "0.1.0"
2730
// date = "{{ date "I" }}"
2831

29-
// // opts store all the configurable options
30-
// opts optsT
32+
// // Opts store all the configurable options
33+
// Opts OptsT
3134
// )
3235
//
33-
// var gfParser = flags.NewParser(&opts, flags.Default)
36+
// var gfParser = flags.NewParser(&Opts, flags.Default)
3437

3538
////////////////////////////////////////////////////////////////////////////
3639
// Function definitions
@@ -39,11 +42,11 @@ import (
3942
// Function main
4043
// func main() {
4144
{{- if .Version }}
42-
// opts.Version = showVersion
45+
// Opts.Version = showVersion
4346
{{- end }}
4447
{{- if .Verbose }}
45-
// opts.Verbflg = func() {
46-
// opts.Verbose++
48+
// Opts.Verbflg = func() {
49+
// Opts.Verbose++
4750
// }
4851
{{- end }}
4952
//
@@ -77,7 +80,7 @@ import (
7780

7881
// Template for type define starts here
7982

80-
{{template "type_struct" argsm "ProgramName" .ProgramName "Options" .Options "Verbose" .Verbose "Version" .Version "typeName" "optsT" "Prefix" "" }}
83+
{{template "type_struct" argsm "ProgramName" .ProgramName "Options" .Options "Verbose" .Verbose "Version" .Version "typeName" "OptsT" "Prefix" "" }}
8184
// Template for type define ends here
8285

8386
{{range .Command}}
@@ -116,9 +119,9 @@ import (
116119
// func (x *{{clk2uc .Name}}Command) Execute(args []string) error {
117120
// fmt.Fprintf(os.Stderr, "{{.Desc}}\n")
118121
// // fmt.Fprintf(os.Stderr, "Copyright (C) {{$.Since}}-{{ date "Y4" }}, {{or $.Authors "The Author(s) <they@their.org>"}}\n\n")
119-
// clis.Setup("{{$.Name}}::{{.Name}}", opts.Verbose)
120-
// clis.Verbose(1, "Doing {{clk2uc .Name}}, with %+v, %+v", opts, args)
121-
// {{$opts := .Options}}// fmt.Println({{range $i, $opt := .Options}}x.{{$opt.Name}}{{if lt $i ($opts | len | minus1)}}, {{end}}{{end}})
122+
// clis.Setup("{{$.Name}}::{{.Name}}", Opts.Verbose)
123+
// clis.Verbose(1, "Doing {{clk2uc .Name}}, with %+v, %+v", Opts, args)
124+
// {{$Opts := .Options}}// fmt.Println({{range $i, $opt := .Options}}x.{{$opt.Name}}{{if lt $i ($Opts | len | minus1)}}, {{end}}{{end}})
122125
// return x.Exec(args)
123126
// }
124127
//

test/commandlineGoFlags.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# GoFlags based cli def file
2+
13
# program name, name for the executable
24
ProgramName: redo
35
Authors: Myself <me@mine.org>

0 commit comments

Comments
 (0)