Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. from argparse import ArgumentParser
  4.  
  5.  
  6. def action(args):
  7. print(args)
  8.  
  9. if __name__ == '__main__':
  10. std = ArgumentParser(add_help=False)
  11. std.add_argument('standard')
  12.  
  13. ap = ArgumentParser()
  14. sp = ap.add_subparsers()
  15.  
  16. cmd = sp.add_parser('subcommand', parents=[std], description='Do subcommand')
  17. cmd.add_argument('arg')
  18. cmd.set_defaults(do=action)
  19.  
  20. args = ap.parse_args()
  21. args.do(args)
  22.  
  23. me@computer$ python test.py
  24. usage: test.py [-h] {subcommand} ...
  25. test.py: error: too few arguments
  26.  
  27. me@computer$ python3 test.py
  28. Traceback (most recent call last):
  29. File "test.py", line 21, in <module>
  30. args.do(args)
  31. AttributeError: 'Namespace' object has no attribute 'do'
  32.  
  33. ap = ArgumentParser()
  34. sp = ap.add_subparsers(dest='parser') # dest needed for error message
  35. sp.required=True # force 'required' testing
  36.  
  37. AttributeError: 'Namespace' object has no attribute 'do'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement