Skip to content

Commit a394e62

Browse files
committed
Added splitstring cli
1 parent ac85db9 commit a394e62

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed

cmds/splitstring/splitstring.go

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"flag"
6+
"fmt"
7+
"io/ioutil"
8+
"os"
9+
"path"
10+
"strings"
11+
12+
// Caltech Library Packages
13+
"github.com/caltechlibrary/cli"
14+
"github.com/caltechlibrary/datatools"
15+
)
16+
17+
var (
18+
usage = `USAGE: %s [OPTIONS] [STRING_TO_SPLIT]`
19+
20+
description = `
21+
%s splits a string based on a delimiting string provided. The default
22+
delimiter is a space. You can specify a delimiting string via
23+
the -d or --delimiter option. %s will split the string provided
24+
as a command line argument but can read split string(s) recieved on
25+
stdin in with the -i or --input option. By default the split
26+
strings are render as a JSON array but with the option -nl or
27+
--newline you can render each split string one per line.
28+
`
29+
30+
examples = `
31+
EXAMPLES
32+
33+
Splitting a string that is double pipe delimited rendering
34+
one sub string per line.
35+
36+
%s -nl -d '||' "one||two||three"
37+
38+
This should yield
39+
40+
one
41+
two
42+
three
43+
44+
Splitting a string that is double pipe delimited rendering JSON
45+
46+
%s -d '||' "one||two||three"
47+
48+
This should yield
49+
50+
["one","two","three"]
51+
52+
`
53+
54+
// Standard Options
55+
showHelp bool
56+
showLicense bool
57+
showVersion bool
58+
showExamples bool
59+
inputFName string
60+
outputFName string
61+
62+
// App Options
63+
delimiter string
64+
newLine bool
65+
)
66+
67+
func init() {
68+
// Standard options
69+
flag.BoolVar(&showHelp, "h", false, "display help")
70+
flag.BoolVar(&showHelp, "help", false, "display help")
71+
flag.BoolVar(&showLicense, "l", false, "display license")
72+
flag.BoolVar(&showLicense, "license", false, "display license")
73+
flag.BoolVar(&showVersion, "v", false, "display version")
74+
flag.BoolVar(&showVersion, "version", false, "display version")
75+
flag.BoolVar(&showExamples, "example", false, "display example(s)")
76+
flag.StringVar(&inputFName, "i", "", "input filename")
77+
flag.StringVar(&inputFName, "input", "", "input filename")
78+
flag.StringVar(&outputFName, "o", "", "output filename")
79+
flag.StringVar(&outputFName, "output", "", "output filename")
80+
81+
// Application specific options
82+
flag.StringVar(&delimiter, "d", " ", "set the delimiting string value")
83+
flag.StringVar(&delimiter, "delimiter", " ", "set the delimiting string value")
84+
flag.BoolVar(&newLine, "nl", false, "output as one substring per line rather than JSON")
85+
flag.BoolVar(&newLine, "newline", false, "output as one substring per line rather than JSON")
86+
}
87+
88+
func main() {
89+
appName := path.Base(os.Args[0])
90+
flag.Parse()
91+
args := flag.Args()
92+
93+
cfg := cli.New(appName, "", datatools.Version)
94+
cfg.UsageText = fmt.Sprintf(usage, appName)
95+
cfg.DescriptionText = fmt.Sprintf(description, appName)
96+
cfg.OptionText = "OPTIONS"
97+
cfg.ExampleText = fmt.Sprintf(examples, appName, appName)
98+
99+
if showHelp == true {
100+
if len(args) > 0 {
101+
fmt.Println(cfg.Help(args...))
102+
} else {
103+
fmt.Println(cfg.Usage())
104+
}
105+
os.Exit(0)
106+
}
107+
108+
if showExamples == true {
109+
if len(args) > 0 {
110+
fmt.Println(cfg.Example(args...))
111+
} else {
112+
fmt.Println(cfg.ExampleText)
113+
}
114+
os.Exit(0)
115+
}
116+
117+
if showLicense == true {
118+
fmt.Println(cfg.License())
119+
os.Exit(0)
120+
}
121+
122+
if showVersion == true {
123+
fmt.Println(cfg.Version())
124+
os.Exit(0)
125+
}
126+
127+
in, err := cli.Open(inputFName, os.Stdin)
128+
if err != nil {
129+
fmt.Fprintf(os.Stderr, "%s\n", err)
130+
os.Exit(1)
131+
}
132+
defer cli.CloseFile(inputFName, in)
133+
134+
out, err := cli.Create(outputFName, os.Stdout)
135+
if err != nil {
136+
fmt.Fprintf(os.Stderr, "%s\n", err)
137+
os.Exit(1)
138+
}
139+
defer cli.CloseFile(outputFName, out)
140+
141+
if inputFName != "" {
142+
src, err := ioutil.ReadAll(in)
143+
if err != nil {
144+
fmt.Fprintf(os.Stderr, "Can't read file, %s", err)
145+
os.Exit(1)
146+
}
147+
args = strings.Split(string(src), "\n")
148+
}
149+
150+
results := []string{}
151+
for _, s := range args {
152+
parts := strings.Split(s, delimiter)
153+
for _, p := range parts {
154+
results = append(results, p)
155+
}
156+
}
157+
158+
if newLine == true {
159+
fmt.Fprintf(out, "%s\n", strings.Join(results, "\n"))
160+
} else {
161+
src, err := json.Marshal(results)
162+
if err != nil {
163+
fmt.Fprintf(os.Stderr, "Can't marshal %+v", err)
164+
os.Exit(1)
165+
}
166+
fmt.Fprintf(out, "%s\n", src)
167+
}
168+
}

0 commit comments

Comments
 (0)