New submission from Trenton Bricken <brickentren...@gmail.com>:

This is a bug with argparse. 

Say I have:

parser.add_argument('--verbose', type=bool, action='store', nargs='+',
                        default = [False],
                        help='turns on verbosity')

If in the command line I have "--verbose False' the default will then evaluate 
to True and return the value "True" rather than the value of "False" that I 
tried to set it to!

When a developer has lots of arguments to pass in, they may not remember if an 
argument defaults to False or True. Setting the value to False and having it 
then return True is very confusing and should not occur. 

Right now I have a work-around where I have a new type=buildBool where:

def buildBool(arg):
   return bool(arg)

and do:

parser.add_argument('--verbose', type=buildBool, action='store', nargs='+',
                        default = ['False'],
                        help='turns on verbosity')

but this means I have to have this type and have my default value be a string 
which is suboptimal and this bug remains a trap for other developers.

----------
messages: 359045
nosy: Trenton Bricken
priority: normal
severity: normal
status: open
title: argparse boolean type bug
type: behavior
versions: Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue39167>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to