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

Rewrite parse_intermixed_args() in argparse #125355

Closed
serhiy-storchaka opened this issue Oct 12, 2024 · 2 comments · Fixed by #125356
Closed

Rewrite parse_intermixed_args() in argparse #125355

serhiy-storchaka opened this issue Oct 12, 2024 · 2 comments · Fixed by #125356
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Oct 12, 2024

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

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Oct 12, 2024
* The parser is no longer temporary modified and can be used
  concurrently.
* Default values are no long handled twice.
* Report about missed arguments now contains names of all required optional
  and positional arguments.
* Unknown options can be intermixed with positionals in
  parse_known_intermixed_args().
@serhiy-storchaka
Copy link
Member Author

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

@serhiy-storchaka
Copy link
Member Author

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

@serhiy-storchaka serhiy-storchaka self-assigned this Oct 12, 2024
@picnixz picnixz added type-feature A feature request or enhancement stdlib Python modules in the Lib dir labels Oct 13, 2024
serhiy-storchaka added a commit that referenced this issue Oct 22, 2024
* The parser no longer changes temporarily during parsing.
* Default values are not processed twice.
* Required mutually exclusive groups containing positional arguments are
  now supported.
* The missing arguments report now includes the names of all required
  optional and positional arguments.
* Unknown options can be intermixed with positional arguments in
  parse_known_intermixed_args().
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 22, 2024
…H-125356)

* The parser no longer changes temporarily during parsing.
* Default values are not processed twice.
* Required mutually exclusive groups containing positional arguments are
  now supported.
* The missing arguments report now includes the names of all required
  optional and positional arguments.
* Unknown options can be intermixed with positional arguments in
  parse_known_intermixed_args().
(cherry picked from commit 759a54d)

Co-authored-by: Serhiy Storchaka <[email protected]>
serhiy-storchaka added a commit that referenced this issue Oct 22, 2024
) (GH-125834)

* The parser no longer changes temporarily during parsing.
* Default values are not processed twice.
* Required mutually exclusive groups containing positional arguments are
  now supported.
* The missing arguments report now includes the names of all required
  optional and positional arguments.
* Unknown options can be intermixed with positional arguments in
  parse_known_intermixed_args().
(cherry picked from commit 759a54d)

Co-authored-by: Serhiy Storchaka <[email protected]>
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Oct 22, 2024
…ythonGH-125356)

* The parser no longer changes temporarily during parsing.
* Default values are not processed twice.
* Required mutually exclusive groups containing positional arguments are
  now supported.
* The missing arguments report now includes the names of all required
  optional and positional arguments.
* Unknown options can be intermixed with positional arguments in
  parse_known_intermixed_args().
(cherry picked from commit 759a54d)

Co-authored-by: Serhiy Storchaka <[email protected]>
serhiy-storchaka added a commit that referenced this issue Oct 22, 2024
) (GH-125839)

* The parser no longer changes temporarily during parsing.
* Default values are not processed twice.
* Required mutually exclusive groups containing positional arguments are
  now supported.
* The missing arguments report now includes the names of all required
  optional and positional arguments.
* Unknown options can be intermixed with positional arguments in
  parse_known_intermixed_args().

(cherry picked from commit 759a54d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
Status: Doc issues
Development

Successfully merging a pull request may close this issue.

2 participants