diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index 9b9ee20e..db71c502 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -237,7 +237,13 @@ def _iter(): 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: diff --git a/tests/test_cli.py b/tests/test_cli.py index 40c3595c..858cd153 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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