How to use getopt/OPTARG in Python? How to shift arguments if too many arguments (9) are given?

Mandar Pande picture Mandar Pande · Jun 17, 2011 · Viewed 60.9k times · Source

How to use getopt/optarg in Python?

Answer

Makis picture Makis · Jun 17, 2011

This is an example of how I do it, I usually use the same basic template:

import sys
import getopt

try:
    opts, args = getopt.getopt(sys.argv[1:], 'm:p:h', ['miner=', 'params=', 'help'])
except getopt.GetoptError:
    usage()
    sys.exit(2)

for opt, arg in opts:
    if opt in ('-h', '--help'):
        usage()
        sys.exit(2)
    elif opt in ('-m', '--miner'):
        miner_name = arg
    elif opt in ('-p', '--params'):
        params = arg
    else:
        usage()
        sys.exit(2)

I don't think there is any 9 parameter limit.