Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Command line arguments that make you smile

Command line arguments that make you smile

Slides from my talk at the Stockholm Python User Group's meetup on best practices. Arguing that everyone should write more scripts, and when they do they should use the excellent docopt.

Martin Melin

October 31, 2013
Tweet

Other Decks in Programming

Transcript

  1. $ 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)
  2. import argparse ! parser = argparse.ArgumentParser( description='Process some integers.') !

    parser.add_argument( 'integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator') !
  3. 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)') !
  4. 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()
  5. 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()
  6. $ 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)
  7. """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) """
  8. """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
  9. """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__)
  10. $ 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)