Skip to content

Commit 1bcbee0

Browse files
committed
migrated from package cli to flag for xlsx2csv
1 parent c7cd92e commit 1bcbee0

File tree

2 files changed

+174
-89
lines changed

2 files changed

+174
-89
lines changed

cmd/xlsx2csv/xlsx2csv.go

Lines changed: 119 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -21,52 +21,107 @@ package main
2121

2222
import (
2323
"encoding/csv"
24+
"flag"
2425
"fmt"
2526
"io"
2627
"os"
28+
"path"
2729
"strings"
2830

2931
// CaltechLibrary packages
30-
"github.com/caltechlibrary/cli"
3132
"github.com/caltechlibrary/datatools"
3233

3334
// 3rd Party packages
3435
"github.com/tealeg/xlsx"
3536
)
3637

3738
var (
38-
description = `
39-
%s is a tool that converts individual Excel Sheets to CSV output.
40-
`
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] EXCEL_WORKBOOK_NAME [SHEET_NAME]
52+
53+
# DESCRIPTION
54+
55+
{app_name} is a tool that converts individual Excel Sheets to CSV output.
56+
57+
# OPTIONS
58+
59+
-help
60+
: display help
61+
62+
-license
63+
: display license
64+
65+
-version
66+
: display version
67+
68+
-N, -sheets
69+
: display the Workbook sheet names
70+
71+
-c, -count
72+
: display number of Workbook sheets
73+
74+
-nl, -newline
75+
: if true add a trailing newline
76+
77+
-o, -output
78+
: output filename
79+
80+
-quiet
81+
: suppress error messages
82+
83+
84+
# EXAMPLES
4185
42-
examples = `
4386
Extract a workbook sheet as a CSV file
4487
45-
%s MyWorkbook.xlsx "My worksheet 1" > sheet1.csv
88+
~~~
89+
{app_name} MyWorkbook.xlsx "My worksheet 1" > sheet1.csv
90+
~~~
4691
4792
This would get the first sheet from the workbook and save it as a CSV file.
4893
49-
%s -count MyWorkbook.xlsx
94+
~~~
95+
{app_name} -count MyWorkbook.xlsx
96+
~~~
97+
5098
5199
This will output the number of sheets in the Workbook.
52100
53-
%s -sheets MyWorkbook.xlsx
101+
~~~
102+
{app_name} -sheets MyWorkbook.xlsx
103+
~~~
54104
55105
This will display a list of sheet names, one per line.
56106
Putting it all together in a shell script.
57107
58-
%s -N MyWorkbook.xlsx | while read SHEET_NAME; do
108+
~~~
109+
{app_name} -N MyWorkbook.xlsx | while read SHEET_NAME; do
59110
CSV_NAME="${SHEET_NAME// /-}.csv"
60-
%s -o "${CSV_NAME}" MyWorkbook.xlsx "${SHEET_NAME}"
111+
{app_name} -o "${CSV_NAME}" MyWorkbook.xlsx "${SHEET_NAME}"
61112
done
113+
~~~
114+
115+
{app_name} {version}
116+
62117
`
63118

64119
// Standard Options
65120
showHelp bool
66121
showLicense bool
67122
showVersion bool
68123
showExamples bool
69-
//inputFName string
124+
70125
outputFName string
71126
generateMarkdown bool
72127
generateManPage bool
@@ -79,6 +134,10 @@ Putting it all together in a shell script.
79134
showSheetNames bool
80135
)
81136

137+
func fmtTxt(src string, appName string, version string) string {
138+
return strings.ReplaceAll(strings.ReplaceAll(src, "{app_name}", appName), "{version}", version)
139+
}
140+
82141
func sheetCount(workBookName string) (int, error) {
83142
xlFile, err := xlsx.OpenFile(workBookName)
84143
if err != nil {
@@ -131,107 +190,100 @@ func xlsx2CSV(out io.Writer, workBookName, sheetName string) error {
131190
}
132191

133192
func main() {
134-
app := cli.NewCli(datatools.Version)
135-
appName := app.AppName()
193+
appName := path.Base(os.Args[0])
136194

137-
// Add Help Docs
138-
app.AddHelp("license", []byte(fmt.Sprintf(datatools.LicenseText, appName, datatools.Version)))
139-
app.AddHelp("description", []byte(fmt.Sprintf(description, appName)))
140-
app.AddHelp("examples", []byte(fmt.Sprintf(examples, appName, appName, appName, appName, appName)))
195+
// Standard Options
196+
flag.BoolVar(&showHelp, "help", false, "display help")
197+
flag.BoolVar(&showLicense, "license", false, "display license")
198+
flag.BoolVar(&showVersion, "version", false, "display version")
141199

142-
// Document non-option parameters
143-
app.SetParams("EXCEL_WORKBOOK_NAME", "[SHEET_NAME]")
200+
flag.StringVar(&outputFName, "o", "", "output filename")
201+
flag.StringVar(&outputFName, "output", "", "output filename")
202+
flag.BoolVar(&quiet, "quiet", false, "suppress error messages")
203+
144204

145-
// Standard Options
146-
app.BoolVar(&showHelp, "h,help", false, "display help")
147-
app.BoolVar(&showLicense, "l,license", false, "display license")
148-
app.BoolVar(&showVersion, "v,version", false, "display version")
149-
app.BoolVar(&showExamples, "examples", false, "display example(s)")
150-
//app.StringVar(&inputFName, "i,input", "", "input filename")
151-
app.StringVar(&outputFName, "o,output", "", "output filename")
152-
app.BoolVar(&quiet, "quiet", false, "suppress error messages")
153-
app.BoolVar(&generateMarkdown, "generate-markdown", false, "generate markdown documentation")
154-
app.BoolVar(&generateManPage, "generate-manpage", false, "generate man page")
155-
app.BoolVar(&newLine, "nl,newline", false, "if true add a trailing newline")
205+
flag.BoolVar(&newLine, "nl", false, "if true add a trailing newline")
206+
flag.BoolVar(&newLine, "newline", false, "if true add a trailing newline")
156207

157208
// App Specific Options
158-
app.BoolVar(&showSheetCount, "c,count", false, "display number of Workbook sheets")
159-
app.BoolVar(&showSheetNames, "N,sheets", false, "display the Workbook sheet names")
209+
flag.BoolVar(&showSheetCount, "c", false, "display number of Workbook sheets")
210+
flag.BoolVar(&showSheetCount, "count", false, "display number of Workbook sheets")
211+
flag.BoolVar(&showSheetNames, "N", false, "display the Workbook sheet names")
212+
flag.BoolVar(&showSheetNames, "sheets", false, "display the Workbook sheet names")
160213

161214
// Parse env and options
162-
app.Parse()
163-
args := app.Args()
215+
flag.Parse()
216+
args := flag.Args()
164217

165218
// Setup IO
166219
var err error
167-
app.Eout = os.Stderr
168220

169-
/* NOTE: this command does not read from stdin
170-
app.In, err = cli.Open(inputFName, os.Stdin)
171-
cli.ExitOnError(app.Eout, err, quiet)
172-
defer cli.CloseFile(inputFName, app.In)
173-
*/
221+
out := os.Stdout
222+
eout := os.Stderr
174223

175-
app.Out, err = cli.Create(outputFName, os.Stdout)
176-
cli.ExitOnError(app.Eout, err, quiet)
177-
defer cli.CloseFile(outputFName, app.Out)
224+
if outputFName != "" {
225+
out, err = os.Create(outputFName)
226+
if err != nil {
227+
fmt.Fprintln(eout, err)
228+
os.Exit(1)
229+
}
230+
defer out.Close()
178231

179-
// Process options
180-
if generateMarkdown {
181-
app.GenerateMarkdown(app.Out)
182-
os.Exit(0)
183-
}
184-
if generateManPage {
185-
app.GenerateManPage(app.Out)
186-
os.Exit(0)
187232
}
188233

189-
if showHelp || showExamples {
190-
if len(args) > 0 {
191-
fmt.Fprintln(app.Out, app.Help(args...))
192-
} else {
193-
app.Usage(app.Out)
194-
}
234+
// Process options
235+
if showHelp {
236+
fmt.Fprintf(out, "%s\n", fmtTxt(helpText, appName, datatools.Version))
195237
os.Exit(0)
196238
}
197239
if showLicense {
198-
fmt.Fprintln(app.Out, app.License())
240+
fmt.Fprintf(out, "%s\n", datatools.LicenseText)
199241
os.Exit(0)
200242
}
201243
if showVersion {
202-
fmt.Fprintln(app.Out, app.Version())
244+
fmt.Fprintf(out, "%s %s\n", appName, datatools.Version)
203245
os.Exit(0)
204246
}
205247
if newLine {
206248
eol = "\n"
207249
}
208250

209251
if len(args) < 1 {
210-
cli.ExitOnError(app.Eout, fmt.Errorf("Missing Excel Workbook names"), quiet)
252+
fmt.Fprintln(eout, "Missing Excel Workbook names")
253+
os.Exit(1)
211254
}
212255

213256
workBookName := args[0]
214257
if showSheetCount == true {
215258
cnt, err := sheetCount(workBookName)
216-
cli.ExitOnError(app.Eout, err, quiet)
217-
fmt.Fprintf(app.Out, "%d%s", cnt, eol)
259+
if err != nil {
260+
fmt.Fprintln(eout, err)
261+
os.Exit(1)
262+
}
263+
fmt.Fprintf(out, "%d%s", cnt, eol)
218264
os.Exit(0)
219265
}
220266

221267
if showSheetNames == true {
222268
names, err := sheetNames(workBookName)
223-
cli.ExitOnError(app.Eout, err, quiet)
224-
fmt.Fprintf(app.Out, "%s%s", strings.Join(names, "\n"), eol)
269+
if err != nil {
270+
fmt.Fprintln(eout, err)
271+
os.Exit(1)
272+
}
273+
fmt.Fprintf(out, "%s%s", strings.Join(names, "\n"), eol)
225274
os.Exit(0)
226275
}
227276

228277
if len(args) < 2 {
229-
cli.ExitOnError(app.Eout, fmt.Errorf("Missing worksheet name"), quiet)
278+
if err != nil {
279+
fmt.Fprintln(eout, err)
280+
os.Exit(1)
281+
}
230282
}
231283
for _, sheetName := range args[1:] {
232284
if len(sheetName) > 0 {
233-
xlsx2CSV(app.Out, workBookName, sheetName)
285+
xlsx2CSV(out, workBookName, sheetName)
234286
}
235287
}
236-
fmt.Fprintf(app.Out, "%s", eol)
288+
fmt.Fprintf(out, "%s", eol)
237289
}

xlsx2csv.1.md

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

2-
USAGE: xlsx2csv [OPTIONS] EXCEL_WORKBOOK_NAME [SHEET_NAME]
7+
# NAME
38

4-
DESCRIPTION
9+
{app_name}
510

6-
xlsx2csv is a tool that converts individual Excel Sheets to CSV output.
11+
# SYNOPSIS
712

8-
OPTIONS
13+
{app_name} [OPTIONS] EXCEL_WORKBOOK_NAME [SHEET_NAME]
914

10-
-N, -sheets display the Workbook sheet names
11-
-c, -count display number of Workbook sheets
12-
-examples display example(s)
13-
-generate-manpage generate man page
14-
-generate-markdown generate markdown documentation
15-
-h, -help display help
16-
-l, -license display license
17-
-nl, -newline if true add a trailing newline
18-
-o, -output output filename
19-
-quiet suppress error messages
20-
-v, -version display version
15+
# DESCRIPTION
2116

17+
{app_name} is a tool that converts individual Excel Sheets to CSV output.
2218

23-
EXAMPLES
19+
# OPTIONS
20+
21+
-help
22+
: display help
23+
24+
-license
25+
: display license
26+
27+
-version
28+
: display version
29+
30+
-N, -sheets
31+
: display the Workbook sheet names
32+
33+
-c, -count
34+
: display number of Workbook sheets
35+
36+
-nl, -newline
37+
: if true add a trailing newline
38+
39+
-o, -output
40+
: output filename
41+
42+
-quiet
43+
: suppress error messages
44+
45+
46+
# EXAMPLES
2447

2548
Extract a workbook sheet as a CSV file
2649

27-
xlsx2csv MyWorkbook.xlsx "My worksheet 1" > sheet1.csv
50+
~~~
51+
{app_name} MyWorkbook.xlsx "My worksheet 1" > sheet1.csv
52+
~~~
2853

2954
This would get the first sheet from the workbook and save it as a CSV file.
3055

31-
xlsx2csv -count MyWorkbook.xlsx
56+
~~~
57+
{app_name} -count MyWorkbook.xlsx
58+
~~~
59+
3260

3361
This will output the number of sheets in the Workbook.
3462

35-
xlsx2csv -sheets MyWorkbook.xlsx
63+
~~~
64+
{app_name} -sheets MyWorkbook.xlsx
65+
~~~
3666

3767
This will display a list of sheet names, one per line.
3868
Putting it all together in a shell script.
3969

40-
xlsx2csv -N MyWorkbook.xlsx | while read SHEET_NAME; do
70+
~~~
71+
{app_name} -N MyWorkbook.xlsx | while read SHEET_NAME; do
4172
CSV_NAME="${SHEET_NAME// /-}.csv"
42-
xlsx2csv -o "${CSV_NAME}" MyWorkbook.xlsx "${SHEET_NAME}"
73+
{app_name} -o "${CSV_NAME}" MyWorkbook.xlsx "${SHEET_NAME}"
4374
done
75+
~~~
76+
77+
{app_name} {version}
4478

45-
xlsx2csv 1.2.1

0 commit comments

Comments
 (0)