[ python-Feature Requests-1627266 ] optparse "store" action should not gobble up next option
SourceForge.net
noreply at sourceforge.net
Fri Mar 16 07:54:02 CET 2007
Feature Requests item #1627266, was opened at 2007-01-03 10:46
Message generated for change (Comment added) made by nnorwitz
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1627266&group_id=5470
Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: None
>Status: Closed
>Resolution: Invalid
Priority: 5
Private: No
Submitted By: Raghuram Devarakonda (draghuram)
Assigned to: Greg Ward (gward)
Summary: optparse "store" action should not gobble up next option
Initial Comment:
Hi,
Check the following code:
--------------opttest.py----------
from optparse import OptionParser
def process_options():
global options, args, parser
parser = OptionParser()
parser.add_option("--test", action="store_true")
parser.add_option("-m", metavar="COMMENT", dest="comment", default=None)
(options, args) = parser.parse_args()
return
process_options()
print "comment (%r)" % options.comment
---------------------
$ ./opttest.py -m --test
comment ('--test')
I was expecting this to give an error as "--test" is an option. But it looks like even C library's getopt() behaves similarly. It will be nice if optparse can report error in this case.
----------------------------------------------------------------------
>Comment By: Neal Norwitz (nnorwitz)
Date: 2007-03-15 23:54
Message:
Logged In: YES
user_id=33168
Originator: NO
Closing since optik is maintained separately. Please file a feature
request at http://sourceforge.net/projects/optik
----------------------------------------------------------------------
Comment By: Steven Bethard (bediviere)
Date: 2007-01-11 10:19
Message:
Logged In: YES
user_id=945502
Originator: NO
At the moment, you generally can't:
http://argparse.python-hosting.com/ticket/25
though the simple value "-" is valid. I do plan to address this in the
not-so-distant future (though no one yet has complained about it).
For optparse module, I think the OP's problem could likely be fixed by
editing _process_long_opt() and _process_short_opts() to do some checks
around the code:
elif nargs == 1:
value = rargs.pop(0)
else:
value = tuple(rargs[0:nargs])
del rargs[0:nargs]
You could make sure that the option arguments (the "value" objects in the
code above) were not already existing options with a check like:
all(not self._match_long_opt(arg) and
not self._short_opt.get(arg)
for arg in rargs[0:nargs])
----------------------------------------------------------------------
Comment By: Georg Brandl (gbrandl)
Date: 2007-01-11 08:16
Message:
Logged In: YES
user_id=849994
Originator: NO
So how does one give option arguments starting with - to argparse?
----------------------------------------------------------------------
Comment By: Steven Bethard (bediviere)
Date: 2007-01-11 07:41
Message:
Logged In: YES
user_id=945502
Originator: NO
For what it's worth, argparse_ gives an error here:
>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('--test', action='store_true')
>>> parser.add_argument('-m', dest='comment')
>>> parser.parse_args(['-m', '--test'])
usage: PROG [-h] [--test] [-m COMMENT]
PROG: error: argument -m: expected one argument
That's because argparse assumes that anything that looks like "--foo" is
an option (unless it's after the pseudo-argument "--" on the command
line).
.. _argparse: http://argparse.python-hosting.com/
----------------------------------------------------------------------
Comment By: Raghuram Devarakonda (draghuram)
Date: 2007-01-05 09:58
Message:
Logged In: YES
user_id=984087
Originator: YES
It is possible to deduce "--test" as an option because it is in the list
of options given to optparse. But your point about what if the user really
wants "--test" as an option argument is valid. I guess this request can be
closed.
Thanks,
Raghu.
----------------------------------------------------------------------
Comment By: David Goodger (goodger)
Date: 2007-01-05 08:28
Message:
Logged In: YES
user_id=7733
Originator: NO
I think what you're asking for is ambiguous at best. In your example, how
could optparse possibly decide that the "--test" is a second option, not an
option argument? What if you *do* want "--test" as an argument?
Assigning to Greg Ward. Recommend closing as invalid.
----------------------------------------------------------------------
Comment By: Raghuram Devarakonda (draghuram)
Date: 2007-01-05 07:19
Message:
Logged In: YES
user_id=984087
Originator: YES
I am attaching the code fragment as a file since the indentation got all
messed up in the original post.
File Added: opttest.py
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1627266&group_id=5470
More information about the Python-bugs-list
mailing list