Next / Previous / Contents / TCC Help System / NM Tech homepage

28.10.5. The ArgumentParser.parse_args() method

Once you have added all your command line arguments, the call to the .parse_args() method looks like this, where P is your ArgumentParser instance.

P.parse_args(args=None, namespace=None)

The args parameter specifies a set of command line arguments as a list of strings. If you omit this, the command line arguments will be taken from sys.argv.

By default, the returned value will be an instance of class argparse.Namespace. The values returned by parsing the command line will be stored as attributes in this instance. However, you may instead use namespace to specify some instance to which the attributes will be added.

Here's an extended example. This script sets up four command line arguments and then tests it against various simulated argument lists.

#!/usr/bin/env python
from __future__ import print_function
import sys
import argparse

def test(p, argList):
    print("\n=== Test with", argList)
    r = p.parse_args(args=argList)
    print(vars(r))

p = argparse.ArgumentParser(prog='larch',
                            description="Number 1: The Larch")
p.add_argument('-n', '--name', default='Dinsdale',
               help='Name your amoeba')
p.add_argument('-x', '--exec', action='store_true',
               help='Shoot amoeba afterwards')
p.add_argument('in', help='Input file', metavar='INFILE')
p.add_argument('outs', nargs='*', help='Output file(s)',
               metavar='OUTFILE')

print("=== Usage message:")
p.print_usage()

print("\n=== Help message:")
p.print_help()

test(p, ['ingoat'])
test(p, ['-x', 'Brian'])
test(p, ['--exec', 'Brian', 'Reg', 'Dirk'])
test(p, ['-n', 'Brian', 'Reg', 'Dirk'])
test(p, ['--name=Pinnet', 'notlob', 'bolton'])
test(p, ['--nosuch', 'Centurion'])

Output of this script:

=== Usage message:
usage: larch [-h] [-n NAME] [-x] INFILE [OUTFILE [OUTFILE ...]]

=== Help message:
usage: larch [-h] [-n NAME] [-x] INFILE [OUTFILE [OUTFILE ...]]

Number 1: The Larch

positional arguments:
  INFILE                Input file
  OUTFILE               Output file(s)

optional arguments:
  -h, --help            show this help message and exit
  -n NAME, --name NAME  Name your amoeba
  -x, --exec            Shoot amoeba afterwards

=== Test with ['ingoat']
{'in': 'ingoat', 'name': 'Dinsdale', 'outs': [], 'exec': False}

=== Test with ['-x', 'Brian']
{'in': 'Brian', 'name': 'Dinsdale', 'outs': [], 'exec': True}

=== Test with ['--exec', 'Brian', 'Reg', 'Dirk']
{'in': 'Brian', 'name': 'Dinsdale', 'outs': ['Reg', 'Dirk'], 'exec': True}

=== Test with ['-n', 'Brian', 'Reg', 'Dirk']
{'in': 'Reg', 'name': 'Brian', 'outs': ['Dirk'], 'exec': False}

=== Test with ['--name=Pinnet', 'notlob', 'bolton']
{'in': 'notlob', 'name': 'Pinnet', 'outs': ['bolton'], 'exec': False}

=== Test with ['--nosuch', 'Centurion']
usage: larch [-h] [-n NAME] [-x] INFILE [OUTFILE [OUTFILE ...]]
larch: error: unrecognized arguments: --nosuch

Notes on this example: