@@ -14,6 +14,7 @@ import (
1414 "io/ioutil"
1515 "os"
1616 "path"
17+ "strings"
1718
1819 // Caltech Library Packages
1920 "github.com/caltechlibrary/cli"
@@ -88,6 +89,7 @@ Would yield
8889 delimiter = ","
8990 expressions []string
9091 permissive bool
92+ quote bool
9193)
9294
9395func handleError (err error , exitCode int ) {
@@ -117,6 +119,7 @@ func init() {
117119 flag .BoolVar (& runInteractive , "r" , false , "run interactively" )
118120 flag .BoolVar (& runInteractive , "repl" , false , "run interactively" )
119121 flag .StringVar (& delimiter , "d" , delimiter , "set the delimiter for multi-field output" )
122+ flag .BoolVar (& quote , "quote" , false , "if dilimiter is found in column value add quotes" )
120123 flag .BoolVar (& permissive , "permissive" , false , "suppress error messages" )
121124 flag .BoolVar (& permissive , "quiet" , false , "suppress error messages" )
122125}
@@ -197,15 +200,23 @@ func main() {
197200 if err == nil {
198201 switch result .(type ) {
199202 case string :
200- fmt .Fprintf (out , "%s" , result )
203+ if quote == true && strings .Contains (result .(string ), delimiter ) == true {
204+ fmt .Fprintf (out , "%q" , result )
205+ } else {
206+ fmt .Fprintf (out , "%s" , result )
207+ }
201208 case json.Number :
202209 fmt .Fprintf (out , "%s" , result .(json.Number ).String ())
203210 default :
204211 src , err := json .Marshal (result )
205212 if err != nil {
206213 handleError (err , 1 )
207214 }
208- fmt .Fprintf (out , "%s" , src )
215+ if quote == true {
216+ fmt .Fprintf (out , "%q" , src )
217+ } else {
218+ fmt .Fprintf (out , "%s" , src )
219+ }
209220 }
210221 } else {
211222 handleError (err , 1 )
0 commit comments