@@ -21,45 +21,99 @@ package main
2121
2222import (
2323 "encoding/json"
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 Workbook Sheets into
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 Workbook Sheets into
4056JSON output.
41- `
4257
43- examples = `
58+ # OPTIONS
59+
60+ -help
61+ : display help
62+
63+ -license
64+ : display license
65+
66+ -version
67+ : display version
68+
69+ -N, -sheets
70+ : display sheet names in Excel Workbook
71+
72+ -c, -count
73+ : display number of sheets in Excel Workbook
74+
75+ -nl, -newline
76+ : if true add a trailing newline
77+
78+ -o, -output
79+ : output filename
80+
81+ -quiet
82+ : suppress error messages
83+
84+
85+ # EXAMPLES
86+
4487This would get the sheet named "Sheet 1" from "MyWorkbook.xlsx" and save as sheet1.json
4588
46- %s MyWorkbook.xlsx "My worksheet 1" > sheet1.json
89+ ~~~
90+ {app_name} MyWorkbook.xlsx "My worksheet 1" > sheet1.json
91+ ~~~
4792
4893This would get the number of sheets in the workbook
4994
50- %s -count MyWorkbook.xlsx
95+ ~~~
96+ {app_name} -count MyWorkbook.xlsx
97+ ~~~
5198
5299This will output the title of the sheets in the workbook
53100
54- %s -sheets MyWorkbook.xlsx
101+ ~~~
102+ {app_name} -sheets MyWorkbook.xlsx
103+ ~~~
55104
56105Putting it all together in a shell script and convert all sheets to
57106into JSON documents..
58107
59- %s -N MyWorkbook.xlsx | while read SHEET_NAME; do
108+ ~~~
109+ {app_name} -N MyWorkbook.xlsx | while read SHEET_NAME; do
60110 JSON_NAME="${SHEET_NAME// /-}.json"
61- %s -o "${JSON_NAME}" MyWorkbook.xlsx "$SHEET_NAME"
62- done
111+ {app_name} -o "${JSON_NAME}" MyWorkbook.xlsx "$SHEET_NAME"
112+ done
113+ ~~~
114+
115+ {app_name} {version}
116+
63117`
64118
65119 // Standard Options
@@ -80,6 +134,10 @@ into JSON documents..
80134 showSheetNames bool
81135)
82136
137+ func fmtTxt (src string , appName string , version string ) string {
138+ return strings .ReplaceAll (strings .ReplaceAll (src , "{app_name}" , appName ), "{version}" , version )
139+ }
140+
83141func sheetCount (workBookName string ) (int , error ) {
84142 xlFile , err := xlsx .OpenFile (workBookName )
85143 if err != nil {
@@ -127,107 +185,102 @@ func xlsx2JSON(out io.Writer, workBookName, sheetName string) error {
127185}
128186
129187func main () {
130- app := cli .NewCli (datatools .Version )
131- appName := app .AppName ()
188+ appName := path .Base (os .Args [0 ])
132189
133- // Add Help Docs
134- app . AddHelp ( "license " , [] byte ( fmt . Sprintf ( datatools . LicenseText , appName , datatools . Version )) )
135- app . AddHelp ( "description " , [] byte ( fmt . Sprintf ( description , appName )) )
136- app . AddHelp ( "examples " , [] byte ( fmt . Sprintf ( examples , appName , appName , appName , appName , appName )) )
190+ // Standard Options
191+ flag . BoolVar ( & showHelp , "help " , false , "display help" )
192+ flag . BoolVar ( & showLicense , "license " , false , "display license" )
193+ flag . BoolVar ( & showVersion , "version " , false , "display version" )
137194
138- // Document non-option parameters
139- app .SetParams ("EXCEL_WORKBOOK_NAME" , "[SHEET_NAME]" )
195+ flag .StringVar (& outputFName , "o" , "" , "output filename" )
196+ flag .StringVar (& outputFName , "output" , "" , "output filename" )
197+ flag .BoolVar (& quiet , "quiet" , false , "suppress error messages" )
198+
140199
141- // Standard Options
142- app .BoolVar (& showHelp , "h,help" , false , "display help" )
143- app .BoolVar (& showLicense , "l,license" , false , "display license" )
144- app .BoolVar (& showVersion , "v,version" , false , "display version" )
145- app .BoolVar (& showExamples , "examples" , false , "display example(s)" )
146- //app.StringVar(&inputFName, "i,input", "", "input filename")
147- app .StringVar (& outputFName , "o,output" , "" , "output filename" )
148- app .BoolVar (& quiet , "quiet" , false , "suppress error messages" )
149- app .BoolVar (& generateMarkdown , "generate-markdown" , false , "generate markdown documentation" )
150- app .BoolVar (& generateManPage , "generate-manpage" , false , "generate man page" )
151- app .BoolVar (& newLine , "nl,newline" , false , "if true add a trailing newline" )
200+ flag .BoolVar (& newLine , "nl" , false , "if true add a trailing newline" )
201+ flag .BoolVar (& newLine , "newline" , false , "if true add a trailing newline" )
152202
153203 // App Specific Options
154- app .BoolVar (& showSheetCount , "c,count" , false , "display number of sheets in Excel Workbook" )
155- app .BoolVar (& showSheetNames , "N,sheets" , false , "display sheet names in Excel Workbook" )
204+ flag .BoolVar (& showSheetCount , "c" , false , "display number of sheets in Excel Workbook" )
205+ flag .BoolVar (& showSheetCount , "count" , false , "display number of sheets in Excel Workbook" )
206+ flag .BoolVar (& showSheetNames , "N" , false , "display sheet names in Excel Workbook" )
207+ flag .BoolVar (& showSheetNames , "sheets" , false , "display sheet names in Excel Workbook" )
156208
157209 // Parse env and options
158- app .Parse ()
159- args := app .Args ()
210+ flag .Parse ()
211+ args := flag .Args ()
160212
161213 // Setup IO
162214 var err error
163- app .Eout = os .Stderr
164215
165- /* NOTE: this command does not read from stdin
166- app.In, err = cli.Open(inputFName, os.Stdin)
167- cli.ExitOnError(app.Eout, err, quiet)
168- defer cli.CloseFile(inputFName, app.In)
169- */
216+ out := os .Stdout
217+ eout := os .Stderr
218+
219+ if outputFName != "" {
220+ out , err = os .Create (outputFName )
221+ if err != nil {
222+ fmt .Fprintln (eout , err )
223+ os .Exit (1 )
224+ }
225+ defer out .Close ()
226+ }
170227
171- app .Out , err = cli .Create (outputFName , os .Stdout )
172- cli .ExitOnError (app .Eout , err , quiet )
173- defer cli .CloseFile (outputFName , app .Out )
174228
175229 // Process options
176- if generateMarkdown {
177- app .GenerateMarkdown (app .Out )
178- os .Exit (0 )
179- }
180- if generateManPage {
181- app .GenerateManPage (app .Out )
182- os .Exit (0 )
183- }
184- if showHelp || showExamples {
185- if len (args ) > 0 {
186- fmt .Fprintln (app .Out , app .Help (args ... ))
187- } else {
188- app .Usage (app .Out )
189- }
230+ if showHelp {
231+ fmt .Fprintf (out , "%s\n " , fmtTxt (helpText , appName , datatools .Version ))
190232 os .Exit (0 )
191233 }
192234 if showLicense {
193- fmt .Fprintln ( app . Out , app . License () )
235+ fmt .Fprintf ( out , "%s \n " , datatools . LicenseText )
194236 os .Exit (0 )
195237 }
196238 if showVersion {
197- fmt .Fprintln ( app . Out , app .Version () )
239+ fmt .Fprintf ( out , "%s %s \n " , appName , datatools .Version )
198240 os .Exit (0 )
199241 }
200242 if newLine {
201243 eol = "\n "
202244 }
203245
204246 if len (args ) < 1 {
205- cli .ExitOnError (app .Eout , fmt .Errorf ("Missing Excel Workbook names" ), quiet )
247+ fmt .Fprintln (eout , "Missing Excel Workbook names" )
248+ os .Exit (1 )
206249 }
207250
208251 workBookName := args [0 ]
209252 if showSheetCount == true {
210253 cnt , err := sheetCount (workBookName )
211- cli .ExitOnError (app .Eout , err , quiet )
212- fmt .Fprintf (app .Out , "%d%s" , cnt , eol )
254+ if err != nil {
255+ fmt .Fprintln (eout , err )
256+ os .Exit (1 )
257+ }
258+ fmt .Fprintf (out , "%d%s" , cnt , eol )
213259 os .Exit (0 )
214260 }
215261
216262 if showSheetNames == true {
217263 names , err := sheetNames (workBookName )
218- cli .ExitOnError (app .Eout , err , quiet )
219- fmt .Fprintf (app .Out , "%s%s" , strings .Join (names , "\n " ), eol )
264+ if err != nil {
265+ fmt .Fprintln (eout , err )
266+ os .Exit (1 )
267+ }
268+ fmt .Fprintf (out , "%s%s" , strings .Join (names , "\n " ), eol )
220269 os .Exit (0 )
221270 }
222271
223272 if len (args ) < 2 {
224- cli .ExitOnError (app .Eout , fmt .Errorf ("Missing worksheet name" ), quiet )
273+ fmt .Fprintln (eout , "Missing worksheet name" )
274+ os .Exit (1 )
225275 }
226276 for _ , sheetName := range args [1 :] {
227277 if len (sheetName ) > 0 {
228- err := xlsx2JSON (app .Out , workBookName , sheetName )
229- cli .ExitOnError (app .Eout , err , quiet )
278+ err := xlsx2JSON (out , workBookName , sheetName )
279+ if err != nil {
280+ fmt .Fprintln (eout , err )
281+ os .Exit (1 )
282+ }
230283 }
231284 }
232- fmt .Fprintf (app . Out , "%s" , eol )
285+ fmt .Fprintf (out , "%s" , eol )
233286}
0 commit comments