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 — Preserving order of options and nonoption arguments in gnu_getopt() · Issue #126390 · python/cpython

Preserving order of options and nonoption arguments in gnu_getopt() #126390

New issue
Copy link
New issue
Copy link
Closed
Closed
Preserving order of options and nonoption arguments in gnu_getopt()#126390
Copy link
Assignees
serhiy-storchaka
Labels
stdlibStandard 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 4, 2024
Issue body actions

Feature or enhancement

by default GNU getopt() permutes the contents of argv as it scans, so that eventually all the nonoptions are at the end. It supports two other scanning modes -- one compatible with Posix getopt() (stop at first nonoption argument), and other allows to return options and nonoption arguments in order.

getopt.gnu_getopt() returns two lists -- the list of option-and-value pairs and the list of nonoption arguments. Thus, the relative order of options and nonoption arguments is lost.

I propose to add support for the missed feature. If the first character of the option string is minus ('-'), non-option arguments that are followed by options will be added to the list of option-and-value pairs as a pair that has None as its first element and the list of non-option arguments as its second element. Non-option arguments that follow the last option will be returned as the second element of the gnu_getopt() result.

For example:

>>> s = 'a1 -x a2 a3 a4 --long a5 a6'
>>> args = s.split()
>>> args
['a1', '-x', 'a2', 'a3', 'a4', '--long', 'a5', 'a6']
>>> optlist, args = getopt.gnu_getopt(args, '-x:', ['long='])
>>> optlist
[(None, ['a1']), ('-x', 'a2'), (None, ['a3', 'a4']), ('--long', 'a5')]
>>> args
['a6']

I considered also alternative options:

  • Returning each nonoption argument as a separate (None, value) tuple instead of (None, list). But if you need a list of consequent arguments, it will be less convenient to build it from separate argument. And if you need separate arguments, you can simply iterate the list.
  • Returning the last portion of nonoption arguments also as a (None, list) pair. This makes handling nonoption arguments followed and not followed by options in user code uniform. But then the second element of the gnu_getopt() result will always be an empty list. I am not sure about this.
  • Add the third function (gnu_getopt_iter()?) which is a generator function and emits (option, value) or (None, list) pairs.

Linked PRs

  • gh-126390: Support for preserving order of options and nonoption arguments in gnu_getopt() #126393

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 4, 2024
  2. serhiy-storchaka
    self-assigned this
    on Nov 4, 2024
  3. serhiy-storchaka
    added a commit that references this issue on Nov 4, 2024

    pythongh-126390: Support for preserving order of options and nonoptio…

    0c40533
  4. erlend-aasland
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Nov 4, 2024
  5. picnixz
    removed
    3.14bugs and security fixesbugs and security fixes
    on Nov 6, 2024
  6. serhiy-storchaka
    added 2 commits that reference this issue on Nov 11, 2024

    pythongh-126390: Support for preserving order of options and nonoptio…

    0659bd2

    gh-126390: Support for preserving order of options and nonoption argu…

    35010b8
  7. serhiy-storchaka
    closed this as completedon Nov 13, 2024
  8. picnixz
    added a commit that references this issue on Dec 8, 2024

    pythongh-126390: Support for preserving order of options and nonoptio…

    6f3cc66
  9. ebonnal
    added a commit that references this issue on Jan 12, 2025

    pythongh-126390: Support for preserving order of options and nonoptio…

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

Metadata

Metadata

Assignees

  • @serhiy-storchaka
    serhiy-storchaka

Labels

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