Skip to content

Instantly share code, notes, and snippets.

Last active January 23, 2023 16:39
Show Gist options
  • Save dideler/2395703 to your computer and use it in GitHub Desktop.
Save dideler/2395703 to your computer and use it in GitHub Desktop.
Parsing Command-Line Argument in Python

Command-line arguments in Python show up in sys.argv as a list of strings (so you'll need to import the sys module).

For example, if you want to print all passed command-line arguments:

import sys
print(sys.argv)  # Note the first argument is always the script filename.

Command-line options are sometimes passed by position (e.g. myprogram foo bar) and sometimes by using a "-name value" pair (e.g. myprogram -a foo -b bar).

Here's a simple way to parse command-line pair arguments. It scans the argv list looking for -optionname optionvalue word pairs and places them in a dictionary for easy retrieval. The code is heavily commented to help Python newcomers.

"""Collect command-line options in a dictionary"""

def getopts(argv):
    opts = {}  # Empty dictionary to store key-value pairs.
    while argv:  # While there are arguments left to parse...
        if argv[0][0] == '-':  # Found a "-name value" pair.
            opts[argv[0]] = argv[1]  # Add key and value to the dictionary.
        argv = argv[1:]  # Reduce the argument list by copying it starting from index 1.
    return opts

if __name__ == '__main__':
    from sys import argv
    myargs = getopts(argv)
    if '-i' in myargs:  # Example usage.

Running this script:

$ python -i input.txt -o output.txt
{'-o': 'output.txt', '-i': 'input.txt'}

Simple solution, but not very robust; it doesn't handle error checking and the like. So don't use this in production code! There are more complex alternatives available. Some modules to consider are:

  • getopt
  • optparse (deprecated since Python 2.7)
  • argparse (recommended if you want something in the standard library)
  • docopt (recommended if you're willing to use something not in the standard library)

This post was inspired by the book "Programming Python" by Mark Lutz.

Copy link

In the case where one option-name is specified multiple times with different values like:

$ python -i albania -i australia

The getopts function can be changed to:

    def getopts(argv):
        opts = {}
        while argv:
            if argv[0][0] == '-':
                if argv[0] in opts:
                    opts[argv[0]] = [argv[1]]
            argv = argv[1:] 
        return opts


{'-i': ['albania', 'australia']}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment