Nested commands with argparse

Given the following argparse setup:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def foo_bar():
  print('Foobar')

parser = argparse.ArgumentParser(prog='app')
subparsers = parser.add_subparsers()

foo = subparsers.add_parser('foo')
foo_subparsers = foo.add_subparsers()

bar = foo_subparsers.add_parser('bar')
bar.set_defaults(func=foo_bar)

args = parser.parse_args()
args.func(args)

I can invoke the nested sub-command like this:

1
2
% app foo bar
Foobar