Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from __future__ import print_function
- from argparse import ArgumentParser
- def action(args):
- print(args)
- if __name__ == '__main__':
- std = ArgumentParser(add_help=False)
- std.add_argument('standard')
- ap = ArgumentParser()
- sp = ap.add_subparsers()
- cmd = sp.add_parser('subcommand', parents=[std], description='Do subcommand')
- cmd.add_argument('arg')
- cmd.set_defaults(do=action)
- args = ap.parse_args()
- args.do(args)
- me@computer$ python test.py
- usage: test.py [-h] {subcommand} ...
- test.py: error: too few arguments
- me@computer$ python3 test.py
- Traceback (most recent call last):
- File "test.py", line 21, in <module>
- args.do(args)
- AttributeError: 'Namespace' object has no attribute 'do'
- ap = ArgumentParser()
- sp = ap.add_subparsers(dest='parser') # dest needed for error message
- sp.required=True # force 'required' testing
- AttributeError: 'Namespace' object has no attribute 'do'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement