Sapnesh Naik
Senior Full Stack Software Developer
Full stack software developer with 6 years of experience building highly scalable web applications using using backend, frontend, and cloud-based technologies.

sys.argv - Command Line Arguments in Python [Part 1]

September 1, 2018
sys.argv - Command Line Arguments in Python [Part 1]

In the programming world, an expression often comes across that “a problem can be solved in many different ways”. Hence the programmer has to decide which solution is best for him/her. The same holds true if you want to process Command Line Arguments (CLA) in Python. There can be many options to choose from such as sys.argv, getopt or argparse.

There are mainly 3 ways to handle CLA in Python:

Let us look at each option in detail. We will be writing a simple python script to add two numbers and see which of the three is suitable for use in what scenarios.

sys.argv:

  • argv is a variable provided by the sys module which holds a list of all the arguments passed to the command line (including the script name).
  • So even if you don’t pass any arguments to your script. The argv variable always contains at least one element i.e the script name.
  • The arguments in argv are always parsed as string. So be careful if you are expecting your input to be of any other type. You may need to cast or convert the elements according to your requirements.
#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys

# add two integers and return the sum
def add(a, b):
    return a + b


def main(argv):

    # make sure there are at least two arguments
    if len(argv) >= 2:
        # convert arg 0 and 1 to int and pass them to add function
        print ('\n Sum of two numbers is :', add(int(argv[0]), int(argv[1])), '\n')
    else:
        print ("\nUsage: python sys_argv.py <number1> <number2>\n")
        print ("Example: python sys_argv.py 7 3\n")
        sys.exit(2)


if __name__ == '__main__':

    # exclude script name from the argumemts list and pass it to main()
    main(sys.argv[1:])

Output:

Command ine arguments using sys.argv in python

  • sys.argv is of the type <list> so you can access the elements just as you would from any other list. For example, sys.argv[1]
  • As you can see on line 26 I am excluding the script name (arg 0) before passing it to the main() function.

So when should I use or not use sys.argv?

  • As you can see sys.argv is just a list of command line arguments and does not provide any additional features such as the option for switches ( ex: as in ls -l) or specifying the data type of arguments nor the ability to have position independent arguments ( ex: long listing of files works as expected even if you specify the option at last like ls -il).
  • It does not provide any inherent mechanism to make any of the arguments as required or optional and we also cannot limit the number of arguments supplied to our script.
  • However, this can be more than sufficient if your problem definition is simple enough. But if your requirements are a bit more advanced than just adding two numbers, you may need to use getopt or argparse.