Title: allow_abbrev=False does not work for single-dash long options · Issue #104860 · python/cpython · GitHub
Open Graph Title: allow_abbrev=False does not work for single-dash long options · Issue #104860 · python/cpython
X Title: allow_abbrev=False does not work for single-dash long options · Issue #104860 · python/cpython
Description: Bug report The following codes get different results between python3.6.9 and python3.8.10 import argparse if __name__ == "__main__": parser = argparse.ArgumentParser( allow_abbrev=False ) parser.add_argument('-on', "--output-name", type=...
Open Graph Description: Bug report The following codes get different results between python3.6.9 and python3.8.10 import argparse if __name__ == "__main__": parser = argparse.ArgumentParser( allow_abbrev=False ) parser.ad...
X Description: Bug report The following codes get different results between python3.6.9 and python3.8.10 import argparse if __name__ == "__main__": parser = argparse.ArgumentParser( allow_abbrev=False )...
Opengraph URL: https://github.com/python/cpython/issues/104860
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"allow_abbrev=False does not work for single-dash long options","articleBody":"# Bug report\r\n\r\nThe following codes get different results between python3.6.9 and python3.8.10\r\n\r\n```Python\r\nimport argparse\r\n\r\n\r\nif __name__ == \"__main__\":\r\n parser = argparse.ArgumentParser(\r\n allow_abbrev=False\r\n )\r\n\r\n parser.add_argument('-on', \"--output-name\", type=str, help=\"output\")\r\n\r\n args, _ = parser.parse_known_args([\"-on\", \"name\", \"-o\", \"dir\"])\r\n print(args)\r\n```\r\n\r\n## python3.6.9\r\n```Shell\r\n[chenf@b12e0231:/mnt/ssd/chenf/projects/test_hhb_2.4/debug/cmd_config]$ python3 --version\r\nPython 3.6.9\r\n[chenf@b12e0231:/mnt/ssd/chenf/projects/test_hhb_2.4/debug/cmd_config]$ python3 ~/test.py\r\nNamespace(output_name='name')\r\n```\r\n\r\n## python3.8.10\r\n```Shell\r\n[chenf@b11b0623:/mnt/ssd/chenf/project/hhb_test_2.4/cmd_config]$ python3 --version\r\nPython 3.8.10\r\n[chenf@b11b0623:/mnt/ssd/chenf/project/hhb_test_2.4/cmd_config]$ python3 ~/test.py\r\nNamespace(output_name='dir')\r\n```\r\n\r\n## Debug\r\nI have tried to debug the codes and get some clues.\r\n\r\nI have set the `allow_abbrev=False` to disable prefix match, but it works in python3.6.9 and fails in python3.8.10.\r\nWhen I div into `argparse.py` and find that it considers the allow_abbrev in python3.6.9 but not in python3.8.10 as follow:\r\n\r\n### in python3.6.9 argparse.py\r\n```Python\r\ndef _parse_optional(self, arg_string):\r\n ....\r\n\r\n if self.allow_abbrev:\r\n # search through all possible prefixes of the option string\r\n # and all actions in the parser for possible interpretations\r\n option_tuples = self._get_option_tuples(arg_string)\r\n\r\n # if multiple actions match, the option string was ambiguous\r\n if len(option_tuples) > 1:\r\n options = ', '.join([option_string\r\n for action, option_string, explicit_arg in option_tuples])\r\n args = {'option': arg_string, 'matches': options}\r\n msg = _('ambiguous option: %(option)s could match %(matches)s')\r\n self.error(msg % args)\r\n\r\n # if exactly one action matched, this segmentation is good,\r\n # so return the parsed action\r\n elif len(option_tuples) == 1:\r\n option_tuple, = option_tuples\r\n return option_tuple\r\n ....\r\n```\r\n\r\n### in python3.8.10 argparse.py\r\n\r\n```Python\r\ndef _parse_optional(self, arg_string):\r\n ....\r\n\r\n # search through all possible prefixes of the option string\r\n # and all actions in the parser for possible interpretations\r\n option_tuples = self._get_option_tuples(arg_string)\r\n\r\n # if multiple actions match, the option string was ambiguous\r\n if len(option_tuples) > 1:\r\n options = ', '.join([option_string\r\n for action, option_string, explicit_arg in option_tuples])\r\n args = {'option': arg_string, 'matches': options}\r\n msg = _('ambiguous option: %(option)s could match %(matches)s')\r\n self.error(msg % args)\r\n\r\n # if exactly one action matched, this segmentation is good,\r\n # so return the parsed action\r\n elif len(option_tuples) == 1:\r\n option_tuple, = option_tuples\r\n return option_tuple\r\n \r\n ....\r\n```\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-124340\n* gh-124749\n* gh-124750\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/SunCrazy","@type":"Person","name":"SunCrazy"},"datePublished":"2023-05-24T11:47:21Z","url":"https://github.com/python/cpython/issues/104860"}
| 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:3dd18113-9b95-9aec-6a2c-f10e292f417f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DB90:2EC689:2BDD7D8:3BDCA78:6B0058A0 |
| html-safe-nonce | 8d7a609c4b8a8809d071c497ab0d374396b97a91ef4dffca2ebd1e96fed4c2ae |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQjkwOjJFQzY4OToyQkREN0Q4OjNCRENBNzg6NkIwMDU4QTAiLCJ2aXNpdG9yX2lkIjoiMTA4MTU1Mzg0MzAyNTYzMTM5MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 9b09ac1a1ce95ce812896067e5b609f9a58de3bc8f3d187be95edbb9d63b60da |
| hovercard-subject-tag | issue:1723798570 |
| 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/104860/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9450b35bb2f2127919141dbda298eb843bf69cc3ea57e3f70108e4c8c30743c6/python/cpython/issues/104860 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9450b35bb2f2127919141dbda298eb843bf69cc3ea57e3f70108e4c8c30743c6/python/cpython/issues/104860 |
| og:image:alt | Bug report The following codes get different results between python3.6.9 and python3.8.10 import argparse if __name__ == "__main__": parser = argparse.ArgumentParser( allow_abbrev=False ) parser.ad... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | SunCrazy |
| 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