New GitHub issue #95100 from CSDUMMI:<br>
<hr>
<pre>
<!--
If you're new to Python and you're not sure whether what you're experiencing is a bug, the CPython issue tracker is not
the right place to seek help. Consider the following options instead:
- reading the Python tutorial: https://docs.python.org/3/tutorial/
- posting in the "Users" category on discuss.python.org: https://discuss.python.org/c/users/7
- emailing the Python-list mailing list: https://mail.python.org/mailman/listinfo/python-list
- searching our issue tracker (https://github.com/python/cpython/issues) to see if
your problem has already been reported
-->
**Bug report**
If you add an optional argument to a parser with `argparse` containing dashes,
those are converted to `_` automatically in the resulting Namespace object.
But if you add a positional argument containing a `-`, this is not converted and the resulting error message suggests the argument name containing the `-` instead of the `_`. Which is of course not possible (without `getattr`), because it's not a valid variable name in Python.
This behaviour is misleading and undocumented and I'd suggest to convert `-` to `_` in positional arguments too.
Reproduction code:
```python
import argparse
parser = argparse.ArgumentParser("example")
parser.add_argument("foo-bar", type=str)
args = parser.parse_args()
print("getattr", getattr(args, "foo-bar"))
print("- replaced by _", args.foo_bar)
```
Results in:
```bash
$ python3 main.py abc
getattr aoe
Traceback (most recent call last):
File "/tmp/main.py", line 11, in <module>
print("- replaced by _", args.foo_bar)
AttributeError: 'Namespace' object has no attribute 'foo_bar'. Did you mean: 'foo-bar'?
```
**Your environment**
<!-- Include as many relevant details as possible about the environment you experienced the bug in -->
- CPython versions tested on: Python 3.10.5
- Operating system and architecture: Linux
<!--
You can freely edit this text. Remove any lines you believe are unnecessary.
-->
</pre>
<hr>
<a href="https://github.com/python/cpython/issues/95100">View on GitHub</a>
<p>Labels: type-bug</p>
<p>Assignee: </p>