Skip to content

Commit 2786dcb

Browse files
fix(cli): honor --no-headers flag with --fmt/--table output
Fixes #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.
1 parent 8d74ffc commit 2786dcb

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

sqlite_utils/cli.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,13 @@ def _iter():
237237
yield row
238238

239239
if table or fmt:
240-
print(tabulate.tabulate(_iter(), headers=headers, tablefmt=fmt or "simple"))
240+
print(
241+
tabulate.tabulate(
242+
_iter(),
243+
headers=() if no_headers else headers,
244+
tablefmt=fmt or "simple",
245+
)
246+
)
241247
elif csv or tsv:
242248
writer = csv_std.writer(sys.stdout, dialect="excel-tab" if tsv else "excel")
243249
if not no_headers:
@@ -2139,7 +2145,9 @@ def _execute_query(
21392145
elif fmt or table:
21402146
print(
21412147
tabulate.tabulate(
2142-
list(cursor), headers=headers, tablefmt=fmt or "simple"
2148+
list(cursor),
2149+
headers=() if no_headers else headers,
2150+
tablefmt=fmt or "simple",
21432151
)
21442152
)
21452153
elif csv or tsv:

tests/test_cli.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,56 @@ def test_output_table(db_path, options, expected):
195195
assert expected == result.output.strip()
196196

197197

198+
@pytest.mark.parametrize(
199+
"fmt,expected",
200+
[
201+
(
202+
"plain",
203+
"verb0 noun0 adjective0\nverb1 noun1 adjective1\nverb2 noun2 adjective2\nverb3 noun3 adjective3",
204+
),
205+
(
206+
"simple",
207+
"----- ----- ----------\nverb0 noun0 adjective0\nverb1 noun1 adjective1\nverb2 noun2 adjective2\nverb3 noun3 adjective3\n----- ----- ----------",
208+
),
209+
],
210+
)
211+
def test_output_table_no_headers(db_path, fmt, expected):
212+
db = Database(db_path)
213+
with db.conn:
214+
db["rows"].insert_all(
215+
[
216+
{
217+
"c1": "verb{}".format(i),
218+
"c2": "noun{}".format(i),
219+
"c3": "adjective{}".format(i),
220+
}
221+
for i in range(4)
222+
]
223+
)
224+
result = CliRunner().invoke(
225+
cli.cli, ["rows", db_path, "rows", "--fmt", fmt, "--no-headers"]
226+
)
227+
assert result.exit_code == 0
228+
assert expected == result.output.strip()
229+
230+
231+
def test_query_fmt_no_headers(db_path):
232+
db = Database(db_path)
233+
with db.conn:
234+
db["dogs"].insert_all(
235+
[
236+
{"id": 1, "name": "Cleo"},
237+
{"id": 2, "name": "Pancakes"},
238+
]
239+
)
240+
result = CliRunner().invoke(
241+
cli.cli,
242+
[db_path, "select id, name from dogs", "--fmt", "plain", "--no-headers"],
243+
)
244+
assert result.exit_code == 0
245+
assert result.output.strip() == "1 Cleo\n2 Pancakes"
246+
247+
198248
def test_create_index(db_path):
199249
db = Database(db_path)
200250
assert [] == db["Gosh"].indexes

0 commit comments

Comments
 (0)