Top Banner
Command line arguments that make you smile @martinmelin [email protected]
40

Command line arguments that make you smile

Nov 29, 2014

Download

Technology

Martin Melin

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/
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Command line arguments that make you smile

Command line arguments that make you smile

!@martinmelin

[email protected]

Page 2: Command line arguments that make you smile

Command line arguments that make you smile :-)

!@martinmelin

[email protected]

Page 3: Command line arguments that make you smile

$ python --versionPython 2.7.5

Page 4: Command line arguments that make you smile

$ python --versionPython 2.7.5

Page 5: Command line arguments that make you smile

UI for your command-line program

Page 6: Command line arguments that make you smile

Who still writes command-line programs?

Page 7: Command line arguments that make you smile

Everyone shouldBut we usually just call them scripts

Page 8: Command line arguments that make you smile

Scripts are awesomeVery few things are actually one-offs

Page 9: Command line arguments that make you smile

One-off scripts spread knowledge

Similar things can be solved by looking at old scripts

Page 10: Command line arguments that make you smile

Scripts save you from yourself

Page 11: Command line arguments that make you smile

$ one-off-script.pyRuntimeError: Stupid mistake

Page 12: Command line arguments that make you smile

Scripts are much better with arguments

Page 13: Command line arguments that make you smile

$ one-off-script.py --dry-runRuntimeError: Stupid mistake

Page 14: Command line arguments that make you smile

... but adding arguments is painful, so we don’t

Page 15: Command line arguments that make you smile

It doesn't have to bebut first, the status quo:

Page 16: Command line arguments that make you smile

argparsestandard library recommendation

Page 17: Command line arguments that make you smile

$ 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

Let’s parse!

Page 19: Command line arguments that make you smile

import argparse!parser = argparse.ArgumentParser( description='Process some integers.')!

Page 20: Command line arguments that make you smile

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

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

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

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

... and we're done

Page 25: Command line arguments that make you smile

Phew! !

All that ugly code for:prog.py [-h] [--sum] N [N ...]

Page 26: Command line arguments that make you smile

There's a better way!Better practices™

Page 27: Command line arguments that make you smile

docoptmade by Vladimir Keleshev

Page 28: Command line arguments that make you smile

$ 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

Let's parse!

Page 30: Command line arguments that make you smile

"""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

"""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

"""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

... and we're done

Page 34: Command line arguments that make you smile

$ python prog.py --sum 1 2 3{'--help': False, '--sum': True, 'N': ['1', '2', '3']}

Page 35: Command line arguments that make you smile

$ 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

Write for humans, let the computer figure it out

Page 37: Command line arguments that make you smile

Write scripts

!

Page 38: Command line arguments that make you smile

Write scripts

Use docopt

Page 39: Command line arguments that make you smile

Write scripts

Use docopt

Smile :-)

Page 40: Command line arguments that make you smile

Thanks!!

@martinmelin !

[email protected]