You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
@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.
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.
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.
Activity
pythongh-126374: Add support of options with optional arguments in th…
gh-126374: Add support of options with optional arguments in the geto…
vbrozik commentedon Nov 13, 2024
@serhiy-storchaka Thank you for implementing this but is anyone using the
getoptmodule in a new code? Is the feature still planned to be implemented forargparse?Since Python 3.13 the
getoptmodule is soft-deprecated:serhiy-storchaka commentedon Nov 13, 2024
The Python implementation of
getoptshould 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 (specialnargsvalue, newadd_argument()option, or the part of a newargparsemode). I will decide after implementing that new mode.I thing that options with optional argument can be supported in
optparseby 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.
pythongh-126374: Add support of options with optional arguments in th…
mhsmith commentedon Dec 18, 2024
Isn't this supported by argparse's
nargs='?'?serhiy-storchaka commentedon Dec 18, 2024
No, argparse's
nargs='?'has different semantic. In argparse,--color=autoand--color autoare 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 autoor--color -- auto.pythongh-126374: Add support of options with optional arguments in th…