@@ -21,52 +21,107 @@ package main
2121
2222import (
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
3738var (
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 = `
4386Extract 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
4792This 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
5199This will output the number of sheets in the Workbook.
52100
53- %s -sheets MyWorkbook.xlsx
101+ ~~~
102+ {app_name} -sheets MyWorkbook.xlsx
103+ ~~~
54104
55105This will display a list of sheet names, one per line.
56106Putting 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+
82141func 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
133192func 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}
0 commit comments