Automatische Hilfe von `optparse`

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
CrackPod
User
Beiträge: 205
Registriert: Freitag 30. Juni 2006, 12:56

Hallo,

im Zuge der Entwicklung meines derzeitigen Projekts hab ich mich mit optparse auseinadergesetzt. Nur bekomme ich bei diesem Code einen Fehler, wenn ich die automatisch generierte Hilfe benutzen möchte

Code: Alles auswählen

def main():
    u = "usage: %prog [options] DIR"
    v = "%prog",__version__
    d = "This programm deletes the *.txt files and renames the *.jpg",
    "file of all Jamendo subdirs in the given directory. It also creates",
    "playlists of the albums and if requestet of all albums from a author.",
    "You have also the possibility to change the directorytree."
    h={"-c":"prevents the renaming of the *.jpg file into cover.jpg.",
    "-t":"prevents the deleting of the *.txt files.",
    "-a":"tells the programm to change only the directoryname not the tree.",
    "-f":"cuts off the Jamendo text after the two hyphens.",
    "-v":"prints exactly what's being done.",
    "-p":"prevents the creating of playlists",
    "-d":"the folder DIR which contains the downloaded albumbs"}

    parser = OptionParser(usage=u, version=v, description=d,
                          add_help_option=True)

    parser.add_option("-f", "--folderrename", action="store_true",
                      dest="folderrename", default=False, help=h["-d"])
    parser.add_option("-v", "--verbose", action="store_true",
                      dest="verbose", default=False, help=h["-v"])
    parser.add_option("-t", "--notxtdelete", action="store_false",
                      dest="txtdelete", default=True, help=h["-t"])
    parser.add_option("-a", "--noartistdirs", action="store_false",
                      dest="artistdir", default=True, help=h["-a"])
    parser.add_option("-c", "--nocorrename", action="store_false",
                      dest="coverrename", default=True, help=h["-c"])
    parser.add_option("-p", "--noplaylists", action="store_false",
                      dest="playlists", default=True, help=h["-p"])
    parser.add_option("-d", "--directory", metavar="DIR",
                      dest="dir", help=h["-d"])

    (options, args) = parser.parse_args()
    print options
    print '-'*20
    print args
    if options.verbose:
        pass
    #jte=JTE(options)

if __name__ == '__main__':
    main()
Fehlerausgabe:

Code: Alles auswählen

tobsl@blechbuechse:~$ python torrentrename.py -h
Traceback (most recent call last):
  File "torrentrename.py", line 102, in ?
    main()
  File "torrentrename.py", line 93, in main
    (options, args) = parser.parse_args()
  File "/usr/lib/python2.4/optparse.py", line 1280, in parse_args
    stop = self._process_args(largs, rargs, values)
  File "/usr/lib/python2.4/optparse.py", line 1324, in _process_args
    self._process_short_opts(rargs, values)
  File "/usr/lib/python2.4/optparse.py", line 1431, in _process_short_opts
    option.process(opt, value, values, self)
  File "/usr/lib/python2.4/optparse.py", line 712, in process
    return self.take_action(
  File "/usr/lib/python2.4/optparse.py", line 733, in take_action
    parser.print_help()
  File "/usr/lib/python2.4/optparse.py", line 1538, in print_help
    file.write(self.format_help())
  File "/usr/lib/python2.4/optparse.py", line 1526, in format_help
    result.append(self.format_description(formatter) + "\n")
  File "/usr/lib/python2.4/optparse.py", line 998, in format_description
    return formatter.format_description(self.get_description())
  File "/usr/lib/python2.4/optparse.py", line 1449, in get_description
    return self.expand_prog_name(self.description)
  File "/usr/lib/python2.4/optparse.py", line 1446, in expand_prog_name
    return s.replace("%prog", self.get_prog_name())
AttributeError: 'tuple' object has no attribute 'replace'
Nur hab ich selber kein Tupel eingebaut - oder ich finde es nicht. Ist das ein Bug von optparse? Was hab ich falsch gemacht? :wink:

Bitte klärt mich mal auf :)
LG Tobi
Benutzeravatar
Rebecca
User
Beiträge: 1662
Registriert: Freitag 3. Februar 2006, 12:28
Wohnort: DN, Heimat: HB
Kontaktdaten:

Code: Alles auswählen

>>> __version__ = "1.1.1"
>>> v = "%prog", __version__
>>> type(v)
<type 'tuple'>
CrackPod
User
Beiträge: 205
Registriert: Freitag 30. Juni 2006, 12:56

Hallo,

danke - das war das einzige, was für mich in Frage kam, was ich aber nich geändert hab und getestet hab, weil man doch bei prints auch Kommata benutzen kann, um Zeichenketten zu verketten. Wieso geht das bei Variablen zuweisungen nicht? Da fehlen doch noch die Klammern, die man für Tupelzuweisungen benutzt odeR?
Versteh ich grad nich..

LG Tobi

--------------- Edit: -----------------
Hab den Code geändert

Code: Alles auswählen

def main():
    __version__ = '0.0.1a'
    u = "usage: %prog [options] DIR"
    d = "This programm deletes the *.txt files and renames the *.jpg",
    "file of all Jamendo subdirs in the given directory. It also creates",
    "playlists of the albums and if requestet of all albums from a author.",
    "You have also the possibility to change the directorytree."
    h={"-c":"prevents the renaming of the *.jpg file into cover.jpg.",
    "-t":"prevents the deleting of the *.txt files.",
    "-a":"tells the programm to change only the directoryname not the tree.",
    "-f":"cuts off the Jamendo text after the two hyphens.",
    "-v":"prints exactly what's being done.",
    "-p":"prevents the creating of playlists",
    "-d":"the folder DIR which contains the downloaded albumbs"}

    parser = OptionParser(usage=u, version=__version__, description=d,
                          add_help_option=True)
Bekomme aber immernoch diesselbe Fehlermeldung.
BlackJack

CrackPod hat geschrieben:danke - das war das einzige, was für mich in Frage kam, was ich aber nich geändert hab und getestet hab, weil man doch bei prints auch Kommata benutzen kann, um Zeichenketten zu verketten. Wieso geht das bei Variablen zuweisungen nicht?
Bei ``print`` trennt das Komma die einzelnen Argumente und verkettet keine Zeichenkette(n).
Da fehlen doch noch die Klammern, die man für Tupelzuweisungen benutzt odeR?
Klammern erzeugen keine Tupel, sondern die Kommata tun das. Einzige Ausnahme ist das leere Tupel, dass man mit ``()`` bekommt. Schon bei einem einelementigen Tupel braucht man zwingend das Komma: ``(1,)``; anderfalls wäre das einfach eine ``1`` in Klammern.

Ferner braucht man Klammern, wenn das Konstrukt sonst nicht eindeutig wäre. Zum Beispiel: ``[a, b for b in c]``. Ist das eine Liste von Tupeln mit festem `a` und jeweils einem `b` aus `c` oder eine Liste mit zwei Elementen ─ ein `a` und ein Generator-Objekt.

Code: Alles auswählen

In [32]: [a, b for b in c]
------------------------------------------------------------
   File "<ipython console>", line 1
     [a, b for b in c]
             ^
SyntaxError: invalid syntax


In [33]: [(a, b) for b in c]
Out[33]: [(42, 0), (42, 1), (42, 2)]

In [34]: [a, (b for b in c)]
Out[34]: [42, <generator object at 0xb78e7c4c>]
CrackPod
User
Beiträge: 205
Registriert: Freitag 30. Juni 2006, 12:56

ok. Dankeschön für die Erklärung. Das einzige Problem, was ich noch habe, dass ich trotz des geänderten Codes noch immer Probleme mit dem Tupel habe.

Code: Alles auswählen

def main():
    u = "usage: %prog [options] DIR"
    d = "This programm deletes the *.txt files and renames the *.jpg",
    "file of all Jamendo subdirs in the given directory. It also creates",
    "playlists of the albums and if requestet of all albums from a author.",
    "You have also the possibility to change the directorytree."
    h={"-c":"prevents the renaming of the *.jpg file into cover.jpg.",
    "-t":"prevents the deleting of the *.txt files.",
    "-a":"tells the programm to change only the directoryname not the tree.",
    "-f":"cuts off the Jamendo text after the two hyphens.",
    "-v":"prints exactly what's being done.",
    "-p":"prevents the creating of playlists",
    "-d":"the folder DIR which contains the downloaded albumbs"}

    parser = OptionParser(usage=u, version=__version__, description=d,
                          add_help_option=True)

    parser.add_option("-f", "--folderrename", action="store_true",
                      dest="folderrename", default=False, help=h["-d"])
    parser.add_option("-v", "--verbose", action="store_true",
                      dest="verbose", default=False, help=h["-v"])
    parser.add_option("-t", "--notxtdelete", action="store_false",
                      dest="txtdelete", default=True, help=h["-t"])
    parser.add_option("-a", "--noartistdirs", action="store_false",
                      dest="artistdir", default=True, help=h["-a"])
    parser.add_option("-c", "--nocorrename", action="store_false",
                      dest="coverrename", default=True, help=h["-c"])
    parser.add_option("-p", "--noplaylists", action="store_false",
                      dest="playlists", default=True, help=h["-p"])
    parser.add_option("-d", "--directory", metavar="DIR",
                      dest="dir", help=h["-d"])

    (options, args) = parser.parse_args()
LG Tobi
Benutzeravatar
Rebecca
User
Beiträge: 1662
Registriert: Freitag 3. Februar 2006, 12:28
Wohnort: DN, Heimat: HB
Kontaktdaten:

Du machst in der Variable "d" genau den gleichen Fehler.
Benutzeravatar
birkenfeld
Python-Forum Veteran
Beiträge: 1603
Registriert: Montag 20. März 2006, 15:29
Wohnort: Die aufstrebende Universitätsstadt bei München

Und zwar nicht nur einen:

"d" ist hier ein 1-Tupel, und die folgenden Zeilen sind jeweils Expression Statements (1-Tupel) ohne Auswirkung.
Dann lieber noch Vim 7 als Windows 7.

http://pythonic.pocoo.org/
CrackPod
User
Beiträge: 205
Registriert: Freitag 30. Juni 2006, 12:56

Rebecca hat geschrieben:Du machst in der Variable "d" genau den gleichen Fehler.
Ok, jetz wirds richtig peinlich...
Danke LG
Antworten