[ python-Feature Requests-1627266 ] optparse "store" action should not gobble up next option
SourceForge.net
noreply at sourceforge.net
Thu Jan 11 17:16:54 CET 2007
Feature Requests item #1627266, was opened at 2007-01-03 18:46
Message generated for change (Comment added) made by gbrandl
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: Open
Resolution: None
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: Georg Brandl (gbrandl)
Date: 2007-01-11 16: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 15: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 17: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 16: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 15: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