Title: Argparse `add_argument` treats `-` in flags differently to `-` in positional arguments. · Issue #95100 · python/cpython · GitHub
Open Graph Title: Argparse `add_argument` treats `-` in flags differently to `-` in positional arguments. · Issue #95100 · python/cpython
X Title: Argparse `add_argument` treats `-` in flags differently to `-` in positional arguments. · Issue #95100 · python/cpython
Description: 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 ...
Open Graph Description: 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 argumen...
X Description: 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 argumen...
Opengraph URL: https://github.com/python/cpython/issues/95100
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Argparse `add_argument` treats `-` in flags differently to `-` in positional arguments.","articleBody":"\u003c!--\r\n 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\r\n the right place to seek help. Consider the following options instead:\r\n\r\n - reading the Python tutorial: https://docs.python.org/3/tutorial/\r\n - posting in the \"Users\" category on discuss.python.org: https://discuss.python.org/c/users/7\r\n - emailing the Python-list mailing list: https://mail.python.org/mailman/listinfo/python-list\r\n - searching our issue tracker (https://github.com/python/cpython/issues) to see if\r\n your problem has already been reported\r\n--\u003e\r\n\r\n**Bug report**\r\n\r\nIf you add an optional argument to a parser with `argparse` containing dashes,\r\nthose are converted to `_` automatically in the resulting Namespace object.\r\n\r\nBut 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.\r\n\r\nThis behaviour is misleading and undocumented and I'd suggest to convert `-` to `_` in positional arguments too.\r\n\r\nReproduction code:\r\n```python\r\nimport argparse\r\n\r\nparser = argparse.ArgumentParser(\"example\")\r\n\r\nparser.add_argument(\"foo-bar\", type=str)\r\n\r\nargs = parser.parse_args()\r\n\r\nprint(\"getattr\", getattr(args, \"foo-bar\"))\r\n\r\nprint(\"- replaced by _\", args.foo_bar)\r\n\r\n```\r\n\r\nResults in:\r\n```bash\r\n$ python3 main.py abc\r\ngetattr aoe\r\nTraceback (most recent call last):\r\n File \"/tmp/main.py\", line 11, in \u003cmodule\u003e\r\n print(\"- replaced by _\", args.foo_bar)\r\nAttributeError: 'Namespace' object has no attribute 'foo_bar'. Did you mean: 'foo-bar'?\r\n\r\n```\r\n\r\nCompounding this issue is the fact, that you are prevented from using the `dest` option on `add_argument` to overwrite the name in the Namespace.\r\n\r\n**Your environment**\r\n\r\n\u003c!-- Include as many relevant details as possible about the environment you experienced the bug in --\u003e\r\n\r\n- CPython versions tested on: Python 3.10.5\r\n- Operating system and architecture: Linux\r\n\r\n\u003c!--\r\nYou can freely edit this text. Remove any lines you believe are unnecessary.\r\n--\u003e\r\n","author":{"url":"https://github.com/CSDUMMI","@type":"Person","name":"CSDUMMI"},"datePublished":"2022-07-21T18:02:13.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":24},"url":"https://github.com/95100/cpython/issues/95100"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d5e6b7b6-245c-ce8f-b277-999420140026 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C1F6:DC558:16BF41D:1E6F00E:6969BA69 |
| html-safe-nonce | e5186b95d6c1b56bdafba65658260254f831271c66ebe3fb300c896e95d11994 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMUY2OkRDNTU4OjE2QkY0MUQ6MUU2RjAwRTo2OTY5QkE2OSIsInZpc2l0b3JfaWQiOiI1MjQ3NTQ5NDE3MTk4MzAzODQ5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 9f57baae80ef73f594f20ab01c0a7cee797fec9e1459068547707bde5e686c76 |
| hovercard-subject-tag | issue:1313615402 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/95100/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fb2947dc11bda6bdb225f33276e300e02b62ce06f75c9d93b69cc7b3d4c64127/python/cpython/issues/95100 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fb2947dc11bda6bdb225f33276e300e02b62ce06f75c9d93b69cc7b3d4c64127/python/cpython/issues/95100 |
| og:image:alt | 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 argumen... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | CSDUMMI |
| hostname | github.com |
| expected-hostname | github.com |
| None | acedec8b5f975d9e3d494ddd8f949b0b8a0de59d393901e26f73df9dcba80056 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 83c08c21cdda978090dc44364b71aa5bc6dcea79 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width