fix(cli): honor --no-headers flag with --fmt/--table output

Fixes simonw/sqlite-utils#566

The --no-headers flag was only checked in the CSV/TSV output branch.
When --fmt or --table was used, tabulate.tabulate() was always called
with headers=headers, ignoring the flag. Pass headers=() instead when
no_headers is set, which suppresses header display while preserving
table formatting.
This commit is contained in:
CrepuscularIRIS 2026-04-15 17:38:32 +08:00
commit 2786dcb914
2 changed files with 60 additions and 2 deletions

View file

@ -237,7 +237,13 @@ def tables(
yield row
if table or fmt:
print(tabulate.tabulate(_iter(), headers=headers, tablefmt=fmt or "simple"))
print(
tabulate.tabulate(
_iter(),
headers=() if no_headers else headers,
tablefmt=fmt or "simple",
)
)
elif csv or tsv:
writer = csv_std.writer(sys.stdout, dialect="excel-tab" if tsv else "excel")
if not no_headers:
@ -2139,7 +2145,9 @@ def _execute_query(
elif fmt or table:
print(
tabulate.tabulate(
list(cursor), headers=headers, tablefmt=fmt or "simple"
list(cursor),
headers=() if no_headers else headers,
tablefmt=fmt or "simple",
)
)
elif csv or tsv:

View file

@ -195,6 +195,56 @@ def test_output_table(db_path, options, expected):
assert expected == result.output.strip()
@pytest.mark.parametrize(
"fmt,expected",
[
(
"plain",
"verb0 noun0 adjective0\nverb1 noun1 adjective1\nverb2 noun2 adjective2\nverb3 noun3 adjective3",
),
(
"simple",
"----- ----- ----------\nverb0 noun0 adjective0\nverb1 noun1 adjective1\nverb2 noun2 adjective2\nverb3 noun3 adjective3\n----- ----- ----------",
),
],
)
def test_output_table_no_headers(db_path, fmt, expected):
db = Database(db_path)
with db.conn:
db["rows"].insert_all(
[
{
"c1": "verb{}".format(i),
"c2": "noun{}".format(i),
"c3": "adjective{}".format(i),
}
for i in range(4)
]
)
result = CliRunner().invoke(
cli.cli, ["rows", db_path, "rows", "--fmt", fmt, "--no-headers"]
)
assert result.exit_code == 0
assert expected == result.output.strip()
def test_query_fmt_no_headers(db_path):
db = Database(db_path)
with db.conn:
db["dogs"].insert_all(
[
{"id": 1, "name": "Cleo"},
{"id": 2, "name": "Pancakes"},
]
)
result = CliRunner().invoke(
cli.cli,
[db_path, "select id, name from dogs", "--fmt", "plain", "--no-headers"],
)
assert result.exit_code == 0
assert result.output.strip() == "1 Cleo\n2 Pancakes"
def test_create_index(db_path):
db = Database(db_path)
assert [] == db["Gosh"].indexes