Restore --quote-all for CSV output (#1084)

* Restore --quote-all flag for CSV output

* make dev: for doc-build artifacts
This commit is contained in:
John Kerl 2022-08-22 09:38:53 -04:00 committed by GitHub
parent a8cc3032dc
commit abf58f4bb5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 24 additions and 17 deletions

View file

@ -334,6 +334,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`.
@ -477,8 +478,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

View file

@ -313,6 +313,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`.
@ -456,8 +457,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

View file

@ -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
@ -249,7 +250,6 @@ They are accepted as no-op flags in order to keep old scripts from breaking.
* `--mmap`: Miller no longer uses memory-mapping to access data files.
* `--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.

View file

@ -576,11 +576,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.",
@ -2177,6 +2172,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
},
},
},
}

View file

@ -106,6 +106,8 @@ type TWriterOptions struct {
JVQuoteAll bool // --jvquoteall
// Not using miller/types enum to avoid package cycle
CSVQuoteAll bool // --quote-all
// When we read things like
//
// x:a=1,x:b=2

View file

@ -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
}

View file

@ -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
}

View file

@ -313,6 +313,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`.
@ -456,8 +457,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

View file

@ -384,6 +384,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
@ -567,8 +568,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