python argparse - add action to subparser with no arguments?

前端 未结 1 1836
猫巷女王i
猫巷女王i 2020-12-30 04:39

I am adding subparsers to my parser to simulate subcommands functionality (for example code see: Simple command line application in python - parse user input?). Now I want t

1条回答
  •  伪装坚强ぢ
    2020-12-30 04:55

    The documentation for subcommands gives two examples of how to identify the subparser.

    https://docs.python.org/dev/library/argparse.html#sub-commands

    One is to give the add_subparsers a dest:

    def do_quit(args):
        # action
        quit()
    
    parser = ArgumentParser()
    subparser = parser.add_subparsers(dest='cmd')
    ....
    subparser.add_parser('quit')
    ...
    args = parser.parse_args()
    print args.cmd   # displays 'quit'
    if args.cmd == 'quit':
       do_quit(args)
    

    the other is to use set_defaults to link the subparser with a function:

    parser = ArgumentParser()
    subparsers = parser.add_subparsers()
    ...
    parser_quit = subparsers.add_parser('quit')
    parser_quit.set_defaults(func=do_quit)
    ...
    args = parser.parse_args()
    args.func(args)
    

    On further thought, here's a way, using a custom Action. It is like _HelpAction (which is used by a -h). It's called by a positional argument with nargs=0 (or '?'). Such an argument is always called, even though there are no strings to match it (or rather, 0 strings match it). This a logical, but somewhat obscure, consequence of how positionals are handled.

    class QuitAction(argparse.Action):
        def __call__(self, parser, *args, **kwargs):
            parser.exit(message="QUITTING\n")
    
    p=argparse.ArgumentParser()
    sp=p.add_subparsers(dest='cmd')
    p1=sp.add_parser('quit')
    p1.add_argument('foo', action=QuitAction, nargs='?', help=argparse.SUPPRESS)
    p.parse_args(['quit'])
    

    producing (when run in Ipython):

    QUITTING
    An exception has occurred, use %tb to see the full traceback.    
    SystemExit: 0
    

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