Skip to content

Instantly share code, notes, and snippets.

@longears
Created January 24, 2012 01:07
Show Gist options
  • Select an option

  • Save longears/1667108 to your computer and use it in GitHub Desktop.

Select an option

Save longears/1667108 to your computer and use it in GitHub Desktop.
using command line arguments in python
# example of how to use command-line arguments
import sys
#===================================================
# COMMAND LINE HANDLING
# this is the message the user will get if they don't supply the right number
# of args at the command line
help = """
usage: hailstone.py num
where "num" is an integer and is used as the starting number
"""
# get command line args.
# we have to remove the first element of this list,
# which is the path of the program itself
args = sys.argv[1:]
# print the help and quit in certain situations:
# * wrong number of args
# * user asked for help
if len(args) != 1 or '-h' in args or '--help' in args:
print help
sys.exit(0)
# assign args to variables so we can use them later in the program
n = args[0]
# ...
# (the rest of your program goes here)
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment