How to design object oriented subparsers for argparse?

前端 未结 1 682
暖寄归人
暖寄归人 2021-01-28 10:49

Problem

I\'m building a package manager that has a lot of sub-commands. I would prefer to have a class structure similar to the following.



        
相关标签:
1条回答
  • 2021-01-28 11:39

    A cmd class and parser:

    import argparse
    
    class Cmd:
      def __init__(self,name):
        self.name = name
    
      def __call__(self, args):
        # do stuff when command is called
        print('Executing %s'%self)
        print('args %s'% args)
    
      def make_sup(self,sp):
          self.parser = sp.add_parser(self.name)
          self.parser.add_argument('--foo')
          self.parser.set_defaults(action=self)
    
      def __repr__(self):
          return '<Cmd> %s'%self.name
    
    cmds = []
    cmds.append(Cmd('list'))
    cmds.append(Cmd('foo'))
    cmds.append(Cmd('bar'))
    print(cmds)
    
    parser = argparse.ArgumentParser()
    sp = parser.add_subparsers(dest='cmd')
    for cmd in cmds:
        cmd.make_sup(sp)
    print(parser._defaults)
    
    args = parser.parse_args()
    print(args)
    args.action(args)
    

    sample runs:

    1834:~/mypy$ python stack46595691.py list --foo xxxx
    [<Cmd> list, <Cmd> foo, <Cmd> bar]
    {}
    Namespace(action=<Cmd> list, cmd='list', foo='xxxx')
    Executing <Cmd> list
    args Namespace(action=<Cmd> list, cmd='list', foo='xxxx')
    1837:~/mypy$ python stack46595691.py bar
    [<Cmd> list, <Cmd> foo, <Cmd> bar]
    {}
    Namespace(action=<Cmd> bar, cmd='bar', foo=None)
    Executing <Cmd> bar
    args Namespace(action=<Cmd> bar, cmd='bar', foo=None)
    1838:~/mypy$ python stack46595691.py foo -h
    [<Cmd> list, <Cmd> foo, <Cmd> bar]
    {}
    usage: stack46595691.py foo [-h] [--foo FOO]
    
    optional arguments:
      -h, --help  show this help message and exit
      --foo FOO
    

    You might also want to look at plac, a package that subclasses ArgumentParser, https://pypi.python.org/pypi/plac

    It can construct subparsers based on the arguments of functions.

    0 讨论(0)
提交回复
热议问题