One of the ways to run a Python program is to run it through a CLI (terminal). The following command is used:
python filename.py
Command line arguments are the values supplied to the terminal. For example, filename.py
is a command line argument in the above command.
In Python, among other languages, we can also provide additional command line arguments, other than the file name. For example, arg1
and arg2
are command line arguments in the following command:
python filename.py arg1 arg2 ...
Python has multiple ways to deal with command line arguments. They're explained below:
sys
libraryThe simplest way to access the command line arguments is to use the sys
library. We can use sys.argv
to access the command line arguments in a list. Since it's a list, different elements can be accessed by using the index notation. The first argument, sys.argv[0]
, will always be the filename.
The following code prints the number of arguments and then the actual arguments in a list. It runs the Python file using the following command:
python main.py argument1 argument2_is_hello bye
import sys # import sys library args = sys.argv # save the arguments in a sep var number_of_args = len(args) # no. of arguments print("There are a total of", number_of_args, "arguments provided.") print("The file name is:", args[0]) print("Following is a list of all provided command line arguments:") print(args)
Once we click run, it'll run the Python file using this command and open a terminal.
Note: You may also run it using custom arguments to see how it behaves. Make sure to prepend the command using
python main.py
.
getopt
libraryThe getopt
library can also be used to get command line parameters. This library makes it easier to process and handle arguments that are followed by a flag as it adds them to a list. The library is used in conjunction with sys.argv
. Therefore, we need to import both the getopt
and sys
libraries for it to work.
getopt.getopt(args, options, [full_options])
args
: This is the list of command line arguments that can be obtained from sys.argv
.
options
: This is a string of option letters that we want the function to look for. Option letters for which an argument is necessary must be followed by a colon (:
).
full_options [optional]
: This is an optional parameter that contains a list of long names of the options. Options for which an argument is necessary must be followed by an equal sign (=
).
The following code demonstrates how the function works. Once we click run, it runs the following command in the terminal:
python main.py -u saad -e abcd@xyz.com -p 123456789
Note: You may also run your custom command to see how the code behaves.
from sys import argv # import sys library import getopt # import getopt library username = None email = None phone = None try: args, opt = getopt.getopt(argv[1:], "u:e:p:", ["username =", "email =", "phone ="]) for option, arg in args: if option in ['-u', '--username']: username = arg if option in ['-e', '--email']: email = arg if option in ['-p', '--phone']: phone = arg if not username or not email: print("Please provide all arguments") print("Usage: python main.py -u username -e email [-p phone]") else: print("The details are:") print("Username:", username) print("Email:", email) if phone: print("Phone:", phone) except: print("An error occured!")
argparse
libraryWe can also use the argparse
library to deal with command-line arguments. This library makes it easier to handle more complicated commands and makes the program more user-friendly.
Note: Read more about using
argparse
at this link.
Free Resources