How to have sub-parser arguments in separate namespace with argparse?

前端 未结 2 1311
失恋的感觉
失恋的感觉 2021-01-11 21:44

I have the following test-code

import argparse
parser = argparse.ArgumentParser()
parser.add_argument(\"--verbose\", default = 0, type=int)

subparsers = par         


        
2条回答
  •  余生分开走
    2021-01-11 22:06

    You need to go into the bowels of argparse a bit but changing your script to the following should do the trick:

    import argparse
    from argparse import _HelpAction, _SubParsersAction
    
    class MyArgumentParser(argparse.ArgumentParser):
        def parse_args(self, *args, **kw):
            res = argparse.ArgumentParser.parse_args(self, *args, **kw)
            from argparse import _HelpAction, _SubParsersAction
            for x in parser._subparsers._actions:
                if not isinstance(x, _SubParsersAction):
                    continue
                v = x.choices[res.parser_name] # select the subparser name
                subparseargs = {}
                for x1 in v._optionals._actions: # loop over the actions
                    if isinstance(x1, _HelpAction): # skip help
                        continue
                    n = x1.dest
                    if hasattr(res, n): # pop the argument
                        subparseargs[n] = getattr(res, n)
                        delattr(res, n)
                res.subparseargs = subparseargs
            return res
    
    parser = MyArgumentParser()
    parser.add_argument("--verbose", default = 0, type=int)
    
    subparsers = parser.add_subparsers(dest = "parser_name")
    
    parser_lan = subparsers.add_parser('car')
    parser_lan.add_argument("--boo")
    parser_lan.add_argument("--foo")
    
    parser_serial = subparsers.add_parser('bus')
    parser_serial.add_argument("--fun")
    
    print parser.parse_args()
    

提交回复
热议问题