Skip to content

Commit d87b08f

Browse files
committed
MNT: use argparse
Drop options.
1 parent 33b229b commit d87b08f

File tree

1 file changed

+49
-56
lines changed

1 file changed

+49
-56
lines changed

src/sphinx_autodoc/main.py

+49-56
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
import argparse
12
import importlib
23
import inspect
3-
import optparse
44
import os
55
import pkgutil
66
import re
77
import shutil
88
import textwrap
9-
import sys
109

1110
pj = os.path.join
1211

@@ -285,138 +284,132 @@ def main():
285284
"""
286285
)
287286

288-
parser = optparse.OptionParser(
289-
usage=textwrap.dedent(
290-
"""\
291-
usage: %prog [options] <package>
287+
parser = argparse.ArgumentParser()
292288

293-
Arguments:
294-
package : The name of the package to walk (e.g 'scipy')
295-
"""
296-
)
289+
parser.add_argument(
290+
"package", help="The name of the package to walk (e.g. 'scipy')"
297291
)
298292

299-
parser.add_option(
293+
parser.add_argument(
300294
"-s",
301295
"--source",
302296
action="store",
303-
help="sphinx source dir below which all rst files will \
304-
be written [%default]",
297+
help="""sphinx source dir below which all rst files will
298+
be written [%(default)s]""",
305299
default="source",
306300
)
307-
parser.add_option(
301+
parser.add_argument(
308302
"-a",
309303
"--apipath",
310304
action="store",
311305
help="""dir (relative to SOURCE) for generated API rst
312-
files, written by default, may be
313-
turned off by --no-write-api [%default]""",
306+
files, written by default, may be
307+
turned off by --no-write-api [%(default)s]""",
314308
default="generated/api",
315309
)
316-
parser.add_option(
310+
parser.add_argument(
317311
"-d",
318312
"--docpath",
319313
action="store",
320314
help="""dir (relative to SOURCE) for extra generated rst
321-
files for doc strings pulled from modules, use with
322-
--write-doc, off by default [%default]""",
315+
files for doc strings pulled from modules, use with
316+
--write-doc, off by default [%(default)s]""",
323317
default="generated/doc",
324318
)
325-
parser.add_option(
319+
parser.add_argument(
326320
"-w",
327321
"--writtenpath",
328322
action="store",
329-
help="""dir (relative to SOURCE) for hand written rst
330-
files, an index.rst file must exist there, only needed
331-
with --write-index [%default]""",
323+
help="""dir (relative to SOURCE) with hand written rst
324+
files, an index.rst file must exist there, this will be added
325+
to SOURCE/index.rst if found, only needed
326+
with --write-index [%(default)s]""",
332327
default="written",
333328
)
334-
parser.add_option(
329+
parser.add_argument(
335330
"-i",
336331
"--write-index",
337332
action="store_true",
338-
help="""(over)write SOURCE/index.rst, not written by
339-
default""",
333+
help="""(over)write SOURCE/index.rst (a backup is made), not written by
334+
default""",
340335
default=False,
341336
)
342-
parser.add_option(
343-
"",
337+
parser.add_argument(
344338
"--write-doc",
345339
action="store_true",
346340
help="""(over)write SOURCE/DOCPATH""",
347341
default=False,
348342
)
349-
parser.add_option(
350-
"",
343+
parser.add_argument(
351344
"--no-write-api",
352345
action="store_false",
353346
dest="write_api",
354347
help="""don't (over)write SOURCE/APIPATH""",
355348
default=True,
356349
)
357-
parser.add_option(
350+
parser.add_argument(
358351
"-X",
359352
"--exclude",
360353
help="""regex for excluding modules, applied to the full
361-
module name [%default]""",
354+
module name [%(default)s]""",
362355
default=None,
363356
)
364357

365-
(opts, args) = parser.parse_args(sys.argv[1:])
358+
args = parser.parse_args()
366359

367-
package_name = args[0]
360+
package_name = args.package
368361
bar = "=" * len(package_name)
369362

370-
print("processing package: %s" % package_name)
363+
print(f"processing package: {package_name}")
371364
package = importlib.import_module(package_name)
372365
mods = [
373366
Module(
374367
name,
375-
source=opts.source,
376-
apipath=opts.apipath,
377-
docpath=opts.docpath,
368+
source=args.source,
369+
apipath=args.apipath,
370+
docpath=args.docpath,
378371
)
379372
for name in walk_package(package)
380373
]
381374

382-
if opts.exclude is not None:
383-
rex = re.compile(opts.exclude)
375+
if args.exclude is not None:
376+
rex = re.compile(args.exclude)
384377
mods = [mod for mod in mods if rex.search(mod.name) is None]
385378

386379
modules_api = ""
387380
modules_doc = ""
388381

389-
if opts.write_api:
390-
os.makedirs(pj(opts.source, opts.apipath), exist_ok=True)
391-
if opts.write_doc:
392-
os.makedirs(pj(opts.source, opts.docpath), exist_ok=True)
382+
if args.write_api:
383+
os.makedirs(pj(args.source, args.apipath), exist_ok=True)
384+
if args.write_doc:
385+
os.makedirs(pj(args.source, args.docpath), exist_ok=True)
393386

394387
print("modules:")
395388
for mod in mods:
396389
print(" %s" % mod.name)
397-
if opts.write_api:
390+
if args.write_api:
398391
mod.write_api()
399392
modules_api += format_name(mod.fullbasename) + "\n"
400-
if opts.write_doc and mod.has_doc:
393+
if args.write_doc and mod.has_doc:
401394
mod.write_doc()
402395
modules_doc += format_name(mod.fullbasename) + "\n"
403396

404-
if opts.write_api:
397+
if args.write_api:
405398
txt = api_index_templ.format(modules_api=modules_api)
406-
file_write(pj(opts.source, opts.apipath, "index.rst"), txt)
399+
file_write(pj(args.source, args.apipath, "index.rst"), txt)
407400

408-
if opts.write_doc:
401+
if args.write_doc:
409402
txt = doc_index_templ.format(modules_doc=modules_doc)
410-
file_write(pj(opts.source, opts.docpath, "index.rst"), txt)
403+
file_write(pj(args.source, args.docpath, "index.rst"), txt)
411404
index_templ = re.sub(
412405
"__docpath_index_entry__", "{docpath}/index", index_templ
413406
)
414407
else:
415408
index_templ = re.sub("__docpath_index_entry__", "", index_templ)
416409

417-
if opts.write_index:
418-
index_fn = pj(opts.source, "index.rst")
419-
w_pth = pj(opts.source, opts.writtenpath)
410+
if args.write_index:
411+
index_fn = pj(args.source, "index.rst")
412+
w_pth = pj(args.source, args.writtenpath)
420413
w_index_fn = pj(w_pth, "index.rst")
421414
print("overwriting main index: %s" % index_fn)
422415
if os.path.exists(w_pth):
@@ -426,9 +419,9 @@ def main():
426419
)
427420
index_templ += written_index_templ
428421
txt = index_templ.format(
429-
apipath=opts.apipath,
430-
docpath=opts.docpath,
431-
writtenpath=opts.writtenpath,
422+
apipath=args.apipath,
423+
docpath=args.docpath,
424+
writtenpath=args.writtenpath,
432425
package_name=package_name,
433426
bar=bar,
434427
)

0 commit comments

Comments
 (0)