Skip to content

Commit fe29599

Browse files
committed
improving cli handling
1 parent 4ba2b0e commit fe29599

File tree

21 files changed

+590
-297
lines changed

21 files changed

+590
-297
lines changed

cmds/csv2json/csv2json.go

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,29 @@ import (
4040
var (
4141
usage = `USAGE: %s [OPTIONS]`
4242

43-
description = `
44-
SYNOPSIS
43+
description = `SYNOPSIS
4544
4645
%s reads CSV from stdin and writes a JSON to stdout. JSON output
4746
can be either an array of JSON blobs or one JSON blob (row as object)
48-
per line.
49-
`
47+
per line.`
5048

51-
examples = `
52-
EXAMPLES
49+
examples = `EXAMPLES
5350
5451
Convert data1.csv to data1.json using Unix pipes.
5552
5653
cat data1.csv | %s > data1.json
5754
5855
Convert data1.csv to JSON blobs, one line per blob
5956
60-
%s -as-blobs -i data1.csv
61-
`
57+
%s -as-blobs -i data1.csv`
6258

6359
// Standard Options
64-
showHelp bool
65-
showLicense bool
66-
showVersion bool
67-
inputFName string
68-
outputFName string
60+
showHelp bool
61+
showLicense bool
62+
showVersion bool
63+
showExamples bool
64+
inputFName string
65+
outputFName string
6966

7067
// Application Options
7168
useHeader bool
@@ -81,6 +78,7 @@ func init() {
8178
flag.BoolVar(&showLicense, "license", false, "display license")
8279
flag.BoolVar(&showVersion, "v", false, "display version")
8380
flag.BoolVar(&showVersion, "version", false, "display version")
81+
flag.BoolVar(&showExamples, "example", false, "display example(s)")
8482
flag.StringVar(&inputFName, "i", "", "input filename")
8583
flag.StringVar(&inputFName, "input", "", "input filename")
8684
flag.StringVar(&outputFName, "o", "", "output filename")
@@ -96,15 +94,31 @@ func init() {
9694
func main() {
9795
appName := path.Base(os.Args[0])
9896
flag.Parse()
97+
args := flag.Args()
9998

10099
// Configuration and command line interation
101-
cfg := cli.New(appName, appName, fmt.Sprintf(datatools.LicenseText, appName, datatools.Version), datatools.Version)
100+
cfg := cli.New(appName, strings.ToUpper(appName), datatools.Version)
101+
cfg.LicenseText = fmt.Sprintf(datatools.LicenseText, appName, datatools.Version)
102102
cfg.UsageText = fmt.Sprintf(usage, appName)
103103
cfg.DescriptionText = fmt.Sprintf(description, appName)
104+
cfg.OptionText = "OPTIONS"
104105
cfg.ExampleText = fmt.Sprintf(examples, appName, appName)
105106

106107
if showHelp == true {
107-
fmt.Println(cfg.Usage())
108+
if len(args) > 0 {
109+
fmt.Println(cfg.Help(args...))
110+
} else {
111+
fmt.Println(cfg.Usage())
112+
}
113+
os.Exit(0)
114+
}
115+
116+
if showExamples == true {
117+
if len(args) > 0 {
118+
fmt.Println(cfg.Example(args...))
119+
} else {
120+
fmt.Println(cfg.ExampleText)
121+
}
108122
os.Exit(0)
109123
}
110124

cmds/csv2mdtable/csv2mdtable.go

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,28 @@ import (
3636
var (
3737
usage = `USAGE: %s [OPTIONS]`
3838

39-
description = `
40-
SYNOPSIS
39+
description = `SYNOPSIS
4140
4241
%s reads CSV from stdin and writes a Github Flavored Markdown
43-
table to stdout.
44-
`
42+
table to stdout.`
4543

46-
examples = `
47-
EXAMPLES
44+
examples = `EXAMPLES
4845
4946
Convert data1.csv to data1.md using Unix pipes.
5047
5148
cat data1.csv | %s > data1.md
5249
5350
Convert data1.csv to data1.md using options.
5451
55-
%s -i data1.csv -o data1.md
56-
`
52+
%s -i data1.csv -o data1.md`
5753

5854
// Standard Options
59-
showHelp bool
60-
showLicense bool
61-
showVersion bool
62-
inputFName string
63-
outputFName string
55+
showHelp bool
56+
showLicense bool
57+
showVersion bool
58+
showExamples bool
59+
inputFName string
60+
outputFName string
6461

6562
// Application Options
6663
delimiter string
@@ -74,6 +71,7 @@ func init() {
7471
flag.BoolVar(&showLicense, "license", false, "display license")
7572
flag.BoolVar(&showVersion, "v", false, "display version")
7673
flag.BoolVar(&showVersion, "version", false, "display version")
74+
flag.BoolVar(&showExamples, "example", false, "display example(s)")
7775
flag.StringVar(&inputFName, "i", "", "input filename")
7876
flag.StringVar(&inputFName, "input", "", "input filename")
7977
flag.StringVar(&outputFName, "o", "", "output filename")
@@ -87,15 +85,31 @@ func init() {
8785
func main() {
8886
appName := path.Base(os.Args[0])
8987
flag.Parse()
88+
args := flag.Args()
9089

9190
// Configuration and command line interation
92-
cfg := cli.New(appName, appName, fmt.Sprintf(datatools.LicenseText, appName, datatools.Version), datatools.Version)
91+
cfg := cli.New(appName, strings.ToUpper(appName), datatools.Version)
92+
cfg.LicenseText = fmt.Sprintf(datatools.LicenseText, appName, datatools.Version)
9393
cfg.UsageText = fmt.Sprintf(usage, appName)
9494
cfg.DescriptionText = fmt.Sprintf(description, appName)
95+
cfg.OptionText = "OPTIONS"
9596
cfg.ExampleText = fmt.Sprintf(examples, appName, appName)
9697

9798
if showHelp == true {
98-
fmt.Println(cfg.Usage())
99+
if len(args) > 0 {
100+
fmt.Println(cfg.Help(args...))
101+
} else {
102+
fmt.Println(cfg.Usage())
103+
}
104+
os.Exit(0)
105+
}
106+
107+
if showExamples == true {
108+
if len(args) > 0 {
109+
fmt.Println(cfg.Example(args...))
110+
} else {
111+
fmt.Println(cfg.ExampleText)
112+
}
99113
os.Exit(0)
100114
}
101115

cmds/csv2xlsx/csv2xlsx.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"io"
2727
"os"
2828
"path"
29+
"strings"
2930

3031
// CaltechLibrary packages
3132
"github.com/caltechlibrary/cli"
@@ -38,15 +39,12 @@ import (
3839
var (
3940
usage = `USAGE: %s [OPTIONS] WORKBOOK_NAME SHEET_NAME`
4041

41-
description = `
42-
SYNOPSIS
42+
description = `SYNOPSIS
4343
4444
%s will take CSV input and create a new sheet in an Excel Workbook.
45-
If the Workbook does not exist then it is created.
46-
`
45+
If the Workbook does not exist then it is created.`
4746

48-
examples = `
49-
EXAMPLE
47+
examples = `EXAMPLE
5048
5149
%s -i data.csv MyWorkbook.xlsx 'My worksheet'
5250
@@ -56,14 +54,14 @@ called 'MyWorkbook.xlsx' with the contents of data.csv.
5654
cat data.csv | %s MyWorkbook.xlsx 'My worksheet'
5755
5856
This does the same but the contents of data.csv are piped into
59-
the workbook's sheet.
60-
`
57+
the workbook's sheet.`
6158

6259
// Standard Options
63-
showHelp bool
64-
showLicense bool
65-
showVersion bool
66-
inputFName string
60+
showHelp bool
61+
showLicense bool
62+
showVersion bool
63+
showExamples bool
64+
inputFName string
6765

6866
// App Specific Options
6967
workbookName string
@@ -119,6 +117,7 @@ func init() {
119117
flag.BoolVar(&showLicense, "license", false, "display license")
120118
flag.BoolVar(&showVersion, "v", false, "display version")
121119
flag.BoolVar(&showVersion, "version", false, "display version")
120+
flag.BoolVar(&showExamples, "example", false, "display example(s)")
122121
flag.StringVar(&inputFName, "i", "", "input filename (CSV content)")
123122
flag.StringVar(&inputFName, "input", "", "input filename (CSV content)")
124123

@@ -135,13 +134,28 @@ func main() {
135134
args := flag.Args()
136135

137136
// Configuration and command line interation
138-
cfg := cli.New(appName, appName, fmt.Sprintf(datatools.LicenseText, appName, datatools.Version), datatools.Version)
137+
cfg := cli.New(appName, strings.ToUpper(appName), datatools.Version)
138+
cfg.LicenseText = fmt.Sprintf(datatools.LicenseText, appName, datatools.Version)
139139
cfg.UsageText = fmt.Sprintf(usage, appName)
140140
cfg.DescriptionText = fmt.Sprintf(description, appName)
141+
cfg.OptionText = "OPTIONS"
141142
cfg.ExampleText = fmt.Sprintf(examples, appName, appName)
142143

143144
if showHelp == true {
144-
fmt.Println(cfg.Usage())
145+
if len(args) > 0 {
146+
fmt.Println(cfg.Help(args...))
147+
} else {
148+
fmt.Println(cfg.Usage())
149+
}
150+
os.Exit(0)
151+
}
152+
153+
if showExamples == true {
154+
if len(args) > 0 {
155+
fmt.Println(cfg.Example(args...))
156+
} else {
157+
fmt.Println(cfg.ExampleText)
158+
}
145159
os.Exit(0)
146160
}
147161

cmds/csvcols/csvcols.go

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,13 @@ const (
4545
var (
4646
usage = `USAGE: %s [OPTIONS] [ARGS_AS_COL_VALUES]`
4747

48-
description = `
49-
SYNOPSIS
48+
description = `SYNOPSIS
5049
5150
%s converts a set of command line args into columns output in CSV format.
5251
It can also be used CSV input rows and rendering only the column numbers
53-
listed on the commandline (first column is 1 not 0).
54-
`
52+
listed on the commandline (first column is 1 not 0).`
5553

56-
examples = `
57-
EXAMPLES
54+
examples = `EXAMPLES
5855
5956
Simple usage of building a CSV file one row at a time.
6057
@@ -74,15 +71,15 @@ Filter a 10 column CSV file for columns 1,4,6 (left most column is one)
7471
7572
Filter a 10 columns CSV file for columns 1,4,6 from file named "10col.csv"
7673
77-
%s -i 10col.csv -col 1,4,6 > 3col.csv
78-
`
74+
%s -i 10col.csv -col 1,4,6 > 3col.csv`
7975

8076
// Standard Options
81-
showHelp bool
82-
showLicense bool
83-
showVersion bool
84-
inputFName string
85-
outputFName string
77+
showHelp bool
78+
showLicense bool
79+
showVersion bool
80+
showExamples bool
81+
inputFName string
82+
outputFName string
8683

8784
// App Options
8885
outputColumns string
@@ -156,6 +153,7 @@ func init() {
156153
flag.BoolVar(&showLicense, "license", false, "display license")
157154
flag.BoolVar(&showVersion, "v", false, "display version")
158155
flag.BoolVar(&showVersion, "version", false, "display version")
156+
flag.BoolVar(&showExamples, "example", false, "display example")
159157
flag.StringVar(&inputFName, "i", "", "input filename")
160158
flag.StringVar(&inputFName, "input", "", "input filename")
161159
flag.StringVar(&outputFName, "o", "", "output filename")
@@ -176,13 +174,28 @@ func main() {
176174
args := flag.Args()
177175

178176
// Configuration and command line interation
179-
cfg := cli.New(appName, appName, fmt.Sprintf(datatools.LicenseText, appName, datatools.Version), datatools.Version)
177+
cfg := cli.New(appName, strings.ToUpper(appName), datatools.Version)
178+
cfg.LicenseText = fmt.Sprintf(datatools.LicenseText, appName, datatools.Version)
180179
cfg.UsageText = fmt.Sprintf(usage, appName)
181180
cfg.DescriptionText = fmt.Sprintf(description, appName)
181+
cfg.OptionText = "OPTIONS"
182182
cfg.ExampleText = fmt.Sprintf(examples, appName, appName, appName, appName, appName, appName)
183183

184184
if showHelp == true {
185-
fmt.Println(cfg.Usage())
185+
if len(args) > 0 {
186+
fmt.Println(cfg.Help(args...))
187+
} else {
188+
fmt.Println(cfg.Usage())
189+
}
190+
os.Exit(0)
191+
}
192+
193+
if showExamples == true {
194+
if len(args) > 0 {
195+
fmt.Println(cfg.Example(args...))
196+
} else {
197+
fmt.Println(cfg.ExampleText)
198+
}
186199
os.Exit(0)
187200
}
188201

0 commit comments

Comments
 (0)