Title: argparse nargs=-1 fails on python 3.12.7 · Issue #126708 · python/cpython · GitHub
Open Graph Title: argparse nargs=-1 fails on python 3.12.7 · Issue #126708 · python/cpython
X Title: argparse nargs=-1 fails on python 3.12.7 · Issue #126708 · python/cpython
Description: Bug report Bug description: It seems there's a pattern in the wild with argparse where nargs=-1 is used to capture the remaining command line arguments. However, I couldn’t find anything in the argparse nargs documentation to indicate th...
Open Graph Description: Bug report Bug description: It seems there's a pattern in the wild with argparse where nargs=-1 is used to capture the remaining command line arguments. However, I couldn’t find anything in the arg...
X Description: Bug report Bug description: It seems there's a pattern in the wild with argparse where nargs=-1 is used to capture the remaining command line arguments. However, I couldn’t find anything in the...
Opengraph URL: https://github.com/python/cpython/issues/126708
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"argparse nargs=-1 fails on python 3.12.7","articleBody":"# Bug report\r\n\r\n### Bug description:\r\n\r\nIt seems there's a pattern in the wild with `argparse` where `nargs=-1` is used to capture the remaining command line arguments. However, I couldn’t find anything in the [argparse nargs documentation](https://docs.python.org/3/library/argparse.html#nargs) to indicate that this is a supported feature, though it appears to be widely used: https://grep.app/search?current=4&q=nargs%3D-1&filter[lang][0]=Python\r\n\r\nThis pattern has stopped working as of Python `v3.12.7`, likely due to #124631 which doesn't appear to have made it to v3.13.0, but it has been backported to the `3.13` branch. \r\n\r\nTo reproduce, \r\n1. Create an `issue.py` file\r\n```python\r\nimport argparse\r\nimport sys\r\nprint(f\":version {sys.version}\")\r\nparser = argparse.ArgumentParser()\r\nparser.add_argument(\"args\", nargs=-1)\r\nparsed_args, extra = parser.parse_known_args()\r\nprint(f\":args {parsed_args} :extra {extra}\")\r\n```\r\n2. and run it with python 3.12.7, you should get a `the following arguments are required` error.\r\n```console\r\n$ d:/local/Python312/python.exe issue.py 5 6\r\nusage: issue.py [-h]\r\nissue.py: error: the following arguments are required: args\r\n:version 3.12.7 (tags/v3.12.7:0b05ead, Oct 1 2024, 03:06:41) [MSC v.1941 64 bit (AMD64)]\r\n```\r\n\r\nThe same works with any other version at the momenet, for example 3.11 or 3.13:\r\n```console\r\n$ python.exe issue.py 5 6\r\n:version 3.11.4 (tags/v3.11.4:d2340ef, Jun 7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)]\r\n:args Namespace(args=[]) :extra ['5', '6']\r\n\r\n$ d:/local/Python313/python.exe issue.py 5 6\r\n:version 3.13.0 (tags/v3.13.0:60403a5, Oct 7 2024, 09:38:07) [MSC v.1941 64 bit (AMD64)]\r\n:args Namespace(args=[]) :extra ['5', '6']\r\n```\r\n\r\nCould you please take a look and advise whether this is something you might want to continue supporting, or if there are alternative approaches you would recommend?\r\n\r\nI don't personally use this pattern, but I came across it and thought it would be helpful to report, as it might assist developers in making informed decisions about it.\r\n\r\nThanks\r\n\r\n### CPython versions tested on:\r\n\r\n3.12, CPython main branch\r\n\r\n### Operating systems tested on:\r\n\r\nWindows","author":{"url":"https://github.com/ikappaki","@type":"Person","name":"ikappaki"},"datePublished":"2024-11-11T23:03:35Z","url":"https://github.com/python/cpython/issues/126708"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:7faf646d-9a04-ee03-c5c2-3c2ca5509081 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EB74:283776:1003502:15787A0:6B005C73 |
| html-safe-nonce | cf23a79c5028a2b93f4221d0f17e7fd91212b365364c571b4503d83793ff14fe |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQjc0OjI4Mzc3NjoxMDAzNTAyOjE1Nzg3QTA6NkIwMDVDNzMiLCJ2aXNpdG9yX2lkIjoiMzczMjQ1NzQ2Njg1MjU2NDA4MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 2a81dbd67226c82db374963cbc00f3c20213560849fa49fc41d5a48d0fd6babb |
| hovercard-subject-tag | issue:2650599822 |
| 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/126708/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4f24166f03e28595cd764ca387edee3b77a4640e89774eba5cae6493a061914f/python/cpython/issues/126708 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4f24166f03e28595cd764ca387edee3b77a4640e89774eba5cae6493a061914f/python/cpython/issues/126708 |
| og:image:alt | Bug report Bug description: It seems there's a pattern in the wild with argparse where nargs=-1 is used to capture the remaining command line arguments. However, I couldn’t find anything in the arg... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ikappaki |
| hostname | github.com |
| expected-hostname | github.com |
| None | 29be975efff6507a5d135de9c7d634cf7ff8f0c55222a64386ab3202209aaf73 |
| 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 | eacd927ba6112e3b01ba99c762bd699a4a4f5df7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width