[docs]defkwdict_argparse(ap,kwdict,groups_kw=dict()):groups=dict()groups_prio=dict()forgname,gdictingroups_kw.items():APpriority=gdict.get("APpriority",None)ifAPpriorityisnotNone:groups_prio[gname]=APpriorityforhname,hdictinkwdict.items():ifhdict.get("APignore",False):continuegroup=hdict.get("APgroup",None)s=groups.setdefault(group,set())s.add(hname)gprio=groups_prio.get(group,float("inf"))hprio=hdict.get("APpriority",float("inf"))ifhprio<gprio:gprio=hpriogroups_prio[group]=gpriognames=list(groups_prio.keys())gnames.sort(key=lambdag:(groups_prio[g],g)ifgroups_prio[g]isnotNoneelsefloat("inf"))forgnameingnames:hset=groups[gname]gkw=dict()ifgnameisNone:apG=apelse:gdict=groups_kw.get(gname,None)ifgdictisnotNone:ghelp=gdict.get("about",None)ifghelpisnotNone:gkw["description"]=padding_remove(ghelp)apG=ap.add_argument_group(gname,**gkw)hlist=list(hset)hlist.sort(key=lambdah:(kwdict[h].get("APpriority",float("inf")),h))forhnameinhlist:hdict=kwdict[hname]name=hdict.get(hname,hname)APshort=hdict.get("APshort",None)APlong="--{}".format(name)APflags=list(hdict.get("APflags",[APlong]))ifAPshortisnotNone:APflags=[APshort]+APflagsAPhide=hdict.get("APhide",False)ifAPhide:helptext=argparse.SUPPRESSelse:helptext=hdict.get("about","<needs doc>").replace("%","%%")helptext=padding_remove(helptext)APkw=dict(dest=name,help=helptext,)APaction=hdict.get("APaction",None)ifAPactionisnotNone:APkw["action"]=APactionAPtype=hdict.get("APtype",None)ifAPtypeisnotNone:APkw["type"]=APtypeAPrequired=hdict.get("APrequired",None)ifAPrequiredisnotNone:APkw["required"]=APrequiredAPnargs=hdict.get("APnargs",None)ifAPnargsisnotNone:APkw["nargs"]=APnargsAPchoices=hdict.get("APchoices ",None)ifAPchoicesisnotNone:APkw["choices "]=APchoicesAPmetavar=hdict.get("APmetavar",None)ifAPmetavarisnotNone:APkw["metavar"]=APmetavarAPchoices=hdict.get("APchoices",None)ifAPchoicesisnotNone:APkw["choices"]=APchoicesAPconst=hdict.get("APconst",None)ifAPconstisnotNone:APkw["const"]=APconstAPdefault=hdict.get("APdefault",None)ifAPdefaultisnotNone:APkw["default"]=APdefaultelse:APkw["default"]=argparse.SUPPRESSAPpositional=hdict.get("APpositional",False)ifnotAPpositional:apG.add_argument(*APflags,**APkw)# add the aliases to parse, but with their help suppressedforanameinhdict.get("aliases",[]):APkw["help"]=argparse.SUPPRESSapG.add_argument("--{}".format(aname),**APkw)foranameinhdict.get("aliases_bad",[]):APkw["help"]=argparse.SUPPRESSapG.add_argument("--{}".format(aname),**APkw)else:dest=APkw.pop("dest")apG.add_argument(dest,**APkw)returnap