This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: OptionParser : Weird comportement in args processing
Type: behavior Stage:
Components: Extension Modules Versions: Python 2.5.3, Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, ohervieu
Priority: normal Keywords:

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

Messages (2)
msg77028 - (view) Author: Olivier Hervieu (ohervieu) Date: 2008-12-05 15:19
Hi guys.. i found something strange on the behavior of OptionParser

If I have this sample code : 

import sys
from optparse import OptionParser

if __name__ == '__main__':
    parser = OptionParser()
    parser.add_option("-p", "--p", help="The P of python", default=None)
    parser.add_option("-y", "--y", help="The Y of python", default=None)
        
    (options,args) = parser.parse_args(sys.argv)
    print options

and i execute :

myFile.py -p -y

the options.p will be -y and options.y will be None

worst.. if my user say :

myFile.py -p -y 'thon'

options.p will be -y and options.y will be None...

In all case I think that, if i do 

myFile.py -p -y 

options -p and options -y must be None

and if i want to put -y in options.p i say something like 

myFile.py -p "-y" so i can do after myFile.py -p "-y" -y "thon".
msg77033 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-12-05 15:37
This is not a bug.  By default, options in optparse take an argument --
therefore, the -y is taken as the argument to the -p option.

Use e.g. add_option(..., action='store_true') to specify an option that
doesn't take an argument.
History
Date User Action Args
2022-04-11 14:56:42adminsetgithub: 48798
2008-12-05 15:37:52georg.brandlsetstatus: open -> closed
resolution: not a bug
messages: + msg77033
nosy: + georg.brandl
2008-12-05 15:19:32ohervieucreate