Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Positional argument

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('name', help='your full name')
args = parser.parse_args()

print(args.name)
$ python argparse_positional.py
usage: argparse_positional.py [-h] name
argparse_positional.py: error: too few arguments
$ python argparse_positional.py -h
usage: argparse_positional.py [-h] name

positional arguments:
  name        your full name

optional arguments:
  -h, --help  show this help message and exit
$ python argparse_positional.py Foo
Foo
$ python argparse_positional.py Foo Bar
usage: argparse_positional.py [-h] name
argparse_positional.py: error: unrecognized arguments: Bar
$ python argparse_positional.py "Foo Bar"
Foo Bar