argparse

from pyugrm import speaker

>>> print(speaker.name)
Niko Wenselowski

>>> print(speaker.job)
Developer at uib gmbh

>>> print(speaker.twitter)
@amokleben

>>> print(speaker.github_url)
https://github.com/okin

>>> print(speaker.notable_things)
Organises the Python Stammtisch Darmstadt

>>> speaker.talk()

argparse

Why?

  • The Future Is Here
  • optparse is deprecated
  • Powerful
  • Parsing the commandline is fun again

Starting

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-s', help='a short option')
parser.add_argument('--long-opt', help='a long option')
parser.add_argument('-b', '--both', help='both')
parser.add_argument('-t', '--tri', '-3', help='and more!')

args = parser.parse_args()
usage: 1.py [-h] [-s S] [--long-opt LONG_OPT] [-b BOTH] [-t TRI]

optional arguments:
  -h, --help            show this help message and exit
  -s S                  a short option
  --long-opt LONG_OPT   a long option
  -b BOTH, --both BOTH  both
  -t TRI, --tri TRI, -3 TRI
                        and more!

Positional arguments

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("first")
parser.add_argument("second")

args = parser.parse_args()
print(args.first)
print(args.second)
usage: 2.py [-h] first second

positional arguments:
  first
  second

optional arguments:
  -h, --help  show this help message and exit
$ python3 2.py eins
usage: 2.py [-h] first second
2.py: error: the following arguments are required: second

Customising an ArgumentParser

parser = argparse.ArgumentParser(prog="program name",
                                 description="Doing amazing things!",
                                 epilog="Still amazing after the help.",
                                 prefix_chars='*')
parser.add_argument('**thingy')
args = parser.parse_args()

print (args.thingy)
$ python3 3.py \*h
usage: program name [*h] [**thingy THINGY]

Doing amazing things!

optional arguments:
  *h, **help       show this help message and exit
  **thingy THINGY

Still amazing after the help.

Customising Arguments

parser = argparse.ArgumentParser()
parser.add_argument('--location', default="CCC FFM HQ")
parser.add_argument('--no-ug', action="store_false", help="Disable usergroup.")
parser.add_argument('-a', '--attendee', dest="attendees", action="append")
args = parser.parse_args()

print (args.attendees)
$ python3 4.py -a foo -a bar
['foo', 'bar']
parser = argparse.ArgumentParser()
parser.add_argument('--speaker', nargs=2)
parser.add_argument('--visitors', type=int)
parser.add_argument('-a', '--attendee', dest="attendees", nargs='*')
args = parser.parse_args()
$ python3 5.py --speaker Niko
usage: 5.py [-h] [--speaker SPEAKER SPEAKER] [--visitors VISITORS]
            [-a [ATTENDEES [ATTENDEES ...]]]
5.py: error: argument --speaker: expected 2 arguments
$ python3 5.py --speaker Niko W. --visitors bla
usage: 5.py [-h] [--speaker SPEAKER SPEAKER] [--visitors VISITORS]
            [-a [ATTENDEES [ATTENDEES ...]]]
5.py: error: argument --visitors: invalid int value: 'bla'
$ python3 5.py -a foo bar baz banane
['foo', 'bar', 'baz', 'banane']
parser = argparse.ArgumentParser()
parser.add_argument('--chaos', choices=['CHAOS', 'nope'])
parser.add_argument('--destroy', action="store_true", required=True)
args = parser.parse_args()
$ python3 6.py -h
usage: 6.py [-h] [--chaos {CHAOS,nope}] --destroy

optional arguments:
  -h, --help            show this help message and exit
  --chaos {CHAOS,nope}
  --destroy
$ python3 6.py --chaos lol
usage: 6.py [-h] [--chaos {CHAOS,nope}] --destroy
6.py: error: argument --chaos: invalid choice: 'lol' (choose from 'CHAOS', 'nope')
$ python3 6.py --chaos CHAOS
usage: 6.py [-h] [--chaos {CHAOS,nope}] --destroy
6.py: error: the following arguments are required: --destroy