Open simonw opened 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.
Simplest solution would be to ditch the version_option
decorator and roll a custom option based on it instead, imitating what this code does:
Idea came up here: https://discord.com/channels/823971286308356157/823971286941302908/1066026473003159783