Skip to content

Commit 1e9d64d

Browse files
committed
support doc for multi variants
1 parent 6264648 commit 1e9d64d

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

dargs/dargs.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,9 @@ def gen_doc_body(self, paths: Optional[List[str]] = None, **kwargs) -> str:
335335
for subarg in self.sub_fields.values():
336336
body_list.append(subarg.gen_doc(paths, **kwargs))
337337
if self.sub_variants:
338+
showflag = len(self.sub_variants) > 1
338339
for subvrnt in self.sub_variants.values():
339-
body_list.append(subvrnt.gen_doc(paths, **kwargs))
340+
body_list.append(subvrnt.gen_doc(paths, showflag, **kwargs))
340341
body = "\n".join(body_list)
341342
return body
342343

@@ -439,13 +440,15 @@ def _convert_choice_alias(self, argdict: dict):
439440
# above are traversing part
440441
# below are doc generation part
441442

442-
def gen_doc(self, paths: Optional[List[str]] = None, **kwargs) -> str:
443+
def gen_doc(self, paths: Optional[List[str]] = None,
444+
showflag : bool = False, **kwargs) -> str:
443445
body_list = [""]
444446
body_list.append(f"Depending on the value of *{self.flag_name}*, "
445447
"different sub args are accepted. \n")
446448
body_list.append(self.gen_doc_flag(paths, **kwargs))
447449
for choice in self.choice_dict.values():
448-
c_str = f"[{choice.name}]"
450+
f_str = f"{self.flag_name}=" if showflag else ""
451+
c_str = f"[{f_str}{choice.name}]"
449452
choice_path = [*paths[:-1], paths[-1]+c_str] if paths else [c_str]
450453
body_list.append("")
451454
if kwargs.get("make_anchor"):

tests/test_docgen.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,50 @@ def test_sub_variants(self):
6868
docstr = ca.gen_doc(make_anchor=True)
6969
# print("\n\n"+docstr)
7070

71+
def test_multi_variants(self):
72+
ca = Argument("base", dict, [
73+
Argument("sub1", int),
74+
Argument("sub2", str)
75+
], [
76+
Variant("vnt_flag", [
77+
Argument("type1", dict, [
78+
Argument("shared", int),
79+
Argument("vnt1_1", int),
80+
Argument("vnt1_2", dict, [
81+
Argument("vnt1_1_1", int)
82+
])
83+
]),
84+
Argument("type2", dict, [
85+
Argument("shared", int),
86+
Argument("vnt2_1", int),
87+
]),
88+
Argument("type3", dict, [
89+
Argument("vnt3_1", int)
90+
], [ # testing cascade variants here
91+
Variant("vnt3_flag1", [
92+
Argument("v3f1t1", dict, [
93+
Argument('v3f1t1_1', int),
94+
Argument('v3f1t1_2', int)
95+
]),
96+
Argument("v3f1t2", dict, [
97+
Argument('v3f1t2_1', int)
98+
])
99+
]),
100+
Variant("vnt3_flag2", [
101+
Argument("v3f2t1", dict, [
102+
Argument('v3f2t1_1', int),
103+
Argument('v3f2t1_2', int)
104+
]),
105+
Argument("v3f2t2", dict, [
106+
Argument('v3f2t2_1', int)
107+
])
108+
])
109+
])
110+
])
111+
])
112+
docstr = ca.gen_doc()
113+
# print("\n\n"+docstr)
114+
71115
def test_dpmd(self):
72116
from dpmdargs import gen_doc
73117
docstr = gen_doc(make_anchor=True)

0 commit comments

Comments
 (0)