@@ -45,6 +45,26 @@ string is a command line tool for transforming strings in common ways.
4545`
4646
4747 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+
4868`
4969
5070 // Standard Options
@@ -60,6 +80,8 @@ string is a command line tool for transforming strings in common ways.
6080 eol string
6181
6282 // App Options
83+ delimiter string
84+ outputDelimiter string
6385)
6486
6587//
@@ -161,7 +183,7 @@ func doSplit(in io.Reader, out io.Writer, eout io.Writer, args []string) int {
161183 fmt .Fprintln (eout , "first parameter is the delimiting string" )
162184 return 1
163185 }
164- delimiter := args [0 ]
186+ delimiter := datatools . NormalizeDelimiter ( args [0 ])
165187 args = args [1 :]
166188 // Handle the case where out input is piped in or read from a file.
167189 if inputFName != "" {
@@ -185,7 +207,7 @@ func doSplitN(in io.Reader, out io.Writer, eout io.Writer, args []string) int {
185207 fmt .Fprintln (eout , "first parameter is the delimiting string, second is the count" )
186208 return 1
187209 }
188- delimiter := args [0 ]
210+ delimiter := datatools . NormalizeDelimiter ( args [0 ])
189211 // Now convert to cnt an integer
190212 cnt , err := strconv .Atoi (args [1 ])
191213 if err != nil {
@@ -216,7 +238,7 @@ func doJoin(in io.Reader, out io.Writer, eout io.Writer, args []string) int {
216238 fmt .Fprintln (eout , "first parameter is the delimiter to join with" )
217239 return 1
218240 }
219- delimiter := args [0 ]
241+ delimiter := datatools . NormalizeDelimiter ( args [0 ])
220242 args = args [1 :]
221243
222244 // 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
388410 return 0
389411}
390412
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+
391427func doContains (in io.Reader , out io.Writer , eout io.Writer , args []string ) int {
392428 if len (args ) < 1 {
393429 fmt .Fprintf (eout , "first parameter is the target string\n " )
@@ -585,6 +621,8 @@ func main() {
585621 app .BoolVar (& generateMarkdownDocs , "generate-markdown-docs" , false , "output documentation in Markdown" )
586622
587623 // App Options
624+ app .StringVar (& delimiter , "d,delimiter" , "" , "set the delimiter" )
625+ app .StringVar (& outputDelimiter , "do,output-delimiter" , "" , "set the output delimiter" )
588626
589627 // Add verbs and functions
590628 app .AddAction ("toupper" , doToUpper , "to upper case: [STRING]" )
@@ -601,6 +639,7 @@ func main() {
601639 app .AddAction ("trim" , doTrim , "trim (beginning and end), CUTSET [STRING]" )
602640 app .AddAction ("trimleft" , doTrimLeft , "left trim: CUTSET [STRING]" )
603641 app .AddAction ("trimright" , doTrimRight , "right trim: CUTSET [STRING]" )
642+ app .AddAction ("trimspace" , doTrimSpace , "trim leading and trailing spaces: [STRING]" )
604643 app .AddAction ("count" , doCount , "count substrings: SUBSTRING [STRING]" )
605644 app .AddAction ("contains" , doContains , "has substrings: SUBSTRING [STRING]" )
606645 app .AddAction ("length" , doLength , "length of string: [STRING]" )
@@ -623,7 +662,7 @@ func main() {
623662 cli .ExitOnError (app .Eout , err , quiet )
624663 defer cli .CloseFile (inputFName , app .In )
625664
626- app .Out , err = cli .Create (inputFName , os .Stdout )
665+ app .Out , err = cli .Create (outputFName , os .Stdout )
627666 cli .ExitOnError (app .Eout , err , quiet )
628667 defer cli .CloseFile (outputFName , app .Out )
629668
0 commit comments