argparse

noun

say it: “ARG-pars” /ˈɑrɡpɑrs/

Python's built-in module for reading the words typed after a script's name, checking them and making a --help page.

What is argparse?

argparse is Python's standard module for a script's command-line arguments. You describe each argument, option and flag, and it reads them, checks them and writes a --help page.

import argparse
parser = argparse.ArgumentParser(description="Sort a folder by file type.")
parser.add_argument("folder")
parser.add_argument("--dry-run", action="store_true")
args = parser.parse_args()
print(args.folder, args.dry_run)   # python sort.py Downloads --dry-run -> Downloads True

In a sentence

  • “argparse stops the script with a clear message when the folder is missing.”
  • “With argparse, typing --help shows how to use the sorter.”

Level: Journeyman · More Python terms at this level

Updated