Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support options with optional arguments #126374

Closed
serhiy-storchaka opened this issue Nov 3, 2024 · 4 comments
Closed

Support options with optional arguments #126374

serhiy-storchaka opened this issue Nov 3, 2024 · 4 comments
Labels
3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Nov 3, 2024

Feature or enhancement

GNU getopt supports options with optional arguments. For example GNU grep can be used with option --color=auto and with just --color. Unix getopt does not need it, because it stops after encountering first nonoption argument, but GNU getopt needs it to support options intermixed with positional arguments. getopt.gnu_getopt() needs it for the same reason.

Neither optparse nor argparse directly support such kind of optional arguments. It can be supported in optparse with custom callback, but support of optional arguments in argparse is non-standard. In future I'm planning to add support of such kind of optional arguments in argparse, and maybe in optparse.

Linked PRs

@serhiy-storchaka serhiy-storchaka added type-feature A feature request or enhancement 3.14 new features, bugs and security fixes labels Nov 3, 2024
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Nov 3, 2024
@erlend-aasland erlend-aasland added the stdlib Python modules in the Lib dir label Nov 3, 2024
@picnixz picnixz removed the 3.14 new features, bugs and security fixes label Nov 6, 2024
@serhiy-storchaka serhiy-storchaka added the 3.14 new features, bugs and security fixes label Nov 11, 2024
@vbrozik
Copy link

vbrozik commented Nov 13, 2024

@serhiy-storchaka Thank you for implementing this but is anyone using the getopt module in a new code? Is the feature still planned to be implemented for argparse?

Since Python 3.13 the getopt module is soft-deprecated:

Deprecated since version 3.13: The getopt module is soft deprecated and will not be developed further; development will continue with the argparse module.

@serhiy-storchaka
Copy link
Member Author

The Python implementation of getopt should be as powerful and flexible as the C implementation. This will help to port programs from other programming languages to Python. This feature was in the TODO list since beginning.

This feature will also be added in argparse, I just haven't decided on one of the three interface options yet (special nargs value, new add_argument() option, or the part of a new argparse mode). I will decide after implementing that new mode.

I thing that options with optional argument can be supported in optparse by using a custom callback. I am not sure whether a more direct support is needed.

This module will be undeprecated (see #126180). It was not changed (except mass stdlib changes) in the last 15 years, so it is difficult to find an interested person for review. I asked for help and @gpshead reviewed #126393 which was created on the top of #126375. His comment did not question the feasibility of these changes, only the style of the tests code. Which I took as acceptance in principle.

@mhsmith
Copy link
Member

mhsmith commented Dec 18, 2024

Neither optparse nor argparse directly support such kind of optional arguments.

Isn't this supported by argparse's nargs='?'?

@serhiy-storchaka
Copy link
Member Author

No, argparse's nargs='?' has different semantic. In argparse, --color=auto and --color auto are the same. The value can only be omitted if the option is at the end of the command line or followed by other option or --, e.g. --color --recursive auto or --color -- auto.

ebonnal pushed a commit to ebonnal/cpython that referenced this issue Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

5 participants