GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Support options with optional arguments · Issue #126374 · python/cpython

Support options with optional arguments #126374

New issue
Copy link
New issue
Copy link
Closed
Closed
Support options with optional arguments#126374
Copy link
Labels
3.14bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

@serhiy-storchaka
serhiy-storchaka
opened on Nov 3, 2024
Issue body actions

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

  • gh-126374: Add support of options with optional arguments in the getopt module #126375
Reacted by Václav Brožík

Activity

  1. serhiy-storchaka
    added
    type-featureA feature request or enhancementA feature request or enhancement
    3.14bugs and security fixesbugs and security fixes
    on Nov 3, 2024
  2. serhiy-storchaka
    added a commit that references this issue on Nov 3, 2024

    pythongh-126374: Add support of options with optional arguments in th…

    3ef0f08
  3. erlend-aasland
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Nov 3, 2024
  4. picnixz
    removed
    3.14bugs and security fixesbugs and security fixes
    on Nov 6, 2024
  5. serhiy-storchaka
    added a commit that references this issue on Nov 11, 2024

    gh-126374: Add support of options with optional arguments in the geto…

    25aee21
  6. serhiy-storchaka
    closed this as completedon Nov 11, 2024
  7. serhiy-storchaka
    added
    3.14bugs and security fixesbugs and security fixes
    on Nov 11, 2024
  8. vbrozik commented on Nov 13, 2024

    @vbrozik
    vbrozik
    on Nov 13, 2024
    More actions

    @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.

  9. serhiy-storchaka commented on Nov 13, 2024

    @serhiy-storchaka
    serhiy-storchaka
    on Nov 13, 2024
    MemberAuthor
    More actions

    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.

    Reacted by Václav Brožík
  10. picnixz
    added a commit that references this issue on Dec 8, 2024

    pythongh-126374: Add support of options with optional arguments in th…

    de49afc
  11. mhsmith commented on Dec 18, 2024

    @mhsmith
    mhsmith
    on Dec 18, 2024
    Member
    More actions

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

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

  12. serhiy-storchaka commented on Dec 18, 2024

    @serhiy-storchaka
    serhiy-storchaka
    on Dec 18, 2024
    MemberAuthor
    More actions

    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.

    Reacted by Václav Brožík
  13. ebonnal
    added a commit that references this issue on Jan 12, 2025

    pythongh-126374: Add support of options with optional arguments in th…

    f41632c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.