command line arguments that make you smile
DESCRIPTION
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/TRANSCRIPT
![Page 3: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/3.jpg)
$ python --versionPython 2.7.5
![Page 4: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/4.jpg)
$ python --versionPython 2.7.5
![Page 5: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/5.jpg)
UI for your command-line program
![Page 6: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/6.jpg)
Who still writes command-line programs?
![Page 7: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/7.jpg)
Everyone shouldBut we usually just call them scripts
![Page 8: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/8.jpg)
Scripts are awesomeVery few things are actually one-offs
![Page 9: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/9.jpg)
One-off scripts spread knowledge
Similar things can be solved by looking at old scripts
![Page 10: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/10.jpg)
Scripts save you from yourself
![Page 11: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/11.jpg)
$ one-off-script.pyRuntimeError: Stupid mistake
![Page 12: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/12.jpg)
Scripts are much better with arguments
![Page 13: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/13.jpg)
$ one-off-script.py --dry-runRuntimeError: Stupid mistake
![Page 14: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/14.jpg)
... but adding arguments is painful, so we don’t
![Page 15: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/15.jpg)
It doesn't have to bebut first, the status quo:
![Page 16: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/16.jpg)
argparsestandard library recommendation
![Page 17: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/17.jpg)
$ python prog.py -husage: 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)
![Page 18: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/18.jpg)
Let’s parse!
![Page 19: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/19.jpg)
import argparse!parser = argparse.ArgumentParser( description='Process some integers.')!
![Page 20: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/20.jpg)
import argparse!parser = argparse.ArgumentParser( description='Process some integers.')!parser.add_argument( 'integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator')!
![Page 21: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/21.jpg)
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)')!
![Page 22: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/22.jpg)
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()
![Page 23: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/23.jpg)
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()
![Page 24: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/24.jpg)
... and we're done
![Page 25: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/25.jpg)
Phew! !
All that ugly code for:prog.py [-h] [--sum] N [N ...]
![Page 26: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/26.jpg)
There's a better way!Better practices™
![Page 27: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/27.jpg)
docoptmade by Vladimir Keleshev
![Page 28: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/28.jpg)
$ python prog.py -husage: 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)
![Page 29: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/29.jpg)
Let's parse!
![Page 30: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/30.jpg)
"""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)"""
![Page 31: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/31.jpg)
"""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
![Page 32: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/32.jpg)
"""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__)
![Page 33: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/33.jpg)
... and we're done
![Page 34: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/34.jpg)
$ python prog.py --sum 1 2 3{'--help': False, '--sum': True, 'N': ['1', '2', '3']}
![Page 35: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/35.jpg)
$ python prog.py --sum 1 2 3{'--help': False, '--sum': True, 'N': ['1', '2', '3']}!$ python prog.py --helpusage: 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)
![Page 36: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/36.jpg)
Write for humans, let the computer figure it out
![Page 37: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/37.jpg)
Write scripts
!
![Page 38: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/38.jpg)
Write scripts
Use docopt
![Page 39: Command line arguments that make you smile](https://reader034.vdocuments.site/reader034/viewer/2022042613/547b0d58b37959932b8b4d12/html5/thumbnails/39.jpg)
Write scripts
Use docopt
Smile :-)