22sphinxnotes.snippet.cli
33~~~~~~~~~~~~~~~~~~~~~~~
44
5- :copyright: Copyright 2020 Shengyu Zhang
5+ Command line entrypoint.
6+
7+ :copyright: Copyright 2024 Shengyu Zhang
68:license: BSD, see LICENSE for details.
79"""
810
11+ # **NOTE**: Import new packages with caution:
12+ # Importing complex packages (like sphinx.*) will directly slow down the
13+ # startup of the CLI tool.
914from __future__ import annotations
1015import sys
1116import argparse
1419from textwrap import dedent
1520from shutil import get_terminal_size
1621import posixpath
17- from importlib .metadata import version
1822
1923from xdg .BaseDirectory import xdg_config_home
20- from sphinx .util .matching import patmatch
2124
2225from .snippets import Document
2326from .config import Config
@@ -40,7 +43,7 @@ def get_integration_file(fn: str) -> str:
4043 .. seealso::
4144
4245 see ``[tool.setuptools.package-data]`` section of pyproject.toml to know
43- how files are included.
46+ how files are included.
4447 """
4548 # TODO: use https://docs.python.org/3/library/importlib.resources.html#importlib.resources.files
4649 prefix = path .abspath (path .dirname (__file__ ))
@@ -62,10 +65,11 @@ def main(argv: List[str] = sys.argv[1:]):
6265 * (any) wildcard for any snippet""" ),
6366 )
6467 parser .add_argument (
65- '-v' ,
6668 '--version' ,
67- action = 'version' ,
68- version = '%(prog)s ' + version ('sphinxnotes.any' ),
69+ # add_argument provides action='version', but it requires a version
70+ # literal and doesn't support lazily obtaining version.
71+ action = 'store_true' ,
72+ help = "show program's version number and exit" ,
6973 )
7074 parser .add_argument (
7175 '-c' , '--config' , default = DEFAULT_CONFIG_FILE , help = 'path to configuration file'
@@ -180,6 +184,16 @@ def main(argv: List[str] = sys.argv[1:]):
180184 # Parse command line arguments
181185 args = parser .parse_args (argv )
182186
187+ # Print version message.
188+ # See parser.add_argument('--version', ...) for more detais.
189+ if args .version :
190+ # NOTE: Importing is slow, do it on demand.
191+ from importlib .metadata import version
192+
193+ pkgname = 'sphinxnotes.snippet'
194+ print (pkgname , version (pkgname ))
195+ parser .exit ()
196+
183197 # Load config from file
184198 if args .config == DEFAULT_CONFIG_FILE and not path .isfile (DEFAULT_CONFIG_FILE ):
185199 print (
@@ -223,6 +237,9 @@ def _on_command_stat(args: argparse.Namespace):
223237def _filter_list_items (
224238 cache : Cache , tags : str , docname_glob : str
225239) -> Iterable [Tuple [IndexID , Index ]]:
240+ # NOTE: Importing is slow, do it on demand.
241+ from sphinx .util .matching import patmatch
242+
226243 for index_id , index in cache .indexes .items ():
227244 # Filter by tags.
228245 if index [0 ] not in tags and '*' not in tags :
0 commit comments