diff --git a/internal/pkg/cli/option_parse.go b/internal/pkg/cli/option_parse.go index 2aa72a6ff..230585467 100644 --- a/internal/pkg/cli/option_parse.go +++ b/internal/pkg/cli/option_parse.go @@ -519,11 +519,6 @@ var LegacyFlagSection = FlagSection{ parser: NoOpParse1, }, - { - name: "--quote-all", - help: "Ignored as of version 6. Types are inferred/retained through the processing flow now.", - parser: NoOpParse1, - }, { name: "--quote-none", help: "Ignored as of version 6. Types are inferred/retained through the processing flow now.", @@ -2157,6 +2152,15 @@ var CSVTSVOnlyFlagSection = FlagSection{ *pargi += 1 }, }, + + { + name: "--quote-all", + help: "Force double-quoting of CSV fields.", + parser: func(args []string, argc int, pargi *int, options *TOptions) { + options.WriterOptions.CSVQuoteAll = true + *pargi += 1 + }, + }, }, } diff --git a/internal/pkg/cli/option_types.go b/internal/pkg/cli/option_types.go index ac1eb0f3e..2262cb0fa 100644 --- a/internal/pkg/cli/option_types.go +++ b/internal/pkg/cli/option_types.go @@ -105,6 +105,8 @@ type TWriterOptions struct { JSONOutputMultiline bool // --jvstack // Not using miller/types enum to avoid package cycle + CSVQuoteAll bool // --quote-all + // When we read things like // // x:a=1,x:b=2 diff --git a/internal/pkg/output/record_writer_csv.go b/internal/pkg/output/record_writer_csv.go index b5b864cf2..9c469bedf 100644 --- a/internal/pkg/output/record_writer_csv.go +++ b/internal/pkg/output/record_writer_csv.go @@ -18,6 +18,8 @@ type RecordWriterCSV struct { lastJoinedHeader *string // Only write one blank line for schema changes / blank input lines justWroteEmptyLine bool + // For double-quote around all fields + quoteAll bool } func NewRecordWriterCSV(writerOptions *cli.TWriterOptions) (*RecordWriterCSV, error) { @@ -32,6 +34,7 @@ func NewRecordWriterCSV(writerOptions *cli.TWriterOptions) (*RecordWriterCSV, er csvWriter: nil, // will be set on first Write() wherein we have the output stream lastJoinedHeader: nil, justWroteEmptyLine: false, + quoteAll: writerOptions.CSVQuoteAll, }, nil } @@ -81,7 +84,7 @@ func (writer *RecordWriterCSV) Write( i++ } //////writer.csvWriter.Write(fields) - writer.WriteCSVRecordMaybeColorized(fields, bufferedOutputStream, outputIsStdout, true) + writer.WriteCSVRecordMaybeColorized(fields, bufferedOutputStream, outputIsStdout, true, writer.quoteAll) } fields := make([]string, outrec.FieldCount) @@ -90,6 +93,6 @@ func (writer *RecordWriterCSV) Write( fields[i] = pe.Value.String() i++ } - writer.WriteCSVRecordMaybeColorized(fields, bufferedOutputStream, outputIsStdout, false) + writer.WriteCSVRecordMaybeColorized(fields, bufferedOutputStream, outputIsStdout, false, writer.quoteAll) writer.justWroteEmptyLine = false } diff --git a/internal/pkg/output/record_writer_csv_colorizer.go b/internal/pkg/output/record_writer_csv_colorizer.go index 47a95e727..a602ae790 100644 --- a/internal/pkg/output/record_writer_csv_colorizer.go +++ b/internal/pkg/output/record_writer_csv_colorizer.go @@ -58,6 +58,7 @@ func (writer *RecordWriterCSV) WriteCSVRecordMaybeColorized( bufferedOutputStream *bufio.Writer, outputIsStdout bool, isKey bool, + quoteAll bool, ) error { comma := writer.csvWriter.Comma @@ -82,7 +83,8 @@ func (writer *RecordWriterCSV) WriteCSVRecordMaybeColorized( // If we don't have to have a quoted field then just // write out the field and continue to the next field. - if !fieldNeedsQuotes(field, comma) { + needsQuotes := quoteAll || fieldNeedsQuotes(field, comma) + if !needsQuotes { if _, err := bufferedOutputStream.WriteString(prefix); err != nil { return err }