Command line arguments that make you smile

916 views

Published on

Slides from my talk at the Stockholm Python User Group's meetup on Best Practices on October 31st, 2013: http://www.meetup.com/pysthlm/events/145658462/

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
916
On SlideShare
0
From Embeds
0
Number of Embeds
30
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Command line arguments that make you smile

  1. 1. Command line arguments that make you smile ! @martinmelin ! martin@tictail.com
  2. 2. Command line arguments that make you smile :-) ! @martinmelin ! martin@tictail.com
  3. 3. $ python --version Python 2.7.5
  4. 4. $ python --version Python 2.7.5
  5. 5. UI for your command-line program
  6. 6. Who still writes command-line programs?
  7. 7. Everyone should But we usually just call them scripts
  8. 8. Scripts are awesome Very few things are actually one-offs
  9. 9. One-off scripts spread knowledge Similar things can be solved by looking at old scripts
  10. 10. Scripts save you from yourself
  11. 11. $ one-off-script.py RuntimeError: Stupid mistake
  12. 12. Scripts are much better with arguments
  13. 13. $ one-off-script.py --dry-run RuntimeError: Stupid mistake
  14. 14. ... but adding arguments is painful, so we don’t
  15. 15. It doesn't have to be but first, the status quo:
  16. 16. argparse standard library recommendation
  17. 17. $ python prog.py -h usage: prog.py [-h] [--sum] N [N ...] ! Process some integers. ! positional arguments: N an integer for the accumulator ! optional arguments: -h, --help show this help message and exit --sum sum the integers (default: find the max)
  18. 18. Let’s parse!
  19. 19. import argparse ! parser = argparse.ArgumentParser( description='Process some integers.') !
  20. 20. import argparse ! parser = argparse.ArgumentParser( description='Process some integers.') ! parser.add_argument( 'integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator') !
  21. 21. import argparse ! parser = argparse.ArgumentParser( description='Process some integers.') ! parser.add_argument( 'integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator') ! parser.add_argument( '--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers (default: find the max)') !
  22. 22. import argparse ! parser = argparse.ArgumentParser( description='Process some integers.') ! parser.add_argument( 'integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator') ! parser.add_argument( '--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers (default: find the max)') ! args = parser.parse_args()
  23. 23. import argparse ! parser = argparse.ArgumentParser( description='Process some integers.') ! parser.add_argument( 'integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator') ! parser.add_argument( '--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers (default: find the max)') ! args = parser.parse_args()
  24. 24. ... and we're done
  25. 25. Phew! ! All that ugly code for: prog.py [-h] [--sum] N [N ...]
  26. 26. There's a better way! Better practices™
  27. 27. docopt made by Vladimir Keleshev
  28. 28. $ python prog.py -h usage: prog.py [-h] [--sum] N [N ...] ! Process some integers. ! positional arguments: N an integer for the accumulator ! optional arguments: -h, --help show this help message and exit --sum sum the integers (default: find the max)
  29. 29. Let's parse!
  30. 30. """usage: prog.py [-h] [--sum] N [N ...] ! Process some integers. ! positional arguments: N an integer for the accumulator ! optional arguments: -h, --help show this help message and exit --sum sum the integers (default: find the max) """
  31. 31. """usage: prog.py [-h] [--sum] N [N ...] ! Process some integers. ! positional arguments: N an integer for the accumulator ! optional arguments: -h, --help show this help message and exit --sum sum the integers (default: find the max) """ import docopt
  32. 32. """usage: prog.py [-h] [--sum] N [N ...] ! Process some integers. ! positional arguments: N an integer for the accumulator ! optional arguments: -h, --help show this help message and exit --sum sum the integers (default: find the max) """ import docopt ! args = docopt.docopt(__doc__)
  33. 33. ... and we're done
  34. 34. $ python prog.py --sum 1 2 3 {'--help': False, '--sum': True, 'N': ['1', '2', '3']}
  35. 35. $ python prog.py --sum 1 2 3 {'--help': False, '--sum': True, 'N': ['1', '2', '3']} ! $ python prog.py --help usage: prog.py [-h] [--sum] N [N ...] ! Process some integers. ! positional arguments: N an integer for the accumulator ! optional arguments: -h, --help show this help message and exit --sum sum the integers (default: find the max)
  36. 36. Write for humans, let the computer figure it out
  37. 37. Write scripts !
  38. 38. Write scripts Use docopt
  39. 39. Write scripts Use docopt Smile :-)
  40. 40. Thanks! ! @martinmelin ! martin@tictail.com

×