Skip to content

Rewrite parse_intermixed_args() in argparse #125355

Closed

Description

@serhiy-storchaka

parse_intermixed_args() and parse_known_intermixed_args() are implemented by parsing command lines twice -- first only optional arguments, then the remaining is parsed as positional arguments. This approach has some issues.

  • The parser is temporary modified to suppress positional or required optional arguments in these two stages. This is not good, because the parser can no longer be used concurrently. This also smells bad in general, this can hide some bugs.
  • Default values are handled twice.
  • Unknown options in parse_known_intermixed_args() cannot be intermixed with positional arguments. Well, "parsing only known arguments" is a dubious feature, but still...

I tried to rewrite the implementation by moving the code deeper. No longer parser patching, defaults are handled only once, unknown options are excluded from positionals parsing. @hpaulj, @bitdancer, as the authors of the original implementation, could you please review this code?

Linked PRs

Activity

  1. added a commit that references this issue on Oct 12, 2024
  2. serhiy-storchaka commented on Oct 12, 2024

    @serhiy-storchaka
    MemberAuthor

    For reference, the original feature was implemented in bpo-14191(gh-58399), PR #3319, 0f6b9d2.

  3. serhiy-storchaka commented on Oct 12, 2024

    @serhiy-storchaka
    MemberAuthor

    This also fixes support for required mutually exclusive groups containing positionals.

  4. added
    type-featureA feature request or enhancement
    stdlibStandard Library Python modules in the Lib/ directory
    on Oct 13, 2024
  5. added a commit that references this issue on Oct 22, 2024
  6. added a commit that references this issue on Oct 22, 2024
  7. added 3 commits that reference this issue on Oct 22, 2024
  8. added a commit that references this issue on Jan 12, 2025
  9. Erotemic commented on Jan 16, 2025

    @Erotemic
    Contributor

    Wish the internal API for _parse_known_args had a default of intermixed=False so usage of it wasn't broken. This broke scriptconfig. I'm able to work around it on my end though.

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

Metadata

Metadata

Labels

stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions