Ich lerne derzeit, wie man das Python optparse-Modul verwendet. Ich versuche das folgende Beispielskript, aber die args-Variable ist leer. Ich habe es mit Python 2.5 und 2.6 versucht, aber ohne Erfolg.
import optparse
def main():
p = optparse.OptionParser()
p.add_option('--person', '-p', action='store', dest='person', default='Me')
options, args = p.parse_args()
print '\n[Debug]: Print options:', options
print '\n[Debug]: Print args:', args
print
if len(args) != 1:
p.print_help()
else:
print 'Hallo %s' % options.person
if __name__ == '__main__':
main()
Ausgabe:
>C:\Scripts\example>hello.py -p Kelvin
[Debug]: Print options: {'person': 'Kelvin'}
[Debug]: Print args: []
Verwendung: hello.py [options]
Optionen: -h, --help zeige diese Hilfe-Nachricht und beende -p PERSON, --person=PERSON