simonw / datasette

An open source multi-tool for exploring and publishing data
https://datasette.io
Apache License 2.0
9.59k stars 691 forks source link

`datasette --version` should also show the SQLite version #1998

Open simonw opened 1 year ago

simonw commented 1 year ago

Idea came up here: https://discord.com/channels/823971286308356157/823971286941302908/1066026473003159783

simonw commented 1 year ago

Fell down a bit of a rabbit hole trying to figure out how to get Click's version_option() to evaluate a custom message. Got this far:

class _VersionMessage(UserString):
    @property
    def data(self):
        return "%(prog)s, version %(version)s (SQLite {})".format(
            sqlite3.connect(":memory:").execute("select sqlite_version()").fetchone()[0]
        )

    @data.setter
    def data(self, value):
        pass

@click.group(cls=DefaultGroup, default="serve", default_if_no_args=True)
@click.version_option(version=__version__, message=_VersionMessage(""))
def cli():
    """
    Datasette is an open source multi-tool for exploring and publishing data

    \b
    About Datasette: https://datasette.io/
    Full documentation: https://docs.datasette.io/
    """

But now:

% datasette --version
%(prog)s, version %(version)s (SQLite 3.40.1)

I was trying to avoid running that select sqlite_version() thing unless the --version option was used.

simonw commented 1 year ago

Simplest solution would be to ditch the version_option decorator and roll a custom option based on it instead, imitating what this code does:

https://github.com/pallets/click/blob/7586834cab38c5592d9d6de3ee0ebe75d4353bfb/src/click/decorators.py#L413-L524