[issue22500] Argparse always stores True for positional arguments
Changes by Berker Peksag berker.pek...@gmail.com: -- resolution: - not a bug stage: - resolved ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22500 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22500] Argparse always stores True for positional arguments
Changes by paul j3 ajipa...@gmail.com: -- status: open - closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22500 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22500] Argparse always stores True for positional arguments
New submission from Tristan Fisher: It's my understanding that giving the action=store_true to an argument in argparse defaults to False. When using non-double-dashed/positional arguments, the argument resorts to True (even if explicitly marked default=False). I've attached a minimal example, but, for clarity, the relevant line is as such: parser.add_argument(meow, action=store_true, default=False) I realize that this might strike some as an odd usage, and I always have the option of using --meow, but I found it odd that a positional argument is always True, even if not specified in sys.argv. -- components: Library (Lib) files: argparse_always_true.py messages: 227584 nosy: Tristan.Fisher priority: normal severity: normal status: open title: Argparse always stores True for positional arguments type: behavior versions: Python 3.4 Added file: http://bugs.python.org/file36727/argparse_always_true.py ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22500 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22500] Argparse always stores True for positional arguments
paul j3 added the comment: A 'store_true' action takes 0 arguments. In effect `nargs=0`. With an `optional` (flagged) argument, the default `False` is used if the flag is absent, and set to `True` when the flag is encountered (its Action `__call__` function is run). A `positional` is 'encountered' whenever there are enough values to meet its `nargs`. With `nargs=0`, an empty list of strings, i.e. none, is enough. Thus such a `positional` is always found, and its `__call__` is run, setting the value to `True`. As a result, action types like 'store_true', 'store_false', 'store_const' only make sense with `optionals`. And I can't think of simple way of using a 'positional' to set an Namespace 'dest' to boolean values. It could be done with a custom Action, but not with the predefined ones. Or you could translate the values after parsing. -- nosy: +paul.j3 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22500 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com