Issue 4640: optparse doesn’t disallow adding one-dash long options (“-option”) (original) (raw)

Created on 2008-12-12 14:14 by kszawala, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test_optparse.py.diff hac.man,2012-07-10 02:21 tests covering the described behavior review
Messages (18)
msg77668 - (view) Author: Krzysztof Szawala (kszawala) Date: 2008-12-12 14:14
I am using optparse for command-line parameters parsing. To follow common naming convention I defined -d (minus followed by a single character option) and --debug (double minus followed by a word). It looks like optparse doesn't complain when -debug (single minus) is specified and morover it doesn't recognize it as --debug. I am using Python 2.5.2. Problem was observed on Linux but also occurs on Windows.
msg77683 - (view) Author: David W. Lambert (LambertDW) Date: 2008-12-12 18:32
When I use it on python 3 optparse reports no such option -e, which is correct since the form -debug permits a run of single character options.
msg77856 - (view) Author: Krzysztof Szawala (kszawala) Date: 2008-12-15 09:29
As I mentionetd, the problem occurs with Python 2.5 (I won't be switching to Python 3). The only perspective for me to upgrade is Python 2.6, but as I can see the problem applies to 2.6 as well.
msg77893 - (view) Author: Gabriel Genellina (ggenellina) Date: 2008-12-16 00:20
could you provide a test case / code fragment showing the bug?
msg78320 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2008-12-26 23:53
-debug may be equivalent to -d -e -b -u -g (if none of these options have an argument), but no to --debug. If you like to use -d as an alias to --debug, just use: add_option("-d", "--debug", ...).
msg108637 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-06-25 22:07
Victor, I am interpreting your comment as saying that this is not a bug. If I am wrong, someone can re-open. In any case, the bug would have to be in 2.6 or more realistically, 2.7, since 2.6.final will be out soon.
msg108638 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-06-25 22:11
optparse specifically supports only GNU-style two-dashes long argument. Victor is right, optparse will always understand -thing as -t -h -i -n -g.
msg108656 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-06-26 00:02
I thought the OP might be saying that -debug didn't throw an error in 2.6. test_optparse doesn't seem to have a test for this, so I wrote one. But as far as I can see the correct error (invalid option -e, given that 'd' is defined) is thrown. In 2.5, as well. Rereading the initial post I understand the problem: his 'd' option takes an argument. So 'ebug' becomes the option value, and thus no error is thrown. Which is working as designed (that's how single letter options that take arguments usually work in Unix) So, it is correct that this is not a bug.
msg108699 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2010-06-26 10:00
> Victor, I am interpreting your comment as saying > that this is not a bug. No, I tried to explain that it is a bug :-) add_option("-debug", "--debug", ...) and add_option("-debug", ...) should raise an error.
msg108701 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2010-06-26 10:05
Reopen: add_option("-debug", "--debug", ...) and add_option("-debug", ...) should raise an error and have a test.
msg108702 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-06-26 10:09
Oh, we all focused on “-debug is not recognized as --debug” and forgot that the bug report also said “optparse does not complain when -debug is specified” (not sure if that meant “added” or “given on the command line”). We have to check if the bug still applies on 2.6, 2.7, 3.1 and 3.2.
msg109213 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-07-04 10:36
David committed a unit test in r82233. Who wants to propose a patch now?
msg109217 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-07-04 14:07
I didn't commit a unit test for the revised subject, my unit test was for the previous misunderstood diagnosis. So a unit test for disallowing single dash long options in add_option is still needed. Note that this appears (at least on a quick test) to apply to argparse as well. I've left 2.7 and 3.1 in versions, but I'm not 100% sure this should be backported since it is a behavior change. But it's hard to imagine working code using this (does it even do anything, or is the single dash option just silently ignored?)
msg111294 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2010-07-23 12:25
In argparse, "-debug" is a perfectly valid flag (you're not required to have "--debug", and you can even have "+debug" if you want and you specify it correctly), so unless I misunderstand what the bug is, this doesn't apply to argparse.
msg111302 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-07-23 12:41
In that case you are correct, it does not apply to argparse.
msg165143 - (view) Author: Aaron (hac.man) Date: 2012-07-10 02:21
I came across this bug report and was unable to reproduce the described behavior. I wrote a few test cases demonstrating that the behavior is indeed correct. It passes both against 2.5.2 (the version described in the report) and the lastest 2.7. The relevant code is line 602 of optparse.py in the function Option._set_opt_strings(). I believe this bug can be closed.
msg165557 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-07-16 00:12
New changeset 844bb753570f by R David Murray in branch 'default': #4640: Add optparse tests for '-xxx' invalid when defining options. http://hg.python.org/cpython/rev/844bb753570f
msg165558 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-07-16 00:15
Re-reading the issue I think the OP was not trying to define '-debug', but was indeed reporting the behavior when -debug was passed to the parser. I've committed your tests (thanks). So we now have tests for both cases, and this issue can be closed as invalid.
History
Date User Action Args
2022-04-11 14:56:42 admin set github: 48890
2012-07-16 00:15:41 r.david.murray set status: open -> closedresolution: accepted -> not a bugmessages: + stage: needs patch -> resolved
2012-07-16 00:12:53 python-dev set nosy: + python-devmessages: +
2012-07-10 02:21:33 hac.man set files: + test_optparse.py.diffnosy: + hac.manmessages: + keywords: + patch
2010-11-01 15:28:02 bethard set nosy: - bethard
2010-07-23 17:02:05 terry.reedy set nosy: - terry.reedy
2010-07-23 12:41:44 r.david.murray set messages: +
2010-07-23 12:25:36 bethard set messages: +
2010-07-04 14:07:39 r.david.murray set nosy: + bethardmessages: + versions: + Python 3.1, Python 3.2, - Python 2.6
2010-07-04 10:36:33 eric.araujo set resolution: acceptedmessages: + stage: test needed -> needs patch
2010-06-26 10:10:44 eric.araujo set title: optparse doesn't distinguish between '--option' and '-option' -> optparse doesn’t disallow adding one-dash long options (“-option”)
2010-06-26 10:09:53 eric.araujo set title: optparse - dosn't distinguish between '--option' and '-option' -> optparse doesn't distinguish between '--option' and '-option'messages: + stage: resolved -> test needed
2010-06-26 10:05:55 vstinner set status: closed -> openresolution: not a bug -> (no value)messages: +
2010-06-26 10:00:41 vstinner set messages: +
2010-06-26 00:02:55 r.david.murray set nosy: + r.david.murraymessages: +
2010-06-25 22:11:11 eric.araujo set nosy: + eric.araujomessages: + resolution: works for me -> not a bugstage: resolved
2010-06-25 22:07:56 terry.reedy set status: open -> closedversions: + Python 2.6, Python 2.7, - Python 2.5nosy: + terry.reedymessages: + resolution: works for me
2008-12-26 23:53:51 vstinner set nosy: + vstinnermessages: +
2008-12-16 00:20:35 ggenellina set nosy: + ggenellinamessages: + components: + Library (Lib), - Extension Modules
2008-12-15 09:29:56 kszawala set messages: +
2008-12-12 18:32:59 LambertDW set nosy: + LambertDWmessages: +
2008-12-12 14:14:42 kszawala create