7777import os
7878import sys
7979from pathlib import Path
80+ from functools import partial
8081
8182import sphinx .ext .intersphinx
8283
@@ -156,20 +157,20 @@ def help_examples(s=""):
156157 return s
157158
158159
159- def help_documents ():
160+ def help_documents (parser , args ):
160161 """
161162 Append and return a tabular list of documents, including a
162163 shortcut 'all' for all documents, available to the Sage
163164 documentation builder.
164165 """
165- docs = get_documents ()
166+ args .source_dir = preprocess_source_dir (parser , args .source_dir )
167+ docs = [str (p ) for p in get_all_documents (args .source_dir )]
166168 s = "DOCUMENTs:\n "
167169 s += format_columns (docs )
168170 s += "\n "
169- if 'reference' in docs :
170- s += "Other valid document names take the form 'reference/DIR', where\n "
171- s += "DIR is a subdirectory of src/doc/en/reference/.\n "
172- s += "This builds just the specified part of the reference manual.\n "
171+ s += "Other valid document names take the form 'en/reference/DIR', where\n "
172+ s += "DIR is a subdirectory of src/doc/en/reference/.\n "
173+ s += "This builds just the specified part of the reference manual.\n "
173174 s += "DOCUMENT may also have the form 'file=/path/to/FILE', which builds\n "
174175 s += "the documentation for the specified file.\n "
175176 return s
@@ -220,7 +221,7 @@ class help_message_long(argparse.Action):
220221 and exits.
221222 """
222223 def __call__ (self , parser , namespace , values , option_string = None ):
223- help_funcs = [help_usage , help_description , help_documents ,
224+ help_funcs = [help_usage , help_description , functools . partial ( help_documents , parser , namespace ) ,
224225 help_formats , help_commands ]
225226 for f in help_funcs :
226227 print (f ())
@@ -253,7 +254,7 @@ class help_wrapper(argparse.Action):
253254 """
254255 def __call__ (self , parser , namespace , values , option_string = None ):
255256 if option_string in ['-D' , '--documents' ]:
256- print (help_documents (), end = "" )
257+ print (help_documents (parser , namespace ), end = "" )
257258 if option_string in ['-F' , '--formats' ]:
258259 print (help_formats (), end = "" )
259260 if self .dest == 'commands' :
@@ -262,6 +263,15 @@ def __call__(self, parser, namespace, values, option_string=None):
262263 sys .exit (0 )
263264
264265
266+ def preprocess_source_dir (parser , val ):
267+ if val is None :
268+ val = Path (os .environ .get ('SAGE_DOC_SRC' , 'src/doc' ))
269+ p = val .absolute ()
270+ if not p .is_dir ():
271+ parser .error (f"Source directory { p } does not exist." )
272+ return p
273+
274+
265275def setup_parser ():
266276 """
267277 Set up and return a command-line ArgumentParser instance for the
@@ -452,14 +462,8 @@ def main():
452462 # Parse the command-line.
453463 parser = setup_parser ()
454464 args : BuildOptions = parser .parse_args () # type: ignore
465+ args .source_dir = preprocess_source_dir (parser , args .source_dir )
455466
456- # Check that the docs source directory exists
457- if args .source_dir is None :
458- args .source_dir = Path (os .environ .get ('SAGE_DOC_SRC' , 'src/doc' ))
459- args .source_dir = args .source_dir .absolute ()
460- if not args .source_dir .is_dir ():
461- parser .error (f"Source directory { args .source_dir } does not exist." )
462-
463467 if args .all_documents :
464468 if args .all_documents == 'reference' :
465469 docs = get_all_reference_documents (args .source_dir / 'en' )
0 commit comments