Python Command Line Arguments: Part 1: argv

Python Command Line Arguments: Part 1: argv

Command-line arguments. What are they, really? Why would you ever use them?

Command-line arguments are a way of reducing hard-coded variables in a program. In this case, if your program takes in user input every now and then and does something to it, you can avoid the need for a prompt for instance:

Enter your first name: 
Enter your second name:

Instead, you can simply run the program like this;

python user_name.py John Doe

Here, the program identifies the first name as John and second as Doe without you having to implicitly write two input statements in your code, assign them to variables and then print them back out. You've made the work much simpler. Easy pizzy, right? In python, command-line arguments are handled in the following ways.

  1. argv
  2. argarse
  3. getopt

To kick-off, let's get a start with using argv. For this illustration, we will be writing a simple program that takes a number and returns the square. Now is that not something cool?

We'll start off by writing the program as one that you would normally have:

# square_numbers.py
#program that allows the user to enter a number and print the square.


def main(number):
    """Return the square of the number """
    return number* number

number = int(input("Enter a number to square:"))
print(main(number))

Now to get the result, you would normally have something like this on the command-line

python square_numbers.py

You would then get a prompt:

Enter a number to square:

Where you would be allowed to input a number and get back the square of that number. Compare this with a python program that uses command-line arguments.

# square.py
# get the square of numbers using command_line_arguments

def main(number):
    """Return the square of the number """
    return number* number

#convert the argument to an integer since it is read as a string.
number = int(sys.argv[1])

print(main(number))

To get the result from this:

python square.py 5

Of-course the result would be 25!
Take note, however, on how we get the value 5 from the command-line. In the program, we import sys , a module short form for system-specific parameters and functions. We then use it in conjunction with argv. Why? Simply because as a module, sys has a lot of barrels under its belt. We are simply specifying that from this well of tools you have, we want access to command-line arguments and thus, so named argv, quite literally.

Here's the catch, sys.argv reads items as a list. What does this mean? It means everything after the word python is an item in the 'virtual' list. So our list looks like this:

[ 'square' , '5' ]

That would shed some light into why we have sys.argv[1] to get the second item on the list. Remember, unless you are an R programmer, who gets right on with 1, counting starts from 0 (zero). I know, right? R programmers are so weird. Harrison if you're reading this don't delete my twitter account.

weird.jpg

Photo by Charles on Unsplash (It could be Harrison but no one will ever know)

Anyway, where were we? Oh, right, getting values from the terminal.

So if we were to pass in more values, it would increase the length of our list. Let's take an example, say, a program that takes two numbers and adds them.

# add.py
# get the sum of two numbers
import sys

def main(first_number, second_number):
    """Return the SUM of the number """
    return first_number + second_number


first_number = int(sys.argv[1])
second_number = int(sys.argv[2])

print(main(first_number, second_number))

This time, if we write python add.py 5 6, we would get a result of 11, the first_number being 5 while the second, 6. We did it! The simplest program in the world other than, well, Hello world but with a twist. This brief article should give you a great foundation for building a ton of great programs. Just pass in a variable, absolutely anything; a URL, a path to a file, you name it. Provided your program gets to read and use it.

We'll take a step deeper into the use of command-line arguments in the next article, comparing yet another way of doing it and its differences with what we have just gone through. Need to go through the files? Take a look at the repository from TheGreenCodes.