Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!news.unit0.net!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Spot the bug: getoptquestion.py Date: Mon, 4 Jul 2016 21:36:13 +1000 Lines: 46 Message-ID: References: <577a4766$0$5839$e4fe514c@news.xs4all.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de FovX1vUaKzgZOqvKVCewxA1ozqPhOaXxuoybpUGl9SDQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'args': 0.04; 'subject:bug': 0.05; 'sys': 0.05; 'trailing': 0.07; 'cc:addr :python-list': 0.09; '#print': 0.09; '__future__': 0.09; 'arg': 0.09; 'err:': 0.09; 'option:': 0.09; 'bug': 0.10; 'exception': 0.13; "%r'": 0.16; "'b',": 0.16; '2016': 0.16; 'above?': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'opts:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'space)': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'bug?': 0.22; '(you': 0.23; 'this:': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'skip:# 10': 0.27; 'message-id:@mail.gmail.com': 0.27; "i'm": 0.30; 'print': 0.30; 'code': 0.30; "i'd": 0.31; 'achieving': 0.33; 'option.': 0.33; 'case,': 0.34; 'except': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'but': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; '(with': 0.38; 'busy': 0.38; 'why': 0.39; 'subject:the': 0.39; 'easy': 0.60; 'your': 0.60; 'great': 0.63; 'jul': 0.72; 'opt': 0.79; 'chrisa': 0.84; 'oscar': 0.84; 'wipe': 0.84; 'to:none': 0.91; 'subject:skip:g 10': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:cc; bh=tegbPhzs696Hpkh54qenKb5BM2CDKrXYpg6wU4VjnOg=; b=kVbN5XCVQUl26gao7rScyGQbjKnO5Gi2VERBlfkI5nj2lbNGKkkb5FIhJ6b6ewND/A SI+Z+yHOz9Vjp9zR1QGqcHiLOqGGkXt0Cc9ueckB7jvIoSInWzRWqJWUKlSu2POJ/hPo rSQeY8wS7baq8kl8F2gMjDYIFAkvOwn45QoHcEd6x7xn8pWICZRlIOBGtlXm7uT21HDz JJo1h+gfKawjLU0KbIW5CUVBh7SfOMbaFxnmh9g/4+ICNk2bpOvQe5IJE7nBCi3H1Vlh 94+Bf5HgIWop3Axc8WUYrDnL8Pfo9qKSTekeF9g0ynuxVtaquB1NBi1xfhvpgMX0RuZd a6cQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:cc; bh=tegbPhzs696Hpkh54qenKb5BM2CDKrXYpg6wU4VjnOg=; b=PfpkKMWeqQg2WNlccPz65twiIG2laU5BHrNkLZYLsHuizQu+cE/7VffYpGROeOTHAB g+YLJL32Bl8wUN5LmZ8fzIzTb1ApswGkK1wqgmexqtrO6LemP69UgAK+0a+9spnjbCwO oj0Fnjc/KNlNuWwS2i2lljnKtYMoMcZZTDyu6as9I1YzLNupB1yoUsL6i1TxwnGD4pCh ZdIC+++fX6JFdgtrlqgeWW3Bu27iM4uNXNePAafbXwELcGTqMEPBPrIUQ8CiumHio1YA AgZXWCa5slRY36654elgszmYIC1v9nv2XMo8ziqJPaXrYXvydJQqo1E82t8xfaGIE75X dUCw== X-Gm-Message-State: ALyK8tJy0BBOV4Epxqx26kubeKGfNSJinBr4GAGjKcZkm9//Tz0a+t4ylqk6oniTOvuHfcRqzKuvh/5pgQyZ/Q== X-Received: by 10.194.97.201 with SMTP id ec9mr9901530wjb.117.1467632174163; Mon, 04 Jul 2016 04:36:14 -0700 (PDT) In-Reply-To: <577a4766$0$5839$e4fe514c@news.xs4all.nl> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <577a4766$0$5839$e4fe514c@news.xs4all.nl> Xref: csiph.com comp.lang.python:111041 On Mon, Jul 4, 2016 at 9:24 PM, Oscar 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