Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore --quote-all for CSV output #1084

Merged
merged 2 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions docs/src/manpage.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ CSV/TSV-ONLY FLAGS
--no-implicit-csv-header -l
your-join-in-with-header.csv ...
your-headerless.csv`.
--quote-all Force double-quoting of CSV fields.
-N Keystroke-saver for `--implicit-csv-header
--headerless-csv-output`.

Expand Down Expand Up @@ -472,8 +473,6 @@ LEGACY FLAGS
--no-mmap Miller no longer uses memory-mapping to access data
files.
--ojsonx The `--jvstack` flag is now default true in Miller 6.
--quote-all Ignored as of version 6. Types are inferred/retained
through the processing flow now.
--quote-minimal Ignored as of version 6. Types are inferred/retained
through the processing flow now.
--quote-none Ignored as of version 6. Types are inferred/retained
Expand Down Expand Up @@ -3282,5 +3281,5 @@ SEE ALSO



2022-08-20 MILLER(1)
2022-08-22 MILLER(1)
</pre>
5 changes: 2 additions & 3 deletions docs/src/manpage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ CSV/TSV-ONLY FLAGS
--no-implicit-csv-header -l
your-join-in-with-header.csv ...
your-headerless.csv`.
--quote-all Force double-quoting of CSV fields.
-N Keystroke-saver for `--implicit-csv-header
--headerless-csv-output`.

Expand Down Expand Up @@ -451,8 +452,6 @@ LEGACY FLAGS
--no-mmap Miller no longer uses memory-mapping to access data
files.
--ojsonx The `--jvstack` flag is now default true in Miller 6.
--quote-all Ignored as of version 6. Types are inferred/retained
through the processing flow now.
--quote-minimal Ignored as of version 6. Types are inferred/retained
through the processing flow now.
--quote-none Ignored as of version 6. Types are inferred/retained
Expand Down Expand Up @@ -3261,4 +3260,4 @@ SEE ALSO



2022-08-20 MILLER(1)
2022-08-22 MILLER(1)
2 changes: 1 addition & 1 deletion docs/src/reference-main-flag-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ These are flags which are applicable to CSV format.
* `--implicit-csv-header or --headerless-csv-input or --hi or --implicit-tsv-header`: Use 1,2,3,... as field labels, rather than from line 1 of input files. Tip: combine with `label` to recreate missing headers.
* `--lazy-quotes`: Accepts quotes appearing in unquoted fields, and non-doubled quotes appearing in quoted fields.
* `--no-implicit-csv-header or --no-implicit-tsv-header`: Opposite of `--implicit-csv-header`. This is the default anyway -- the main use is for the flags to `mlr join` if you have main file(s) which are headerless but you want to join in on a file which does have a CSV/TSV header. Then you could use `mlr --csv --implicit-csv-header join --no-implicit-csv-header -l your-join-in-with-header.csv ... your-headerless.csv`.
* `--quote-all`: Force double-quoting of CSV fields.
* `-N`: Keystroke-saver for `--implicit-csv-header --headerless-csv-output`.

## File-format flags
Expand Down Expand Up @@ -241,7 +242,6 @@ They are accepted as no-op flags in order to keep old scripts from breaking.
* `--no-jvstack`: Put objects/arrays all on one line for JSON output. This is the default for JSON Lines output format.
* `--no-mmap`: Miller no longer uses memory-mapping to access data files.
* `--ojsonx`: The `--jvstack` flag is now default true in Miller 6.
* `--quote-all`: Ignored as of version 6. Types are inferred/retained through the processing flow now.
* `--quote-minimal`: Ignored as of version 6. Types are inferred/retained through the processing flow now.
* `--quote-none`: Ignored as of version 6. Types are inferred/retained through the processing flow now.
* `--quote-numeric`: Ignored as of version 6. Types are inferred/retained through the processing flow now.
Expand Down
14 changes: 9 additions & 5 deletions internal/pkg/cli/option_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -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
},
},
},
}

Expand Down
2 changes: 2 additions & 0 deletions internal/pkg/cli/option_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions internal/pkg/output/record_writer_csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand All @@ -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
}
4 changes: 3 additions & 1 deletion internal/pkg/output/record_writer_csv_colorizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (writer *RecordWriterCSV) WriteCSVRecordMaybeColorized(
bufferedOutputStream *bufio.Writer,
outputIsStdout bool,
isKey bool,
quoteAll bool,
) error {
comma := writer.csvWriter.Comma

Expand All @@ -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
}
Expand Down
5 changes: 2 additions & 3 deletions man/manpage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ CSV/TSV-ONLY FLAGS
--no-implicit-csv-header -l
your-join-in-with-header.csv ...
your-headerless.csv`.
--quote-all Force double-quoting of CSV fields.
-N Keystroke-saver for `--implicit-csv-header
--headerless-csv-output`.

Expand Down Expand Up @@ -451,8 +452,6 @@ LEGACY FLAGS
--no-mmap Miller no longer uses memory-mapping to access data
files.
--ojsonx The `--jvstack` flag is now default true in Miller 6.
--quote-all Ignored as of version 6. Types are inferred/retained
through the processing flow now.
--quote-minimal Ignored as of version 6. Types are inferred/retained
through the processing flow now.
--quote-none Ignored as of version 6. Types are inferred/retained
Expand Down Expand Up @@ -3261,4 +3260,4 @@ SEE ALSO



2022-08-20 MILLER(1)
2022-08-22 MILLER(1)
7 changes: 3 additions & 4 deletions man/mlr.1
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
.\" Title: mlr
.\" Author: [see the "AUTHOR" section]
.\" Generator: ./mkman.rb
.\" Date: 2022-08-20
.\" Date: 2022-08-22
.\" Manual: \ \&
.\" Source: \ \&
.\" Language: English
.\"
.TH "MILLER" "1" "2022-08-20" "\ \&" "\ \&"
.TH "MILLER" "1" "2022-08-22" "\ \&" "\ \&"
.\" -----------------------------------------------------------------
.\" * Portability definitions
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -383,6 +383,7 @@ These are flags which are applicable to CSV format.
--no-implicit-csv-header -l
your-join-in-with-header.csv ...
your-headerless.csv`.
--quote-all Force double-quoting of CSV fields.
-N Keystroke-saver for `--implicit-csv-header
--headerless-csv-output`.
.fi
Expand Down Expand Up @@ -554,8 +555,6 @@ They are accepted as no-op flags in order to keep old scripts from breaking.
--no-mmap Miller no longer uses memory-mapping to access data
files.
--ojsonx The `--jvstack` flag is now default true in Miller 6.
--quote-all Ignored as of version 6. Types are inferred/retained
through the processing flow now.
--quote-minimal Ignored as of version 6. Types are inferred/retained
through the processing flow now.
--quote-none Ignored as of version 6. Types are inferred/retained
Expand Down