Python 3 – Command Line Arguments
Command-line arguments are the values passed to the program during the execution of the program. These arguments are provided when the program is called and can be used to control the behavior of the program. In Python 3, it is very easy to work with command-line arguments, and we can access them from the sys
module.
The sys
module provides access to some variables used by the interpreter and to functions that interact strongly with the interpreter. One of the functions included in the sys
module is sys.argv
, which returns a list containing the command-line arguments passed to the Python script. sys.argv
is an array in Python that contains all the arguments passed to the script. The first element of the list, sys.argv[0]
, is the name of the program itself.
How to Use Command-Line Arguments in Python?
Before providing the command-line arguments, let’s create a simple Python script to work with:
import sys
print("Hello, {}!".format(sys.argv[1]))
In the above example, we import the sys
module and then use the print
function to print a message containing the first command-line argument provided to the script. The {}
is a placeholder that will be replaced with the value of sys.argv[1]
.
Now let’s execute the script and provide a command-line parameter:
$ python3 script.py John
The output of the above command will be:
Hello, John!
In the above command, we pass John
as a first argument to the script.
Using Multiple Command-Line Arguments
We can also provide multiple arguments to the script, and all of them will be available in the sys.argv
list. Let’s create a script that prints out all the command-line arguments provided to it:
import sys
for arg in sys.argv[1:]:
print(arg)
In the above example, we are looping through all arguments starting from the second and printing out each argument.
Let’s execute the script with the following command:
$ python3 script.py arg1 arg2 arg3
The output of this command will be:
arg1
arg2
arg3
As you can see, all arguments are printed one by one.
Using Flags with Command-Line Arguments
We can also use flags with command-line arguments to control the behavior of the program. Flags are a way of providing a set of options to a program. To use flags, we can use the argparse
library.
argparse
is a module in Python that makes it easy to write user-friendly command-line interfaces. The argparse
module also automatically generates help and usage messages and issues errors when users give the program invalid arguments.
Let’s see an example of using flags with argparse
:
import argparse
# create parser object
parser = argparse.ArgumentParser()
# add argument
parser.add_argument("-v", "--verbose", help="increase output verbosity", action="store_true")
parser.add_argument("name", help="your name")
# parse the arguments
args = parser.parse_args()
# print the argument
if args.verbose:
print("Hello, {}! Welcome to our website.".format(args.name))
else:
print("Hello, {}!".format(args.name))
In the above example, we create a parser object using the argparse.ArgumentParser()
method. We then add two arguments to our parser object, the first one is a flag -v
that sets the args.verbose
variable to True
, and the second argument is a string that will be displayed when the help
option is used.
Then we parse the arguments using the parser.parse_args()
method. This method returns an args
object that contains the values of our arguments.
Finally, we print out the argument args.name
and check if the args.verbose
flag is set to True, in which case we display an additional greeting.
Let’s run this script with different parameters:
$ python3 script.py John -v
Hello, John! Welcome to our website.
In the above command, we have passed -v
as an option, so the verbose message is printed.
$ python3 script.py James
Hello, James!
In the above command, we have not passed any options, so the regular message is printed.
Conclusion
In conclusion, Python 3 provides a very easy way to work with command-line arguments. Command-line arguments can be used to control the behavior of the program and provide additional options to the user. Using the sys
module or the argparse
library, we can easily access and parse command-line arguments in Python 3.