Feb-28-2024, 07:15 PM
(This post was last modified: Feb-28-2024, 07:15 PM by deanhystad.)
This line was wrong.
options, args = getopt.getopt(argv, "f:l:", ["first =", "last ="])It should be
options, args = getopt.getopt(argv, "f:l:", ["first=", "last="])
from getopt import getopt def getargs(argv): options, args = getopt(argv, "f:l:", ["first=", "last="]) options = dict(options) return ( options.get('--first', options.get('-f')), options.get('--last', options.get('-l')), *args ) print(getargs("-f Knowledge -l Hut".split())) print(getargs("--first Knowledge --last Hut".split())) print(getargs("-f Knowledge --last Hut".split())) print(getargs("-f Knowledge --last Hut arg1 arg2".split()))
Output:('Knowledge', 'Hut')
('Knowledge', 'Hut')
('Knowledge', 'Hut')
('Knowledge', 'Hut', 'arg1', 'arg2')