simonw / sqlite-utils

Python CLI utility and library for manipulating SQLite databases
https://sqlite-utils.datasette.io
Apache License 2.0
1.62k stars 109 forks source link

`--no-headers` doesn't work on most formats #566

Open zellyn opened 1 year ago

zellyn commented 1 year ago

Version 3.33

sqlite-utils query library.db 'select asin from audible' --fmt plain --no-headers | head -3
asin
0062804006
0062891421
simonw commented 1 year ago

Thanks, looks like a bug.

simonw commented 1 year ago

On consulting https://pypi.org/project/tabulate/ it looks like most of those formats don't actually makes sense without headers - so the right thing here might be to raise an error if --fmt and --no-headers are used at the same time.

tobych commented 4 months ago

Seems to me that this is not a bug, and that @zellyn might be misunderstanding what --no-headers is for.

The help text for --no-headers reads: "Omit CSV headers".

(Well, it applies to TSV files, too. So the help text could be improved.)

I reckon raising an error if --no-header is used without either --csv or --tsv would be good.

Furthermore, the options seem a bit messy to me. I mean, you could specify --csv, --tsv, and --fmt all at once. I'd suggest at least check that none of that is happening. And make it clear in the help text that --fmt is using the tabulate library. Don't try to hide it: that abstraction is leaking already.

zellyn commented 4 months ago

My use case for using --no-headers is almost 100% “I'm about to pass this through xargs or some other unix utility.

But you're right that it doesn't make sense for most of the formats that are generating some kind of markup rendering.

tobych commented 4 months ago

@zellyn Which formats are you working with? I've not looked closely at the list of formats tabulate can write, and which have headers. Are you trying to skip headers from a format that sqlite-utils is delegating to tabulate for? Perhaps the tabulate library has an option for omitting a header: sqlite-utils could pass that (or arbitrary) options to tabulate.

Do bear in mind that if you're using a system that has the tail command, you can use tail -n +2 as a filter to remove the first line of a text stream:

$ printf "1\n2\n3\n" | tail -n +2
2
3
zellyn commented 4 months ago

Yeah, I've done the tail thing for sure. I've mostly gotten around this now by using tsv and only selecting one column, which basically is plain.