From b8c134059e89f0fa040b84fb7d0bda25b9a52759 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 9 Jan 2022 10:07:48 -0800 Subject: [PATCH] --fmt now implies --table, closes #374 --- docs/cli.rst | 6 ++---- sqlite_utils/cli.py | 13 ++++++++----- tests/test_cli.py | 21 ++++++++++++++++----- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/docs/cli.rst b/docs/cli.rst index 497e00e..f56e382 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -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() | +====================+ diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index 624f47e..2bd0278 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -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: diff --git a/tests/test_cli.py b/tests/test_cli.py index 7afe9ca..5a9ca65 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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()