--fmt now implies --table, closes #374

This commit is contained in:
Simon Willison 2022-01-09 10:07:48 -08:00
commit b8c134059e
3 changed files with 26 additions and 14 deletions

View file

@ -168,7 +168,7 @@ You can use the ``--table`` option (or ``-t`` shortcut) to output query results
You can use the ``--fmt`` option to specify different table formats, for example ``rst`` for reStructuredText::
$ sqlite-utils dogs.db "select * from dogs" --table --fmt rst
$ sqlite-utils dogs.db "select * from dogs" --fmt rst
==== ===== ========
id age name
==== ===== ========
@ -182,7 +182,6 @@ Available ``--fmt`` options are:
import tabulate
cog.out("\n" + "\n".join('- ``{}``'.format(t) for t in tabulate.tabulate_formats) + "\n\n")
.. ]]]
- ``fancy_grid``
- ``fancy_outline``
- ``github``
@ -207,7 +206,6 @@ Available ``--fmt`` options are:
- ``tsv``
- ``unsafehtml``
- ``youtrack``
.. [[[end]]]
This list can also be found by running ``sqlite-utils query --help``.
@ -289,7 +287,7 @@ It takes all of the same output formatting options as :ref:`sqlite-utils query <
$ sqlite-utils memory 'select sqlite_version()' --csv
sqlite_version()
3.35.5
$ sqlite-utils memory 'select sqlite_version()' --table --fmt grid
$ sqlite-utils memory 'select sqlite_version()' --fmt grid
+--------------------+
| sqlite_version() |
+====================+

View file

@ -81,7 +81,6 @@ def output_options(fn):
help="Table format - one of {}".format(
", ".join(tabulate.tabulate_formats)
),
default="simple",
),
click.option(
"--json-cols",
@ -192,8 +191,8 @@ def tables(
row.append(db[name].schema)
yield row
if table:
print(tabulate.tabulate(_iter(), headers=headers, tablefmt=fmt))
if table or fmt:
print(tabulate.tabulate(_iter(), headers=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:
@ -1456,8 +1455,12 @@ def _execute_query(
sys.stdout.buffer.write(data)
else:
sys.stdout.write(str(data))
elif table:
print(tabulate.tabulate(list(cursor), headers=headers, tablefmt=fmt))
elif fmt or table:
print(
tabulate.tabulate(
list(cursor), headers=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:

View file

@ -114,10 +114,10 @@ def test_tables_schema(db_path):
@pytest.mark.parametrize(
"fmt,expected",
"options,expected",
[
(
"simple",
["--fmt", "simple"],
(
"c1 c2 c3\n"
"----- ----- ----------\n"
@ -128,7 +128,18 @@ def test_tables_schema(db_path):
),
),
(
"rst",
["-t"],
(
"c1 c2 c3\n"
"----- ----- ----------\n"
"verb0 noun0 adjective0\n"
"verb1 noun1 adjective1\n"
"verb2 noun2 adjective2\n"
"verb3 noun3 adjective3"
),
),
(
["--fmt", "rst"],
(
"===== ===== ==========\n"
"c1 c2 c3\n"
@ -142,7 +153,7 @@ def test_tables_schema(db_path):
),
],
)
def test_output_table(db_path, fmt, expected):
def test_output_table(db_path, options, expected):
db = Database(db_path)
with db.conn:
db["rows"].insert_all(
@ -155,7 +166,7 @@ def test_output_table(db_path, fmt, expected):
for i in range(4)
]
)
result = CliRunner().invoke(cli.cli, ["rows", db_path, "rows", "-t", "--fmt", fmt])
result = CliRunner().invoke(cli.cli, ["rows", db_path, "rows"] + options)
assert 0 == result.exit_code
assert expected == result.output.strip()