Adding Command Line Arguments to Your Code

Adding Command Line Arguments to Your Code

When writing code, people often tend to hard code values into their programs. While this approach works, if they later want their program to exhibit a different behavior, they would likely have to manually edit a particular line of code. An alternative method is to include a bit of extra code in your programs to accept command line arguments. Doing so creates a more robust program and prevents the need to constantly go back and make edits. In this post, we will show you the difference between these two methods and highlight the benefits of using command line arguments.

Our example program is cake.py. Consider the following code:

extra_cake=False

response="You have recieved"

if extra_cake:
    response=response + " extra"

response=response +" cake."
print(response)

When executed, it returns the message:

You have recieved cake.

To get extra cake, we can manually change line 1 to extra_cake=True and our program will return:

You have received extra cake.

If we know that we only want extra cake sometimes, we could manually edit the value of extra_cake as needed. However, this method is inconvenient and editing code carries a risk of introducing an error of some kind.

By adding command line arguments, we can create dynamic behavior with static code. There are many different tools available but for this example, we will use argparse, a popular Python library for setting command line arguments. Let’s look at a different version of our program, cake_args.py:

import argparse

parser = argparse.ArgumentParser(description='Gives cake to the user.')

parser.add_argument('--extra','-x',
    action='store_true',
    help='If enabled, gives extra cake')

args=parser.parse_args()

response="You have recieved"

if args.extra:
    response=response + " extra"

response=response +" cake."
print(response)

At first glance, our code looks a lot more complicated but the most important change is here:

parser.add_argument('--extra','-x',
    action='store_true',
    help='If enabled, gives extra cake')

This creates a variable, extra, and sets it to True if the program is run with either the --extra or -x flag. It also sets a help message, which we will take a closer look at later. The rest is essentially book keeping for the argument parsing processes. We can now run our program like so:

$ python3 cake_args.py --extra
You have recieved extra cake.

If the program is run without any arguments, extra takes on the default value False and the word “extra” will not be printed.

An additional benefit to using the argparse python module is that it will auto-generate a help screen, which makes documentation for your program easier.

$ python3 cake_arg.py --help
usage: cake_arg.py [-h] [--extra]

Gives cake to the user.

options:
  -h, --help   show this help message and exit
  --extra, -x  If enabled, gives extra cake

We have only scratched the surface of what is possible with command line arguments, but hopefully you now have an idea of how useful they can be. While we only used Python and the argparse library here, the concepts discused work with other libraries—such as getopt—and other programming languages like R, bash, C, etc. We hope that you will consider adding command line arguments to your programs, too!