2
2
sphinxnotes.snippet.cli
3
3
~~~~~~~~~~~~~~~~~~~~~~~
4
4
5
- :copyright: Copyright 2020 Shengyu Zhang
5
+ Command line entrypoint.
6
+
7
+ :copyright: Copyright 2024 Shengyu Zhang
6
8
:license: BSD, see LICENSE for details.
7
9
"""
8
10
11
+ # **NOTE**: Import new packages with caution:
12
+ # Importing complex packages (like sphinx.*) will directly slow down the
13
+ # startup of the CLI tool.
9
14
from __future__ import annotations
10
15
import sys
11
16
import argparse
14
19
from textwrap import dedent
15
20
from shutil import get_terminal_size
16
21
import posixpath
17
- from importlib .metadata import version
18
22
19
23
from xdg .BaseDirectory import xdg_config_home
20
- from sphinx .util .matching import patmatch
21
24
22
25
from .snippets import Document
23
26
from .config import Config
@@ -40,7 +43,7 @@ def get_integration_file(fn: str) -> str:
40
43
.. seealso::
41
44
42
45
see ``[tool.setuptools.package-data]`` section of pyproject.toml to know
43
- how files are included.
46
+ how files are included.
44
47
"""
45
48
# TODO: use https://docs.python.org/3/library/importlib.resources.html#importlib.resources.files
46
49
prefix = path .abspath (path .dirname (__file__ ))
@@ -62,10 +65,11 @@ def main(argv: List[str] = sys.argv[1:]):
62
65
* (any) wildcard for any snippet""" ),
63
66
)
64
67
parser .add_argument (
65
- '-v' ,
66
68
'--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" ,
69
73
)
70
74
parser .add_argument (
71
75
'-c' , '--config' , default = DEFAULT_CONFIG_FILE , help = 'path to configuration file'
@@ -180,6 +184,16 @@ def main(argv: List[str] = sys.argv[1:]):
180
184
# Parse command line arguments
181
185
args = parser .parse_args (argv )
182
186
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
+
183
197
# Load config from file
184
198
if args .config == DEFAULT_CONFIG_FILE and not path .isfile (DEFAULT_CONFIG_FILE ):
185
199
print (
@@ -223,6 +237,9 @@ def _on_command_stat(args: argparse.Namespace):
223
237
def _filter_list_items (
224
238
cache : Cache , tags : str , docname_glob : str
225
239
) -> Iterable [Tuple [IndexID , Index ]]:
240
+ # NOTE: Importing is slow, do it on demand.
241
+ from sphinx .util .matching import patmatch
242
+
226
243
for index_id , index in cache .indexes .items ():
227
244
# Filter by tags.
228
245
if index [0 ] not in tags and '*' not in tags :
0 commit comments