Commit graph

157 commits

Author SHA1 Message Date
Tomas R.
1c158705f1
[3.13] gh-126413: Add translation tests for getopt and optparse (GH-126698) (GH-126755)
(cherry picked from commit dff074d144)
2024-11-15 13:06:54 +02:00
Serhiy Storchaka
ff044ed800
[3.13] gh-124295: Add translation tests for argparse (GH-124803) (GH-126046)
(cherry picked from commit 0922a4ae0d)

Co-authored-by: Tomas R. <tomas.roun8@gmail.com>
2024-10-27 18:35:30 +00:00
Miss Islington (bot)
1fe63b15eb
[3.13] gh-125355: Rewrite parse_intermixed_args() in argparse (GH-125356) (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 759a54d28f)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-10-22 15:58:05 +03:00
Serhiy Storchaka
d3d306a9d6
[3.13] gh-86357: argparse: use str() consistently and explicitly to print choices (GH-117766) (GH-125431)
(cherry picked from commit 66b3922b97)

Signed-off-by: Jan Chren ~rindeal <dev.rindeal@gmail.com>
Co-authored-by: rindeal <dev.rindeal@gmail.com>
2024-10-14 07:09:06 +00:00
Miss Islington (bot)
33c41360c8
[3.13] gh-125254: Fix error report about ambiguous option in argparse (GH-125273) (GH-125359)
This was a regression introduced in gh-58573. It was only tested for the
case when the ambiguous option is the last argument in the command line.
(cherry picked from commit 63cf4e914f)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-10-12 16:00:41 +03:00
Miss Islington (bot)
079bf1c31c
[3.13] gh-61011: Fix inheritance of nested mutually exclusive groups in argparse (GH-125210) (GH-125308)
Previously, all nested mutually exclusive groups lost their connection
to the group containing them and were displayed as belonging directly
to the parser.

(cherry picked from commit 18c7449768)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Danica J. Sutherland <djsutherland@users.noreply.github.com>
2024-10-11 09:14:04 +00:00
Serhiy Storchaka
ea2ccb3d43
[3.13] gh-85935: Improve tests for invalid arguments in test_argparse (GH-124891) (GH-124901)
Check also specific error messages.
(cherry picked from commit 2c050d4bc2)
2024-10-08 11:19:30 +03:00
Serhiy Storchaka
e28f2c6705
[3.13] gh-58282: Fix support of tuple metavar for positional arguments in argparse (GH-124782) (GH-124882)
Previously, formatting help output or error message for positional argument
with a tuple metavar raised exception.

(cherry picked from commit 9b31a2d83f)

Co-authored-by: Cyker Way <cykerway@gmail.com>
2024-10-08 06:36:38 +00:00
Serhiy Storchaka
6925e5b5c7
[3.13] gh-58573: Fix conflicts between abbreviated long options in the parent parser and subparsers in argparse (GH-124631) (GH-124760)
Check for ambiguous options if the option is consumed, not when it is
parsed.
(cherry picked from commit 3f27153e07)
2024-10-07 22:55:27 +00:00
Miss Islington (bot)
0a046771c0
[3.13] gh-72795: Make positional arguments with nargs='*' or REMAINDER non-required (GH-124306) (#124421)
This allows to use positional argument with nargs='*' and without default
in mutually exclusive group and improves error message about required
arguments.
(cherry picked from commit 3c83f9958c)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-10-07 22:51:35 +00:00
Miss Islington (bot)
db3ccd8b62
[3.13] gh-53780: Ignore the first "--" (double dash) between an option and command in argparse (GH-124275) (GH-125073)
(cherry picked from commit c578271366)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-10-07 22:51:07 +00:00
Miss Islington (bot)
2a40dda89d
[3.13] gh-95468: Add more tests for "--" (double dash) in test_argparse (GH-124274) (GH-125068)
(cherry picked from commit baa3550bc3)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-10-07 21:40:47 +00:00
Miss Islington (bot)
4a9a359f32
[3.13] gh-61181: Fix support of choices with string value in argparse (GH-124578) (GH-124755)
Substrings of the specified string no longer considered valid values.
(cherry picked from commit f1a2417b9e)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-10-08 00:28:17 +03:00
Miss Islington (bot)
11d4b54b88
[3.13] gh-116850: Fix argparse for namespaces with not directly writable dict (GH-124667) (GH-124757)
It now always uses setattr() instead of setting the dict item to modify
the namespace. This allows to use a class as a namespace.
(cherry picked from commit 95e92ef6c7)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-10-08 00:27:11 +03:00
Miss Islington (bot)
597b6211ab
[3.13] gh-124345: Support abbreviated single-dash long options with = in argparse (GH-124428) (GH-124753)
(cherry picked from commit 61180446ee)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-10-08 00:26:37 +03:00
Miss Islington (bot)
f28906e58e
[3.13] gh-80259: Fix conflict between type and default=SUPPRESS in argparse (GH-124519) (GH-124751)
type() no longer called for SUPPRESS.

This only affects positional arguments with nargs='?'.
(cherry picked from commit 9bcadf589a)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-10-08 00:26:00 +03:00
Miss Islington (bot)
aa648c21e9
[3.13] gh-104860: Fix allow_abbrev=False for single-dash long options (GH-124340) (GH-124749)
(cherry picked from commit 49e105f948)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-10-08 00:25:13 +03:00
Miss Islington (bot)
16127de2eb
[3.13] gh-59317: Improve parsing optional positional arguments in argparse (GH-124303) (GH-124436)
Fix parsing positional argument with nargs equal to '?' or '*' if it is
preceded by an option and another positional argument.
(cherry picked from commit 4a5e4aade4)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-10-08 00:24:31 +03:00
Miss Islington (bot)
167d8d2f07
[3.13] gh-63143: Fix parsing mutually exclusive arguments in argparse (GH-124307) (GH-124418)
Arguments with the value identical to the default value (e.g. booleans,
small integers, empty or 1-character strings) are no longer considered
"not present".
(cherry picked from commit 3094cd17b0)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-10-08 00:20:31 +03:00
Miss Islington (bot)
63870162f4
[3.13] gh-81691: Fix handling of multiple "--" (double dashes) in argparse (GH-124233) (GH-124266)
Only the first one has now been removed, all subsequent ones are now
taken literally.
(cherry picked from commit aae126748f)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-10-08 00:12:28 +03:00
Miss Islington (bot)
dceac5b8ea
[3.13] gh-124245: Fix UserWarning in test_argparse (GH-124246) (#124255)
gh-124245: Fix UserWarning in test_argparse (GH-124246)
(cherry picked from commit 992e8f6102)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-09-29 18:07:04 -07:00
Miss Islington (bot)
8d3a0fecbe
[3.13] GH-87041: Fix incorrect indentation in argparse help (GH-124230) (#124373)
GH-87041: Fix incorrect indentation in argparse help (GH-124230)

In case of usage a long command along with max_help_position more than
the length of the command, the command's help was incorrectly started
on the new line.

(cherry picked from commit 7ee9921734)

Co-authored-by: Savannah Ostrowski <savannahostrowski@gmail.com>
Co-authored-by: Pavel Ditenbir <pavel.ditenbir@gmail.com>
2024-09-29 18:05:51 -07:00
Miss Islington (bot)
f1a6d2254f
[3.13] gh-121151: argparse: Fix wrapping of long usage text of arguments inside a mutually exclusive groups (GH-121159) (#122777)
gh-121151: argparse: Fix wrapping of long usage text of arguments inside a mutually exclusive groups (GH-121159)
(cherry picked from commit 013a092975)

Co-authored-by: Ali Hamdan <ali.hamdan.dev@gmail.com>
2024-09-02 12:55:16 +02:00
Serhiy Storchaka
99de20d729
[3.13] gh-121018: Fix more cases of exiting in argparse when exit_on_error=False (GH-121056) (GH-121128)
* parse_intermixed_args() now raises ArgumentError instead of calling
  error() if exit_on_error is false.
* Internal code now always raises ArgumentError instead of calling
  error(). It is then caught at the higher level and error() is called if
  exit_on_error is true.
(cherry picked from commit 81a654a342)
2024-06-28 15:04:44 +00:00
Miss Islington (bot)
6bc7e2cca5
[3.13] gh-121018: Ensure ArgumentParser.parse_args with exit_on_error=False raises instead of exiting when given unrecognized arguments (GH-121019) (GH-121032)
(cherry picked from commit 0654336dd5)

Co-authored-by: blhsing <blhsing@gmail.com>
2024-06-26 10:16:19 +00:00
Ali Hamdan
de1428f8c2
gh-62090: Simplify argparse usage formatting (GH-105039)
Rationale
=========

argparse performs a complex formatting of the usage for argument grouping
and for line wrapping to fit the terminal width. This formatting has been
a constant source of bugs for at least 10 years (see linked issues below)
where defensive assertion errors are triggered or brackets and paranthesis
are not properly handeled.

Problem
=======

The current implementation of argparse usage formatting relies on regular
expressions to group arguments usage only to separate them again later
with another set of regular expressions. This is a complex and error prone
approach that caused all the issues linked below. Special casing certain
argument formats has not solved the problem. The following are some of
the most common issues:
- empty `metavar`
- mutually exclusive groups with `SUPPRESS`ed arguments
- metavars with whitespace
- metavars with brackets or paranthesis

Solution
========

The following two comments summarize the solution:
- https://github.com/python/cpython/issues/82091#issuecomment-1093832187
- https://github.com/python/cpython/issues/77048#issuecomment-1093776995

Mainly, the solution is to rewrite the usage formatting to avoid the
group-then-separate approach. Instead, the usage parts are kept separate
and only joined together at the end. This allows for a much simpler
implementation that is easier to understand and maintain. It avoids the
regular expressions approach and fixes the corresponding issues.

This closes the following GitHub issues:
-  #62090
-  #62549
-  #77048
-  #82091
-  #89743
-  #96310
-  #98666

These PRs become obsolete:
-  #15372
-  #96311
2024-05-07 09:28:51 +02:00
Daniel Mach
5f7df88821
gh-96310: Fix a traceback in argparse when all options in a mutually exclusive group are suppressed (GH-96311)
Reproducer depends on terminal size - the traceback occurs when there's
an option long enough so the usage line doesn't fit the terminal width.
Option order is also important for reproducibility.

Excluding empty groups (with all options suppressed) from inserts
fixes the problem.
2024-02-21 13:58:04 +00:00
Serhiy Storchaka
e47ecbd042
gh-60346: Improve handling single-dash options in ArgumentParser.parse_known_args() (GH-114180) 2024-02-19 19:20:00 +02:00
Serhiy Storchaka
bb57ffdb38
gh-83648: Support deprecation of options, arguments and subcommands in argparse (GH-114086) 2024-02-06 00:41:34 +02:00
Serhiy Storchaka
4aa4f0906d
gh-109475: Fix support of explicit option value "--" in argparse (GH-114814)
For example "--option=--".
2024-02-05 22:42:43 +02:00
Jokimax
c4a2e8a2c5
gh-101599: argparse: simplify the option help string (GH-103372)
If the option with argument has short and long names,
output argument only once, after the long name:

   -o, --option ARG    description

instead of

   -o ARG, --option ARG    description
2024-02-02 22:13:00 +00:00
Tian Gao
9efaff5fd3
gh-103558: Add coverage tests for argparse (#103570)
Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
Co-authored-by: hauntsaninja <hauntsaninja@gmail.com>
2023-06-05 00:14:00 -07:00
Nikita Sobolev
27a7d5e1cd
gh-92248: Deprecate type, choices, metavar parameters of argparse.BooleanOptionalAction (#103678)
Co-authored-by: Kirill <80244920+Eclips4@users.noreply.github.com>
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
2023-05-19 16:44:43 +00:00
Oleg Iarygin
42f54d1f92
gh-101640: Make argparse _print_message catch any write error (#101802)
* In particular, don't exit when trying to print to stderr = None.
* Add tests

Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
2023-05-06 18:53:48 -04:00
Yeojin Kim
9a478be1a4
gh-101979: argparse: fix a bug where parentheses in metavar argument of add_argument() were dropped (#102318) 2023-03-05 06:54:33 -08:00
Hai Shi
e02cc6d42a
gh-80448: argparse: Fix IndexError on store_true action (#15656)
Co-authored-by: Rémi Lapeyre <remi.lapeyre@henki.fr>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
2022-11-11 17:51:38 -08:00
Harry
ad7340e8c5
gh-92445 Improve interaction between nargs="*" and choices() (GH-92565) 2022-08-25 06:18:38 -05:00
Christian Heimes
7e0d98ecb3
gh-94315: Check for DAC override capability (GH-94316)
``os.geteuid() == 0`` is not a reliable check whether the current user
has the capability to bypass permission checks. Tests now probe for DAC
override.
2022-06-27 20:27:19 +02:00
Serhiy Storchaka
605e9c66ad
gh-85308: Add argparse tests for reading non-ASCII arguments from file (GH-94160) 2022-06-24 23:09:13 +03:00
Christian Heimes
22fed605e0
gh-90473: Make chmod a dummy on WASI, skip chmod tests (GH-93534)
WASI does not have the ``chmod(2)`` syscall yet.
2022-06-06 19:24:11 +02:00
Serhiy Storchaka
086c6b1b0f
bpo-45046: Support context managers in unittest (GH-28045)
Add methods enterContext() and enterClassContext() in TestCase.
Add method enterAsyncContext() in IsolatedAsyncioTestCase.
Add function enterModuleContext().
2022-05-08 17:49:09 +03:00
Toshio Kuratomi
20490d5018
gh-88753: Make BooleanOptionalAction's addition of default to help more similar to other actions (#27808)
Help for other actions omit the default value if default is SUPPRESS or
already contains the special format string '%(default)'.  Add those
special cases to BooleanOptionalAction's help formatting too.

Fixes https://bugs.python.org/issue44587 so that default=SUPPRESS is not
emitted.

Fixes https://bugs.python.org/issue38956 as this code will detect
whether '%(default)s' has already been specified in the help string.

Signed-off-by: Micky Yun Chan (michiboo): <chanmickyyun@gmail.com>
Co-authored-by: Micky Yun Chan <michan@redhat.com>
2022-05-03 18:38:18 +02:00
Antony Lee
ad5e8520f3
bpo-39716: Raise on conflicting subparser names. (GH-18605)
Raise an ArgumentError when the same subparser name is added twice to an
ArgumentParser.  This is consistent with the (default) behavior when the
same option string is added twice to an ArgumentParser.

(Support for `conflict_handler="resolve"` could be considered as a
followup feature, although real use cases seem even rarer than
"resolve"ing option-strings.)

Automerge-Triggered-By: GH:rhettinger
2022-04-30 23:04:50 -07:00
Abhigyan Bose
4ed3900041
gh-91832: Add 'required' attr to argparse.Action repr (GH-91841)
# Adding 'required' to names in Lib.argparse.Action

gh-91832: 
Added 'required' to the list `names` in `Lib.argparse.Action`. 
Changed constant strings that test the Action object.

Automerge-Triggered-By: GH:merwok
2022-04-28 07:50:27 -07:00
Abhigyan Bose
88dd227959
gh-91984: Fix trailing spaces in multiline test strings in test_argparse (GH-91986) 2022-04-28 14:29:24 +03:00
MojoVampire
eafec26ae5
bpo-14156: Make argparse.FileType work correctly for binary file modes when argument is '-' (GH-13165)
Also made modes containing 'a' or 'x' act the same as a mode containing 'w' when argument is '-'
(so 'a'/'x' return sys.stdout like 'w', and 'ab'/'xb' return sys.stdout.buffer like 'wb').
2022-03-06 13:49:42 +02:00
Felix Fontein
9e87c0e03f
bpo-46080: fix argparse help generation exception in edge case (GH-30111)
Fix an uncaught exception during help text generation when
argparse.BooleanOptionalAction is used with default=argparse.SUPPRESS
and help is specified.
2022-01-21 00:48:48 +02:00
Irit Katriel
a287b31bcb
bpo-46411: Remove unnecessary calls to sys.exc_info() in tests (GH-30638) 2022-01-18 07:05:16 +00:00
Irit Katriel
30322c497e
bpo-22047: [argparse] deprecate nested argument groups and mutually exclusive groups (GH-30098) 2021-12-16 15:31:08 +00:00
Irit Katriel
86de99588d
bpo-26952: [argparse] clearer error when formatting an empty mutually… (GH-30099) 2021-12-15 10:08:26 +00:00