Skip to content

Commit bc100eb

Browse files
committed
migrated from package cli to flag for toml2json
1 parent 3f058de commit bc100eb

File tree

2 files changed

+146
-75
lines changed

2 files changed

+146
-75
lines changed

cmd/toml2json/toml2json.go

Lines changed: 103 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,77 @@ package main
2121

2222
import (
2323
"encoding/json"
24+
"flag"
2425
"fmt"
2526
"io"
2627
"os"
28+
"path"
29+
"strings"
2730

2831
// CaltechLibrary packages
29-
"github.com/caltechlibrary/cli"
3032
"github.com/caltechlibrary/datatools"
3133

3234
// 3rd Party packages
3335
"github.com/BurntSushi/toml"
3436
)
3537

3638
var (
37-
description = `
38-
%s is a tool that converts TOML into JSON output.
39-
`
39+
helpText = `---
40+
title: "{app_name} (1) user manual"
41+
author: "R. S. Doiel"
42+
pubDate: 2023-01-09
43+
---
44+
45+
# NAME
46+
47+
{app_name}
48+
49+
# SYNOPSIS
50+
51+
{app_name} [OPTIONS] [TOML_FILENAME] [JSON_NAME]
52+
53+
# DESCRIPTION
54+
55+
{app_name} is a tool that converts TOML into JSON. It operates
56+
on standard input and writes to standard output.
57+
58+
# OPTIONS
59+
60+
-help
61+
: display help
62+
63+
-license
64+
: display license
65+
66+
-version
67+
: display version
68+
69+
-nl, -newline
70+
: if true add a trailing newline
71+
72+
-o, -output
73+
: output filename
74+
75+
-p, -pretty
76+
: pretty print output
77+
78+
-quiet
79+
: suppress error messages
80+
81+
82+
# EXAMPLES
4083
41-
examples = `
4284
These would get the file named "my.toml" and save it as my.json
4385
44-
%s my.toml > my.json
86+
~~~
87+
{app_name} my.toml > my.json
4588
46-
%s my.toml my.json
89+
{app_name} my.toml my.json
4790
48-
cat my.toml | %s -i - > my.json
91+
cat my.toml | {app_name} -i - > my.json
92+
~~~
93+
94+
{app_name} {version}
4995
`
5096

5197
// Standard Options
@@ -65,6 +111,10 @@ These would get the file named "my.toml" and save it as my.json
65111
prettyPrint bool
66112
)
67113

114+
func fmtTxt(src string, appName string, version string) string {
115+
return strings.ReplaceAll(strings.ReplaceAll(src, "{app_name}", appName), "{version}", version)
116+
}
117+
68118
func toml2JSON(in io.Reader, out io.Writer, printPrint bool) error {
69119
var (
70120
src []byte
@@ -85,35 +135,27 @@ func toml2JSON(in io.Reader, out io.Writer, printPrint bool) error {
85135
}
86136

87137
func main() {
88-
app := cli.NewCli(datatools.Version)
89-
appName := app.AppName()
90-
91-
// Add Help Docs
92-
app.AddHelp("license", []byte(fmt.Sprintf(datatools.LicenseText, appName, datatools.Version)))
93-
app.AddHelp("description", []byte(fmt.Sprintf(description, appName)))
94-
app.AddHelp("examples", []byte(fmt.Sprintf(examples, appName, appName, appName)))
95-
96-
// Document non-option parameters
97-
app.SetParams("[TOML_FILENAME]", "[JSON_NAME]")
138+
appName := path.Base(os.Args[0])
98139

99140
// Standard Options
100-
app.BoolVar(&showHelp, "h,help", false, "display help")
101-
app.BoolVar(&showLicense, "l,license", false, "display license")
102-
app.BoolVar(&showVersion, "v,version", false, "display version")
103-
app.BoolVar(&showExamples, "examples", false, "display example(s)")
104-
//app.StringVar(&inputFName, "i,input", "", "input filename")
105-
app.StringVar(&outputFName, "o,output", "", "output filename")
106-
app.BoolVar(&quiet, "quiet", false, "suppress error messages")
107-
app.BoolVar(&generateMarkdown, "generate-markdown", false, "generate markdown documentation")
108-
app.BoolVar(&generateManPage, "generate-manpage", false, "generate man page")
109-
app.BoolVar(&newLine, "nl,newline", false, "if true add a trailing newline")
141+
flag.BoolVar(&showHelp, "help", false, "display help")
142+
flag.BoolVar(&showLicense, "license", false, "display license")
143+
flag.BoolVar(&showVersion, "version", false, "display version")
144+
145+
flag.StringVar(&outputFName, "o", "", "output filename")
146+
flag.StringVar(&outputFName, "output", "", "output filename")
147+
flag.BoolVar(&quiet, "quiet", false, "suppress error messages")
148+
149+
flag.BoolVar(&newLine, "nl", false, "if true add a trailing newline")
150+
flag.BoolVar(&newLine, "newline", false, "if true add a trailing newline")
110151

111152
// App Specific Options
112-
app.BoolVar(&prettyPrint, "p,pretty", false, "pretty print output")
153+
flag.BoolVar(&prettyPrint, "p", false, "pretty print output")
154+
flag.BoolVar(&prettyPrint, "pretty", false, "pretty print output")
113155

114156
// Parse env and options
115-
app.Parse()
116-
args := app.Args()
157+
flag.Parse()
158+
args := flag.Args()
117159

118160
// Handle case of input/output filenames provided without -i, -o
119161
if len(args) > 0 {
@@ -125,46 +167,51 @@ func main() {
125167

126168
// Setup IO
127169
var err error
128-
app.Eout = os.Stderr
129170

130-
app.In, err = cli.Open(inputFName, os.Stdin)
131-
cli.ExitOnError(app.Eout, err, quiet)
132-
defer cli.CloseFile(inputFName, app.In)
171+
in := os.Stdin
172+
out := os.Stdout
173+
eout := os.Stderr
133174

134-
app.Out, err = cli.Create(outputFName, os.Stdout)
135-
cli.ExitOnError(app.Eout, err, quiet)
136-
defer cli.CloseFile(outputFName, app.Out)
137175

138-
// Process options
139-
if generateMarkdown {
140-
app.GenerateMarkdown(app.Out)
141-
os.Exit(0)
142-
}
143-
if generateManPage {
144-
app.GenerateManPage(app.Out)
145-
os.Exit(0)
176+
if inputFName != "" {
177+
in, err = os.Open(inputFName)
178+
if err != nil {
179+
fmt.Fprintln(eout, err)
180+
os.Exit(1)
181+
}
182+
defer in.Close()
146183
}
147-
if showHelp || showExamples {
148-
if len(args) > 0 {
149-
fmt.Fprintln(app.Out, app.Help(args...))
150-
} else {
151-
app.Usage(app.Out)
184+
185+
if outputFName != "" {
186+
out, err = os.Create(outputFName)
187+
if err != nil {
188+
fmt.Fprintln(eout, err)
189+
os.Exit(1)
152190
}
191+
defer out.Close()
192+
}
193+
194+
// Process options
195+
if showHelp {
196+
fmt.Fprintf(out, "%s\n", fmtTxt(helpText, appName, datatools.Version))
153197
os.Exit(0)
154198
}
155199
if showLicense {
156-
fmt.Fprintln(app.Out, app.License())
200+
fmt.Fprintf(out, "%s\n", datatools.LicenseText)
157201
os.Exit(0)
158202
}
159203
if showVersion {
160-
fmt.Fprintln(app.Out, app.Version())
204+
fmt.Fprintf(out, "%s %s\n", appName, datatools.Version)
161205
os.Exit(0)
162206
}
163207
if newLine {
164208
eol = "\n"
165209
}
166210

167-
err = toml2JSON(app.In, app.Out, prettyPrint)
168-
cli.ExitOnError(app.Eout, err, quiet)
169-
fmt.Fprintf(app.Out, "%s", eol)
211+
err = toml2JSON(in, out, prettyPrint)
212+
if err != nil {
213+
fmt.Fprintln(eout, err)
214+
os.Exit(1)
215+
}
216+
fmt.Fprintf(out, "%s", eol)
170217
}

toml2json.1.md

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,56 @@
1+
---
2+
title: "{app_name} (1) user manual"
3+
author: "R. S. Doiel"
4+
pubDate: 2023-01-09
5+
---
16

2-
USAGE: toml2json [OPTIONS] [TOML_FILENAME] [JSON_NAME]
7+
# NAME
8+
9+
{app_name}
310

4-
DESCRIPTION
11+
# SYNOPSIS
512

6-
toml2json is a tool that converts TOML into JSON output.
13+
{app_name} [OPTIONS] [TOML_FILENAME] [JSON_NAME]
714

8-
OPTIONS
15+
# DESCRIPTION
916

10-
-examples display example(s)
11-
-generate-manpage generate man page
12-
-generate-markdown generate markdown documentation
13-
-h, -help display help
14-
-l, -license display license
15-
-nl, -newline if true add a trailing newline
16-
-o, -output output filename
17-
-p, -pretty pretty print output
18-
-quiet suppress error messages
19-
-v, -version display version
17+
{app_name} is a tool that converts TOML into JSON. It operates
18+
on standard input and writes to standard output.
2019

20+
# OPTIONS
2121

22-
EXAMPLES
22+
-help
23+
: display help
24+
25+
-license
26+
: display license
27+
28+
-version
29+
: display version
30+
31+
-nl, -newline
32+
: if true add a trailing newline
33+
34+
-o, -output
35+
: output filename
36+
37+
-p, -pretty
38+
: pretty print output
39+
40+
-quiet
41+
: suppress error messages
42+
43+
44+
# EXAMPLES
2345

2446
These would get the file named "my.toml" and save it as my.json
2547

26-
toml2json my.toml > my.json
48+
~~~
49+
{app_name} my.toml > my.json
2750
28-
toml2json my.toml my.json
51+
{app_name} my.toml my.json
2952
30-
cat my.toml | toml2json -i - > my.json
53+
cat my.toml | {app_name} -i - > my.json
54+
~~~
3155

32-
toml2json 1.2.1
56+
{app_name} {version}

0 commit comments

Comments
 (0)