diff --git a/docs/cli.rst b/docs/cli.rst new file mode 100644 index 0000000..cf46853 --- /dev/null +++ b/docs/cli.rst @@ -0,0 +1,17 @@ +.. _python_api: + +================================ + sqlite-utils command-line tool +================================ + +The ``sqlite-utils`` command-line tool can be used to manipulate SQLite databases in a number of different ways. + +Listing tables +============== + +You can list the names of tables in a database using the ``table_names`` subcommand:: + + $ sqlite-utils table_names mydb.db + dogs + cats + chickens diff --git a/docs/index.rst b/docs/index.rst index 6bbbb73..37641ee 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -17,6 +17,7 @@ Contents :maxdepth: 2 python-api + cli changelog Take a look at `this script `_ for an example of this library in action. diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index 6177c6b..8b252e4 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -1,6 +1,22 @@ import click +import sqlite_utils -@click.command() -def cli(*args): - click.echo(repr(args)) +@click.group() +@click.version_option() +def cli(): + "Commands for interacting with a SQLite database" + pass + + +@cli.command() +@click.argument( + "path", + type=click.Path(exists=True, file_okay=True, dir_okay=False, allow_dash=False), + required=True, +) +def table_names(path): + """List the tables in the database""" + db = sqlite_utils.Database(path) + for name in db.table_names: + print(name) diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..8b8c061 --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,23 @@ +from sqlite_utils import cli +from click.testing import CliRunner +import pytest +import sqlite3 + + +CREATE_TABLES = """ +create table Gosh (c1 text, c2 text, c3 text); +create table Gosh2 (c1 text, c2 text, c3 text); +""" + + +@pytest.fixture +def db_path(tmpdir): + path = str(tmpdir / "test.db") + db = sqlite3.connect(path) + db.executescript(CREATE_TABLES) + return path + + +def test_table_names(db_path): + result = CliRunner().invoke(cli.cli, ["table_names", db_path]) + assert "Gosh\nGosh2" == result.output.strip()