import sys
import getopt
# Beispiel für Kommandozeile: test.py -i inp.py --output out.py -h
n = len(sys.argv)
print("Scriptname:", sys.argv[0])
print("Anz. Parameter:", n)
print("Parameterliste:", str(sys.argv))
for i in range(0, n):
print("Parameter", i, ":", sys.argv[i])
try:
# ersten Parameter (Scriptname) aus der Liste entfernen
pars = sys.argv[1:]
# Options und Argumente aus Parameterliste parsen
opts, args = getopt.getopt(pars, "hi:o:", ["help", "input=", "output="])
# Options durchlaufen
for arg, val in opts:
if arg in ("-i", "--input"):
print("Input:", arg, "Value:", val)
elif arg in ("-o", "--output"):
print("Output:", arg, "Value:", val)
elif arg in ("-h", "--help"):
print("Help:", arg, "Value:", val)
# Parse-Exception abfagen
except getopt.error as err:
print (str(err))
Links