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: argparse: Accept option without needing to distinguish "-" from "_" in arg_string
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.11
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: catherinedevlin, eric.smith, paul.j3, rhettinger, wbarnha
Priority: normal Keywords: patch

Created on 2021-05-21 17:51 by wbarnha, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 26295 closed wbarnha, 2021-05-21 17:58
Messages (7)
msg394135 - (view) Author: William Barnhart (wbarnha) * Date: 2021-05-21 17:51
An issue I encountered recently with argparse was when I tried running a script with its argument changed from something like:
```
parser.add_argument('--please_work')
```
and was replaced with:
```
parser.add_argument('--please-work')
```

I have not ever seen anyone add an argument where a function has two arguments existing in a function, such as please_work and please-work. I think this is a pretty safe feature to implement, plus it enforces good practices. So if I wrote something such as:

```
parser = argparse.ArgumentParser(description="check this out")
parser.add_argument('--please-work')
```

Then I could call the program using:
```
python3 test.py --please_work True
```
or:
```
python3 test.py --please-work True
```
msg394148 - (view) Author: Catherine Devlin (catherinedevlin) * Date: 2021-05-21 19:55
Your PR doesn't include any tests... but I like the idea enough to create the tests for you if you prefer.

However, first I'd like to see whether the core devs like the idea.  I could see saying either "yes, why not make things easy" or "no, there should be only one way to do it".
msg394166 - (view) Author: William Barnhart (wbarnha) * Date: 2021-05-21 23:00
I'm glad someone else thinks it's a good idea. If you have some ideas for tests, I'd be happy to help write them in order to spare the inconvenience (unless the tests are that easy to write then by all means please do). 

I can appreciate why the core developers could be polarized. One side could say it's safer to just take better care of our Python scripts and have them in wrapper functions. But people don't always do this.

In favor of the idea: This method can be used to spare developers from agony when a "_" is changed to a "-" when they aren't informed of the change. As a result, this can allow for code to remain usable since most developers are more interested in the letters of arguments and not so much special characters. But then again, a --help option could be all that's needed to fix this issue.

I think a solution that satisfies both parties would be creating some safe guard for adding arguments that are named identically, containing "_" or "-" in the middle, and raising an exception when the regexes of the arguments are conflicting. Such as if I wrote:
```
parser.add_argument('--please_work')
parser.add_argument('--please-work')
```
then an exception should be raised with an error for conflicting argument names.
msg394175 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-05-22 03:47
Ideally, we should keep argparse in line with common practices.  AFAICT, treating '-' and '_' the same isn't a norm.  Click, for example, doesn't do this.
msg394337 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-05-25 12:33
I agree with Raymond: I've never seen this behavior anywhere. In general, I think being looser with allowed inputs is not a good idea.
msg394410 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-05-26 00:26
William, thank you for the suggestion, but we're going to decline for the reasons listed by Eric and me.
msg394411 - (view) Author: Catherine Devlin (catherinedevlin) * Date: 2021-05-26 02:22
All that said, it's easy to imagine a PyPI package that would give this functionality for those who want it.  Maybe even one that could also alter the behavior of Click and Typer.  If you want to go that route, let me know.
History
Date User Action Args
2022-04-11 14:59:46adminsetgithub: 88374
2021-05-26 02:22:44catherinedevlinsetmessages: + msg394411
2021-05-26 00:26:12rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg394410

stage: patch review -> resolved
2021-05-25 12:33:04eric.smithsetnosy: + eric.smith
messages: + msg394337
2021-05-22 03:47:45rhettingersetnosy: + rhettinger

messages: + msg394175
versions: - Python 3.6, Python 3.7, Python 3.8, Python 3.9, Python 3.10
2021-05-22 03:27:12rhettingersetnosy: + paul.j3
2021-05-21 23:00:40wbarnhasetmessages: + msg394166
2021-05-21 19:55:49catherinedevlinsetnosy: + catherinedevlin
messages: + msg394148
2021-05-21 17:58:52wbarnhasetkeywords: + patch
stage: patch review
pull_requests: + pull_request24899
2021-05-21 17:51:20wbarnhacreate