@@ -45,6 +45,26 @@ string is a command line tool for transforming strings in common ways.
45
45
`
46
46
47
47
examples = `
48
+ Convert text to upper case
49
+
50
+ string toupper "one"
51
+
52
+ Convert text to lower case
53
+
54
+ string tolower "ONE"
55
+
56
+ Captialize an English phrase
57
+
58
+ string englishtitle "one more thing to know"
59
+
60
+ Split a space newline delimited list of words into a JSON array
61
+
62
+ string -i wordlist.txt split "\n"
63
+
64
+ Join a JSON array of strings into a newline delimited list
65
+
66
+ string join '\n' '["one","two","three","four","five"]'
67
+
48
68
`
49
69
50
70
// Standard Options
@@ -60,6 +80,8 @@ string is a command line tool for transforming strings in common ways.
60
80
eol string
61
81
62
82
// App Options
83
+ delimiter string
84
+ outputDelimiter string
63
85
)
64
86
65
87
//
@@ -161,7 +183,7 @@ func doSplit(in io.Reader, out io.Writer, eout io.Writer, args []string) int {
161
183
fmt .Fprintln (eout , "first parameter is the delimiting string" )
162
184
return 1
163
185
}
164
- delimiter := args [0 ]
186
+ delimiter := datatools . NormalizeDelimiter ( args [0 ])
165
187
args = args [1 :]
166
188
// Handle the case where out input is piped in or read from a file.
167
189
if inputFName != "" {
@@ -185,7 +207,7 @@ func doSplitN(in io.Reader, out io.Writer, eout io.Writer, args []string) int {
185
207
fmt .Fprintln (eout , "first parameter is the delimiting string, second is the count" )
186
208
return 1
187
209
}
188
- delimiter := args [0 ]
210
+ delimiter := datatools . NormalizeDelimiter ( args [0 ])
189
211
// Now convert to cnt an integer
190
212
cnt , err := strconv .Atoi (args [1 ])
191
213
if err != nil {
@@ -216,7 +238,7 @@ func doJoin(in io.Reader, out io.Writer, eout io.Writer, args []string) int {
216
238
fmt .Fprintln (eout , "first parameter is the delimiter to join with" )
217
239
return 1
218
240
}
219
- delimiter := args [0 ]
241
+ delimiter := datatools . NormalizeDelimiter ( args [0 ])
220
242
args = args [1 :]
221
243
222
244
// Handle the case where out input is piped in or read from a file.
@@ -388,6 +410,20 @@ func doTrimRight(in io.Reader, out io.Writer, eout io.Writer, args []string) int
388
410
return 0
389
411
}
390
412
413
+ func doTrimSpace (in io.Reader , out io.Writer , eout io.Writer , args []string ) int {
414
+ // Handle content coming from file
415
+ if inputFName != "" {
416
+ src , err := ioutil .ReadAll (in )
417
+ exitOnError (eout , err , quiet )
418
+ args = append (args , string (src ))
419
+ }
420
+ // Handle content common from args
421
+ for _ , arg := range args {
422
+ fmt .Fprintf (out , "%s%s" , strings .TrimSpace (arg ), eol )
423
+ }
424
+ return 0
425
+ }
426
+
391
427
func doContains (in io.Reader , out io.Writer , eout io.Writer , args []string ) int {
392
428
if len (args ) < 1 {
393
429
fmt .Fprintf (eout , "first parameter is the target string\n " )
@@ -585,6 +621,8 @@ func main() {
585
621
app .BoolVar (& generateMarkdownDocs , "generate-markdown-docs" , false , "output documentation in Markdown" )
586
622
587
623
// App Options
624
+ app .StringVar (& delimiter , "d,delimiter" , "" , "set the delimiter" )
625
+ app .StringVar (& outputDelimiter , "do,output-delimiter" , "" , "set the output delimiter" )
588
626
589
627
// Add verbs and functions
590
628
app .AddAction ("toupper" , doToUpper , "to upper case: [STRING]" )
@@ -601,6 +639,7 @@ func main() {
601
639
app .AddAction ("trim" , doTrim , "trim (beginning and end), CUTSET [STRING]" )
602
640
app .AddAction ("trimleft" , doTrimLeft , "left trim: CUTSET [STRING]" )
603
641
app .AddAction ("trimright" , doTrimRight , "right trim: CUTSET [STRING]" )
642
+ app .AddAction ("trimspace" , doTrimSpace , "trim leading and trailing spaces: [STRING]" )
604
643
app .AddAction ("count" , doCount , "count substrings: SUBSTRING [STRING]" )
605
644
app .AddAction ("contains" , doContains , "has substrings: SUBSTRING [STRING]" )
606
645
app .AddAction ("length" , doLength , "length of string: [STRING]" )
@@ -623,7 +662,7 @@ func main() {
623
662
cli .ExitOnError (app .Eout , err , quiet )
624
663
defer cli .CloseFile (inputFName , app .In )
625
664
626
- app .Out , err = cli .Create (inputFName , os .Stdout )
665
+ app .Out , err = cli .Create (outputFName , os .Stdout )
627
666
cli .ExitOnError (app .Eout , err , quiet )
628
667
defer cli .CloseFile (outputFName , app .Out )
629
668
0 commit comments