18
18
from xdg .BaseDirectory import xdg_config_home
19
19
from sphinx .util .matching import patmatch
20
20
21
- from . import __version__
21
+ from . import __version__ , Document
22
22
from .config import Config
23
23
from .cache import Cache , IndexID , Index
24
24
from .table import tablify , COLUMNS
@@ -114,6 +114,9 @@ def main(argv: List[str] = sys.argv[1:]):
114
114
getparser .add_argument (
115
115
'--file' , '-f' , action = 'store_true' , help = 'get source file path of snippet'
116
116
)
117
+ getparser .add_argument (
118
+ '--deps' , action = 'store_true' , help = 'get dependent files of document'
119
+ )
117
120
getparser .add_argument (
118
121
'--line-start' ,
119
122
action = 'store_true' ,
@@ -234,17 +237,36 @@ def _on_command_list(args: argparse.Namespace):
234
237
235
238
236
239
def _on_command_get (args : argparse .Namespace ):
240
+ # Wrapper for warning when nothing is printed
241
+ printed = False
242
+
243
+ def p (* args , ** opts ):
244
+ nonlocal printed
245
+ printed = True
246
+ print (* args , ** opts )
247
+
237
248
for index_id in args .index_id :
238
249
item = args .cache .get_by_index_id (index_id )
239
250
if not item :
240
- print ('no such index ID' , file = sys .stderr )
251
+ p ('no such index ID' , file = sys .stderr )
241
252
sys .exit (1 )
242
253
if args .text :
243
- print ('\n ' .join (item .snippet .rst ))
254
+ p ('\n ' .join (item .snippet .rst ))
244
255
if args .docname :
245
- print (item .snippet .docname )
256
+ p (item .snippet .docname )
246
257
if args .file :
247
- print (item .snippet .file )
258
+ p (item .snippet .file )
259
+ if args .deps :
260
+ if not isinstance (item .snippet , Document ):
261
+ print (
262
+ f'{ type (item .snippet )} ({ index_id } ) is not a document' ,
263
+ file = sys .stderr ,
264
+ )
265
+ sys .exit (1 )
266
+ if len (item .snippet .deps ) == 0 :
267
+ p ('' ) # prevent print nothing warning
268
+ for dep in item .snippet .deps :
269
+ p (dep )
248
270
if args .url :
249
271
# HACK: get doc id in better way
250
272
doc_id , _ = args .cache .index_id_to_doc_id .get (index_id )
@@ -258,11 +280,15 @@ def _on_command_get(args: argparse.Namespace):
258
280
url = posixpath .join (base_url , doc_id [1 ] + '.html' )
259
281
if item .snippet .refid :
260
282
url += '#' + item .snippet .refid
261
- print (url )
283
+ p (url )
262
284
if args .line_start :
263
- print (item .snippet .lineno [0 ])
285
+ p (item .snippet .lineno [0 ])
264
286
if args .line_end :
265
- print (item .snippet .lineno [1 ])
287
+ p (item .snippet .lineno [1 ])
288
+
289
+ if not printed :
290
+ print ('please specify at least one argument' , file = sys .stderr )
291
+ sys .exit (1 )
266
292
267
293
268
294
def _on_command_integration (args : argparse .Namespace ):
0 commit comments