Skip to content

Commit 8e6155b

Browse files
committed
- [#] update cmd:fmt and the whole GoFlags based wireframe
1 parent 96d1214 commit 8e6155b

File tree

10 files changed

+69
-51
lines changed

10 files changed

+69
-51
lines changed

cmd_esc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func init() {
3939
func (x *EscCommand) Execute(args []string) error {
4040
fmt.Fprintf(os.Stderr, "Escape json string\n")
4141
// fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2023, Tong Sun\n\n")
42-
clis.Setup("jsonfiddle::esc", opts.Verbose)
43-
clis.Verbose(1, "Doing Esc, with %+v, %+v", opts, args)
42+
clis.Setup("jsonfiddle::esc", Opts.Verbose)
43+
clis.Verbose(1, "Doing Esc, with %+v, %+v", Opts, args)
4444
// fmt.Println(x.Filei, x.Fileo)
4545
return x.Exec(args)
4646
}

cmd_fmt.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
////////////////////////////////////////////////////////////////////////////
22
// Program: jsonfiddle
33
// Purpose: JSON Fiddling
4-
// Authors: Tong Sun (c) 2017-2023, All rights reserved
4+
// Authors: Tong Sun (c) 2017-2025, All rights reserved
55
////////////////////////////////////////////////////////////////////////////
66

77
package main
@@ -22,6 +22,7 @@ import (
2222
type FmtCommand struct {
2323
Filei string `short:"i" long:"input" description:"the source to get json string from (mandatory)" required:"true"`
2424
Fileo string `short:"o" long:"output" description:"the output, default to stdout" default:"-"`
25+
Concise bool `short:"s" long:"concise" description:"Compact the top level array into concise array style"`
2526
Unescape bool `short:"u" long:"unescape" description:"Unescape unicode of form \u003c to their literal characters"`
2627
}
2728

@@ -33,16 +34,17 @@ var fmtCommand FmtCommand
3334
func init() {
3435
gfParser.AddCommand("fmt",
3536
"Format json string",
36-
"",
37+
`
38+
`,
3739
&fmtCommand)
3840
}
3941

4042
func (x *FmtCommand) Execute(args []string) error {
4143
fmt.Fprintf(os.Stderr, "Format json string\n")
42-
// fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2023, Tong Sun\n\n")
43-
clis.Setup("jsonfiddle::fmt", opts.Verbose)
44-
clis.Verbose(1, "Doing Fmt, with %+v, %+v", opts, args)
45-
// fmt.Println(x.Filei, x.Fileo)
44+
// fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2025, Tong Sun\n\n")
45+
clis.Setup("jsonfiddle::fmt", Opts.Verbose)
46+
clis.Verbose(1, "Doing Fmt, with %+v, %+v", Opts, args)
47+
// fmt.Println(x.Filei, x.Fileo, x.Concise, x.Unescape)
4648
return x.Exec(args)
4749
}
4850

cmd_j2s.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ func init() {
4343
func (x *J2sCommand) Execute(args []string) error {
4444
fmt.Fprintf(os.Stderr, "JSON to struct\n")
4545
// fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2023, Tong Sun\n\n")
46-
clis.Setup("jsonfiddle::j2s", opts.Verbose)
47-
clis.Verbose(1, "Doing J2s, with %+v, %+v", opts, args)
46+
clis.Setup("jsonfiddle::j2s", Opts.Verbose)
47+
clis.Verbose(1, "Doing J2s, with %+v, %+v", Opts, args)
4848
// fmt.Println(x.FmtType, x.Filei, x.Fileo, x.Name, x.Pkg, x.SubStruct)
4949
return x.Exec(args)
5050
}

cmd_sort.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func init() {
3939
func (x *SortCommand) Execute(args []string) error {
4040
fmt.Fprintf(os.Stderr, "Sort json fields recursively\n")
4141
// fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2023, Tong Sun\n\n")
42-
clis.Setup("jsonfiddle::sort", opts.Verbose)
43-
clis.Verbose(1, "Doing Sort, with %+v, %+v", opts, args)
42+
clis.Setup("jsonfiddle::sort", Opts.Verbose)
43+
clis.Verbose(1, "Doing Sort, with %+v, %+v", Opts, args)
4444
// fmt.Println(x.Filei, x.Fileo)
4545
return x.Exec(args)
4646
}

cmd_x2j.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func init() {
3939
func (x *X2jCommand) Execute(args []string) error {
4040
fmt.Fprintf(os.Stderr, "XML to JSON\n")
4141
// fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2023, Tong Sun\n\n")
42-
clis.Setup("jsonfiddle::x2j", opts.Verbose)
43-
clis.Verbose(1, "Doing X2j, with %+v, %+v", opts, args)
42+
clis.Setup("jsonfiddle::x2j", Opts.Verbose)
43+
clis.Verbose(1, "Doing X2j, with %+v, %+v", Opts, args)
4444
// fmt.Println(x.Filei, x.Fileo)
4545
return x.Exec(args)
4646
}

imp_fmt.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ func (x *FmtCommand) Exec(args []string) error {
2828

2929
var out bytes.Buffer
3030
var err error
31-
if opts.Compact {
31+
if Opts.Compact {
3232
err = json.Compact(&out, data)
3333
} else {
34-
err = json.Indent(&out, data, opts.Prefix, opts.Indent)
34+
err = json.Indent(&out, data, Opts.Prefix, Opts.Indent)
3535
}
3636
clis.AbortOn("Formatting input", err)
3737
fileO := clis.GetOutputStream(x.Fileo)

imp_sort.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ func cmdSort(r io.Reader, w io.Writer) error {
3434
json.Unmarshal(content, &res)
3535
var js []byte
3636
var err error
37-
if opts.Compact {
37+
if Opts.Compact {
3838
js, err = json.Marshal(res)
3939
} else {
40-
js, err = json.MarshalIndent(res, opts.Prefix, opts.Indent)
40+
js, err = json.MarshalIndent(res, Opts.Prefix, Opts.Indent)
4141
}
4242
clis.AbortOn("[::sort] Marshaling input", err)
4343
fmt.Fprintln(w, string(js))

jsonfiddle_cli.yaml

Lines changed: 7 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: jsonfiddle
35
Authors: Tong Sun
@@ -75,6 +77,11 @@ Command:
7577
Value: "-"
7678
Usage: 'the output, default to stdout'
7779

80+
- Name: Concise
81+
Type: bool
82+
Flag: 's,concise'
83+
Usage: "Compact the top level array into concise array style"
84+
7885
- Name: Unescape
7986
Type: bool
8087
Flag: 'u,unescape'

jsonfiddle_cliDef.go

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import (
1919

2020
// Template for main starts here
2121

22+
// // for `go generate -x`
23+
// //go:generate sh jsonfiddle_cliGen.sh
24+
2225
//////////////////////////////////////////////////////////////////////////
2326
// Constant and data type/structure definitions
2427

@@ -28,23 +31,23 @@ import (
2831
// var (
2932
// progname = "jsonfiddle"
3033
// version = "0.1.0"
31-
// date = "2025-01-15"
34+
// date = "2025-11-18"
3235

33-
// // opts store all the configurable options
34-
// opts optsT
36+
// // Opts store all the configurable options
37+
// Opts OptsT
3538
// )
3639
//
37-
// var gfParser = flags.NewParser(&opts, flags.Default)
40+
// var gfParser = flags.NewParser(&Opts, flags.Default)
3841

3942
////////////////////////////////////////////////////////////////////////////
4043
// Function definitions
4144

4245
//==========================================================================
4346
// Function main
4447
// func main() {
45-
// opts.Version = showVersion
46-
// opts.Verbflg = func() {
47-
// opts.Verbose++
48+
// Opts.Version = showVersion
49+
// Opts.Verbflg = func() {
50+
// Opts.Verbose++
4851
// }
4952
//
5053
// if _, err := gfParser.Parse(); err != nil {
@@ -75,8 +78,8 @@ import (
7578

7679
// Template for type define starts here
7780

78-
// The optsT type defines all the configurable options from cli.
79-
type optsT struct {
81+
// The OptsT type defines all the configurable options from cli.
82+
type OptsT struct {
8083
Compact bool `short:"c" long:"compact" description:"Compact JSON data, remove all whitespaces"`
8184
Prefix string `long:"prefix" description:"prefix for json string output"`
8285
Indent string `short:"d" long:"indent" description:"indent for json string output" default:" "`
@@ -124,15 +127,16 @@ type optsT struct {
124127
// func init() {
125128
// gfParser.AddCommand("esc",
126129
// "Escape json string",
127-
// "",
130+
// `
131+
// `,
128132
// &escCommand)
129133
// }
130134
//
131135
// func (x *EscCommand) Execute(args []string) error {
132136
// fmt.Fprintf(os.Stderr, "Escape json string\n")
133137
// // fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2025, Tong Sun\n\n")
134-
// clis.Setup("jsonfiddle::esc", opts.Verbose)
135-
// clis.Verbose(1, "Doing Esc, with %+v, %+v", opts, args)
138+
// clis.Setup("jsonfiddle::esc", Opts.Verbose)
139+
// clis.Verbose(1, "Doing Esc, with %+v, %+v", Opts, args)
136140
// // fmt.Println(x.Filei, x.Fileo)
137141
// return x.Exec(args)
138142
// }
@@ -172,6 +176,7 @@ type optsT struct {
172176
// type FmtCommand struct {
173177
// Filei string `short:"i" long:"input" description:"the source to get json string from (mandatory)" required:"true"`
174178
// Fileo string `short:"o" long:"output" description:"the output, default to stdout" default:"-"`
179+
// Concise bool `short:"s" long:"concise" description:"Compact the top level array into concise array style"`
175180
// Unescape bool `short:"u" long:"unescape" description:"Unescape unicode of form \u003c to their literal characters"`
176181
// }
177182

@@ -184,16 +189,17 @@ type optsT struct {
184189
// func init() {
185190
// gfParser.AddCommand("fmt",
186191
// "Format json string",
187-
// "",
192+
// `
193+
// `,
188194
// &fmtCommand)
189195
// }
190196
//
191197
// func (x *FmtCommand) Execute(args []string) error {
192198
// fmt.Fprintf(os.Stderr, "Format json string\n")
193199
// // fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2025, Tong Sun\n\n")
194-
// clis.Setup("jsonfiddle::fmt", opts.Verbose)
195-
// clis.Verbose(1, "Doing Fmt, with %+v, %+v", opts, args)
196-
// // fmt.Println(x.Filei, x.Fileo, x.Unescape)
200+
// clis.Setup("jsonfiddle::fmt", Opts.Verbose)
201+
// clis.Verbose(1, "Doing Fmt, with %+v, %+v", Opts, args)
202+
// // fmt.Println(x.Filei, x.Fileo, x.Concise, x.Unescape)
197203
// return x.Exec(args)
198204
// }
199205
//
@@ -243,15 +249,16 @@ type optsT struct {
243249
// func init() {
244250
// gfParser.AddCommand("sort",
245251
// "Sort json fields recursively",
246-
// "",
252+
// `
253+
// `,
247254
// &sortCommand)
248255
// }
249256
//
250257
// func (x *SortCommand) Execute(args []string) error {
251258
// fmt.Fprintf(os.Stderr, "Sort json fields recursively\n")
252259
// // fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2025, Tong Sun\n\n")
253-
// clis.Setup("jsonfiddle::sort", opts.Verbose)
254-
// clis.Verbose(1, "Doing Sort, with %+v, %+v", opts, args)
260+
// clis.Setup("jsonfiddle::sort", Opts.Verbose)
261+
// clis.Verbose(1, "Doing Sort, with %+v, %+v", Opts, args)
255262
// // fmt.Println(x.Filei, x.Fileo)
256263
// return x.Exec(args)
257264
// }
@@ -306,15 +313,16 @@ type optsT struct {
306313
// func init() {
307314
// gfParser.AddCommand("j2s",
308315
// "JSON to struct",
309-
// "JSON convert to Go struct",
316+
// `JSON convert to Go struct
317+
// `,
310318
// &j2sCommand)
311319
// }
312320
//
313321
// func (x *J2sCommand) Execute(args []string) error {
314322
// fmt.Fprintf(os.Stderr, "JSON to struct\n")
315323
// // fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2025, Tong Sun\n\n")
316-
// clis.Setup("jsonfiddle::j2s", opts.Verbose)
317-
// clis.Verbose(1, "Doing J2s, with %+v, %+v", opts, args)
324+
// clis.Setup("jsonfiddle::j2s", Opts.Verbose)
325+
// clis.Verbose(1, "Doing J2s, with %+v, %+v", Opts, args)
318326
// // fmt.Println(x.FmtType, x.Filei, x.Fileo, x.Name, x.Pkg, x.SubStruct)
319327
// return x.Exec(args)
320328
// }
@@ -365,15 +373,16 @@ type optsT struct {
365373
// func init() {
366374
// gfParser.AddCommand("x2j",
367375
// "XML to JSON",
368-
// "",
376+
// `
377+
// `,
369378
// &x2jCommand)
370379
// }
371380
//
372381
// func (x *X2jCommand) Execute(args []string) error {
373382
// fmt.Fprintf(os.Stderr, "XML to JSON\n")
374383
// // fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2025, Tong Sun\n\n")
375-
// clis.Setup("jsonfiddle::x2j", opts.Verbose)
376-
// clis.Verbose(1, "Doing X2j, with %+v, %+v", opts, args)
384+
// clis.Setup("jsonfiddle::x2j", Opts.Verbose)
385+
// clis.Verbose(1, "Doing X2j, with %+v, %+v", Opts, args)
377386
// // fmt.Println(x.Filei, x.Fileo)
378387
// return x.Exec(args)
379388
// }

jsonfiddle_main.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,21 @@ var (
3535
version = "0.5.0"
3636
date = "2023-01-22"
3737

38-
// opts store all the configurable options
39-
opts optsT
38+
// Opts store all the configurable options
39+
Opts OptsT
4040
)
4141

42-
var gfParser = flags.NewParser(&opts, flags.Default)
42+
var gfParser = flags.NewParser(&Opts, flags.Default)
4343

4444
////////////////////////////////////////////////////////////////////////////
4545
// Function definitions
4646

47-
//==========================================================================
47+
// ==========================================================================
4848
// Function main
4949
func main() {
50-
opts.Version = showVersion
51-
opts.Verbflg = func() {
52-
opts.Verbose++
50+
Opts.Version = showVersion
51+
Opts.Verbflg = func() {
52+
Opts.Verbose++
5353
}
5454

5555
if _, err := gfParser.Parse(); err != nil {
@@ -77,7 +77,7 @@ func readJson(r io.Reader) []byte {
7777
data, err := ioutil.ReadAll(r)
7878
clis.AbortOn("Reading json input", err)
7979

80-
if opts.Protect {
80+
if Opts.Protect {
8181
data = regexp.MustCompile(`({{)([^ }]+)(}})`).
8282
ReplaceAll(data, []byte(`<<${2}>>`))
8383
// "age":<<C_age>> => "age":"<<C_age>>"

0 commit comments

Comments
 (0)