https://realpython.com/command-line-interfaces-python-argparse/
Table of Contents
One of the strengths of Python is that it comes with batteries included: it has a rich and versatile standard library that makes it one of the best programming languages for writing scripts for the command line. But, if you write scripts for the command line, then you also need to provide a good command line interface, which you can create with the Python argparse
library.
In this article, you’ll learn:
argparse
library is, and why it’s important to use it if you need to write command line scripts in Pythonargparse
library to quickly create a simple CLI in Pythonargparse
library isThis article is written for early intermediate Pythonistas who probably write scripts in Python for their everyday work but have never implemented a command line interface for their scripts.
If that sounds like you, and you’re used to setting variable values at the beginning of your scripts or manually parsing the sys.argv
system list instead of using a more robust CLI development tool, then this article is for you.
Free Bonus: Click here to get access to a chapter from Python Tricks: The Book that shows you Python's best practices with simple examples you can apply instantly to write more beautiful + Pythonic code.
The command line interface (also known as CLI) is a means to interact with a command line script. Python comes with several different libraries that allow you to write a command line interface for your scripts, but the standard way for creating a CLI in Python is currently the Python argparse
library.
The Python argparse
library was released as part of the standard library with Python 3.2 on February the 20th, 2011. It was introduced with Python Enhancement Proposal 389 and is now the standard way to create a CLI in Python, both in 2.7 and 3.2+ versions.
This new module was released as a replacement for the older getopt
and optparse
modules because they were lacking some important features.
The Python argparse
library: