Skip to content

Instantly share code, notes, and snippets.

@andersonbosa
Forked from dideler/pyargs.md
Created December 4, 2021 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersonbosa/51536c0eb9bf2d6c1c01aeb94f80a0a4 to your computer and use it in GitHub Desktop.
Save andersonbosa/51536c0eb9bf2d6c1c01aeb94f80a0a4 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.
        print(myargs['-i'])
    print(myargs)

Running this script:

$ python main.py -i input.txt -o output.txt
input.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.

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