33import os
44import re
55from typing import List
6-
6+ from inspect import cleandoc
77from mdutils .mdutils import MdUtils
88
99
@@ -23,35 +23,10 @@ def inline_code(code: str) -> str:
2323 return f"`{ code } `"
2424
2525
26- def get_indent (line : str ) -> int :
27- """
28-
29- Get the indent of the file.
30-
31- Args:
32-
33- line (str) : A string whose indent needs to be found
34-
35- Returns:
36-
37- int: Indent
38-
39- """
40- indent = 0
41- for c in line :
42- if c == " " :
43- indent += 1
44- elif c == "\t " :
45- indent += 8
46- else :
47- # break on first word / non-white char
48- break
49- return indent
50-
5126
5227def gen_help (lines : List ) -> None :
5328 """
54- Generate the help given the source code as list of lines
29+ Generate lines of code containing the argument parser and pass it to md_help.
5530
5631 Args:
5732
@@ -62,27 +37,35 @@ def gen_help(lines: List) -> None:
6237 None
6338
6439 """
65- indent = 0
40+ lines_string = ""
41+ lines_string += "import argparse"
42+ lines_string += "\n "
43+ lines_string += "import argmark"
44+ lines_string += "\n "
45+
6646 parser_expr = re .compile (r"(\w+)\.parse_args\(" )
6747 for i , line in enumerate (lines ):
48+ if "ArgumentParser(" in line :
49+ firstline = i
6850 if ".parse_args(" in line :
6951 parser = re .search (parser_expr , line )
7052 if parser is not None :
7153 lastline = i
7254 parser = parser .group (1 )
73- indent = get_indent (line )
7455 break
75- lines = lines [:lastline ]
76- lines .append ("\n " )
77- lines .append (" " * indent + "import argmark" )
78- lines .append (" " * indent + f"argmark.md_help({ parser } )" )
79- logging .debug ("\n " .join (lines ))
80- exec ("\n " .join (lines ), {"__name__" : "__main__" })
56+
57+ lines = lines [firstline :lastline ]
58+
59+ lines_string += cleandoc ("\n " .join (lines ))
60+ lines_string += "\n "
61+ lines_string += f"argmark.md_help({ parser } )"
62+ logging .debug (lines_string )
63+ exec (lines_string , {"__name__" : "__main__" })
8164
8265
8366def md_help (parser : _argparse .ArgumentParser ) -> None :
8467 """
85-
68+ Generate a mardown file from the given argument parser.
8669 Args:
8770 parser: parser object
8871
0 commit comments