1818from xdg .BaseDirectory import xdg_config_home
1919from sphinx .util .matching import patmatch
2020
21- from . import __version__
21+ from . import __version__ , Document
2222from .config import Config
2323from .cache import Cache , IndexID , Index
2424from .table import tablify , COLUMNS
@@ -114,6 +114,9 @@ def main(argv: List[str] = sys.argv[1:]):
114114 getparser .add_argument (
115115 '--file' , '-f' , action = 'store_true' , help = 'get source file path of snippet'
116116 )
117+ getparser .add_argument (
118+ '--deps' , action = 'store_true' , help = 'get dependent files of document'
119+ )
117120 getparser .add_argument (
118121 '--line-start' ,
119122 action = 'store_true' ,
@@ -234,17 +237,36 @@ def _on_command_list(args: argparse.Namespace):
234237
235238
236239def _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+
237248 for index_id in args .index_id :
238249 item = args .cache .get_by_index_id (index_id )
239250 if not item :
240- print ('no such index ID' , file = sys .stderr )
251+ p ('no such index ID' , file = sys .stderr )
241252 sys .exit (1 )
242253 if args .text :
243- print ('\n ' .join (item .snippet .rst ))
254+ p ('\n ' .join (item .snippet .rst ))
244255 if args .docname :
245- print (item .snippet .docname )
256+ p (item .snippet .docname )
246257 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 )
248270 if args .url :
249271 # HACK: get doc id in better way
250272 doc_id , _ = args .cache .index_id_to_doc_id .get (index_id )
@@ -258,11 +280,15 @@ def _on_command_get(args: argparse.Namespace):
258280 url = posixpath .join (base_url , doc_id [1 ] + '.html' )
259281 if item .snippet .refid :
260282 url += '#' + item .snippet .refid
261- print (url )
283+ p (url )
262284 if args .line_start :
263- print (item .snippet .lineno [0 ])
285+ p (item .snippet .lineno [0 ])
264286 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 )
266292
267293
268294def _on_command_integration (args : argparse .Namespace ):
0 commit comments