An Introduction to Python Command-Line Arguments

Athena Ozanich
Athena Ozanich


Python includes a command-line for handling user input and certain types of data entry during the execution of Python programs. This enables more robust tasks and more interactivity with the program, allowing users to enter data and perform otherwise impossible tasks.

Woman watching a video on the basics behind Python command-line arguments and how to understand them.

In this post, you will learn what the Python command-line interface (CLI) is and how it improves the user experience and your software. You will also learn how to use it, add it to your software, and improve its performance. Furthermore, you will see some code examples implementing CLI arguments and how to create your own.

Let’s get started.

Download Now: An Introduction to Python [Free Guide]

Command-line Argument in Python

A command line interface (CLI) is a program that is designed to allow users to interact with software on their computer. Many kinds of software have specific tasks that run and perform based on keywords and variables that together create a command line argument. A command-line argument is an argument that gets passed into a program through the CLI by the user. If you aren't familiar with Python you should take a look at our post discussing the Python basics.

How to Use a Command-Line Argument in Python

This process is done through the argparse Python library, which is included in the standard library. To use the CLI arguments library, you simply need to import it into your code; this is done like any other Python import.

/* Import the library */
import argparse

With that added to your code, you will be ready to start using it. Once you have imported the library, you will need to make a few more changes to prepare your software to accept CLI arguments.

/*Create the parser*/
parser = argparse.ArgumentParser()

This line of code will create an instance of the argparse. However, the argparse still needs more information to operate in a meaningful way. Next, you will need to add an argument for the argument parser to accept input from the user.

/*Add an argument*/
parser.add_argument('--firstName', type=str, required=True)

This line of code dictates a new argument for the parser to accept. The add_argument() method takes three parameters, the first of which is what the argument should be called. In this case, the code creates an argument called --firstName. The second is the type parameter, and it is used to specify what type of data the argument will be; in this example, it is the type of string. The final parameter dictates if the argument is required for the software to run, and it is called the required. It only accepts boolean values of true or false.

/*Parse the argument*/
args = parser.parse_args()

This line of code tells the program to check and parse the arguments if any were provided. If required arguments are not present it will throw an error telling the user to add the argument accordingly.

/*Print "Hello" + the user input argument*/
print('Hello,', args.firstName)

This line of code tells the program what to do with the arguments provided. In this example, the program should print the word “Hello” and the provided firstName argument.

How to Read a Command-Line Argument in Python

Command-line arguments can be broken down into three main pieces, the first is the command itself, and typically, this invokes some process or runs a program. This part of the command line would look like the following in our example.

With this line of code, we are telling Python to run the file named; this will instruct the program to run and complete whatever tasks the program is meant to run. However, if you run this, you would get an error message due to the missing --firstName argument.

--firstName Castiel

This bit of code will provide the argument required by the program and pass the name Castiel as its value. To finish this example off, let's look at the full line of code needed to run the program with its required argument.

python --name Castiel

Using Command-Line Arguments in Your Software

By this point, you have learned the basics of CLI arguments in Python and are familiar with the syntax and setting up the argparse library. You also know how to create your arguments and make them required for software that needs input from the user to run correctly.


Topics: What Is Python?

Related Articles

We're committed to your privacy. HubSpot uses the information you provide to us to contact you about our relevant content, products, and services. You may unsubscribe from these communications at any time. For more information, check out our Privacy Policy.

A guide for marketers, developers, and data analysts.