getopt, i don't get it

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dominik Kaspar

    getopt, i don't get it

    I tried to use getopt and copied the example from:


    but nothing is working... getopt.GetoptEr ror doesn't seem to exist and
    when i run the program commenting this out, none of "-v", "-h" and
    "-o" wants to be recognized... what's the matter?!

    Dominik


    import getopt, sys

    def usage():
    print "Use it like this: bla bla bla"

    def main():
    try:
    opts, args = getopt.getopt(s ys.argv[1:], "ho:v", ["help",
    "output="])
    except: # getopt.GetoptEr ror:
    # print help information and exit:
    usage()
    sys.exit(2)
    output = None
    verbose = False
    for o, a in opts:
    if o == "-v":
    verbose = True
    if o in ("-h", "--help"):
    usage()
    sys.exit()
    if o in ("-o", "--output"):
    output = a
    # ...

    if __name__ == "__main__":
    main()
  • Peter Hansen

    #2
    Re: getopt, i don't get it

    Dominik Kaspar wrote:[color=blue]
    >
    > I tried to use getopt and copied the example from:
    > http://www.python.org/doc/current/li...le-getopt.html
    >
    > but nothing is working... getopt.GetoptEr ror doesn't seem to exist and
    > when i run the program commenting this out, none of "-v", "-h" and
    > "-o" wants to be recognized... what's the matter?![/color]

    It seems likely you have another file called getopt.py (or perhaps
    a leftover getopt.pyc from a previous time) in your Python path
    (check all directories in sys.path, starting with current directory).

    You can't safely reuse the names of standard library modules most
    of the time...

    -Peter

    Comment

    • Ville Vainio

      #3
      Re: getopt, i don't get it

      dokaspar@studen t.ethz.ch (Dominik Kaspar) writes:
      [color=blue]
      > I tried to use getopt and copied the example from:
      > http://www.python.org/doc/current/li...le-getopt.html[/color]

      If you haven't used getopt before, don't start now. Check out optparse
      instead.

      --
      Ville Vainio http://www.students.tut.fi/~vainio24

      Comment

      • Josef Meile

        #4
        Re: getopt, i don't get it

        > If you haven't used getopt before, don't start now. Check out optparse[color=blue]
        > instead.[/color]
        Thanks for the tip, I have already wrote something with getopt, but I found
        that the "add_option " of the optparse is cleaner and easier to read than the
        list of options that you use with getopt. Other thing I liked is that you
        don't
        have to write a method to print the help, you just pass a descriptive text
        to the parser and it will do the rest.

        Really nice :-). I will try it.

        Regards,
        Josef


        Comment

        Working...