Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #111041

Re: Spot the bug: getoptquestion.py

From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Spot the bug: getoptquestion.py
Date 2016-07-04 21:36 +1000
Message-ID <mailman.66.1467632176.2295.python-list@python.org> (permalink)
References <577a4766$0$5839$e4fe514c@news.xs4all.nl> <CAPTjJmq9SFqLFwr9U=qH2wbJMkicuYTtaXXYNDbRn79rH4j-5g@mail.gmail.com>

Show all headers | View raw


On Mon, Jul 4, 2016 at 9:24 PM, Oscar <jornws0718@xs4all.nl> wrote:
> Is this:
>
> a) a bug in getopt.getopt
> b) a bug in my code
> c) a great way to keep me busy for a while
> d) all of the above?
>
>
> #!/usr/bin/python
>
> from __future__ import print_function
> from getopt import getopt, GetoptError
> import sys
>
> try:
>     opts, args = getopt(sys.argv[1:], 'b', ['bug '])
>
> except GetoptError as err:
>     print('Caught:', repr(err))
>
> else:
>     if opts:
>         for opt, arg in opts:
>
>             if opt in ('-b', '--bug'):
>                 print ("Ceci n'est pas un bug!")
>             else:
>                 #print ('Missed option: "{0}"'.format(opt))
>                 print ('Missed option:', opt)
>     else:
>         print('Usage:', sys.argv[0],'-b|--bug')

Well, first thing I'd do is wipe out your try/except. It's not really
achieving much (you effectively catch an exception to print it to the
console and terminate).

But then what I'm seeing is that you have 'bug ' (with a trailing
space) in your list of long options, so getopt returns '--bug ' as a
long option. Is that a bug? I dunno. Why do you have the trailing
space? In any case, it's pretty easy to see if you change one line of
your code to:

print('Missed option: %r' % opt)

ChrisA

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: Spot the bug: getoptquestion.py Chris Angelico <rosuav@gmail.com> - 2016-07-04 21:36 +1000
  Re: Spot the bug: getoptquestion.py jornws0718@xs4all.nl (Oscar) - 2016-07-04 12:38 +0000
    Re: Spot the bug: getoptquestion.py Chris Angelico <rosuav@gmail.com> - 2016-07-04 23:42 +1000
      Re: Spot the bug: getoptquestion.py Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-05 21:04 -0700
        Re: Spot the bug: getoptquestion.py Chris Angelico <rosuav@gmail.com> - 2016-07-06 14:14 +1000
          Re: Spot the bug: getoptquestion.py jornws0718@xs4all.nl (Oscar) - 2016-07-07 11:30 +0000
            Re: Spot the bug: getoptquestion.py Chris Angelico <rosuav@gmail.com> - 2016-07-07 22:12 +1000
              Re: Spot the bug: getoptquestion.py jornws0718@xs4all.nl (Oscar) - 2016-07-07 12:29 +0000
            Re: Spot the bug: getoptquestion.py Steven D'Aprano <steve@pearwood.info> - 2016-07-26 11:12 +1000

csiph-web