Commit graph

150 commits

Author SHA1 Message Date
Serhiy Storchaka
4c039c0723
[3.12] gh-124295: Add translation tests for argparse (GH-124803) (GH-126046) (GH-126054)
(cherry picked from commit 0922a4ae0d)
(cherry picked from commit ff044ed800)

Co-authored-by: Tomas R. <tomas.roun8@gmail.com>
2024-10-27 18:57:11 +00:00
Serhiy Storchaka
da3d81d3ea
[3.12] gh-125355: Rewrite parse_intermixed_args() in argparse (GH-125356) (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 759a54d28f)
2024-10-22 13:23:30 +00:00
Serhiy Storchaka
21524eec48
[3.12] gh-86357: argparse: use str() consistently and explicitly to print choices (GH-117766) (GH-125432)
(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:04:44 +00:00
Miss Islington (bot)
aa0cdeb93c
[3.12] gh-125254: Fix error report about ambiguous option in argparse (GH-125273) (GH-125360)
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:24 +03:00
Miss Islington (bot)
23cefd9f4c
[3.12] gh-61011: Fix inheritance of nested mutually exclusive groups in argparse (GH-125210) (GH-125309)
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:07:03 +00:00
Miss Islington (bot)
6660d29745
[3.12] gh-85935: Improve tests for invalid arguments in test_argparse (GH-124891) (GH-124898)
Check also specific error messages.
(cherry picked from commit 2c050d4bc2)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-10-02 17:31:20 +03:00
Miss Islington (bot)
72bbebdfd9
[3.12] gh-58282: Fix support of tuple metavar for positional arguments in argparse (GH-124782) (GH-124881)
Previously, formatting help output or error message for positional argument
with a tuple metavar raised exception.

(cherry picked from commit 9b31a2d83f)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Cyker Way <cykerway@gmail.com>
2024-10-02 12:07:38 +03:00
Miss Islington (bot)
cbea45ad74
[3.12] gh-58573: Fix conflicts between abbreviated long options in the parent parser and subparsers in argparse (GH-124631) (GH-124759)
Check for ambiguous options if the option is consumed, not when it is
parsed.
(cherry picked from commit 3f27153e07)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-09-29 09:18:06 +00:00
Miss Islington (bot)
5464c8aa98
[3.12] gh-116850: Fix argparse for namespaces with not directly writable dict (GH-124667) (GH-124758)
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-09-29 08:40:00 +00:00
Miss Islington (bot)
7677be5ee6
[3.12] gh-61181: Fix support of choices with string value in argparse (GH-124578) (GH-124756)
Substrings of the specified string no longer considered valid values.
(cherry picked from commit f1a2417b9e)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-09-29 08:32:51 +00:00
Miss Islington (bot)
71a2b8d185
[3.12] gh-124345: Support abbreviated single-dash long options with = in argparse (GH-124428) (GH-124754)
(cherry picked from commit 61180446ee)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-09-29 08:30:26 +00:00
Miss Islington (bot)
bdcdb0ac7f
[3.12] gh-80259: Fix conflict between type and default=SUPPRESS in argparse (GH-124519) (GH-124752)
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-09-29 08:17:08 +00:00
Miss Islington (bot)
00fd32af21
[3.12] gh-104860: Fix allow_abbrev=False for single-dash long options (GH-124340) (GH-124750)
(cherry picked from commit 49e105f948)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-09-29 08:16:38 +00:00
Miss Islington (bot)
996e409401
[3.12] gh-59317: Improve parsing optional positional arguments in argparse (GH-124303) (GH-124437)
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-09-24 13:08:22 +00:00
Miss Islington (bot)
7e2d414a59
[3.12] gh-72795: Make positional arguments with nargs='*' or REMAINDER non-required (GH-124306) (GH-124422)
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-09-24 11:43:26 +03:00
Miss Islington (bot)
0e838b52fe
[3.12] gh-53780: Ignore the first "--" (double dash) between an option and command in argparse (GH-124275) (GH-124420)
(cherry picked from commit c578271366)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-09-24 07:54:32 +00:00
Miss Islington (bot)
e57fbe3d2d
[3.12] gh-63143: Fix parsing mutually exclusive arguments in argparse (GH-124307) (GH-124419)
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-09-24 07:42:34 +00:00
Miss Islington (bot)
7cc773ba3d
[3.12] GH-87041: Fix incorrect indentation in argparse help (GH-124230) (GH-124374)
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-23 20:16:45 +00:00
Miss Islington (bot)
25312403de
[3.12] gh-95468: Add more tests for "--" (double dash) in test_argparse (GH-124274) (GH-124276)
(cherry picked from commit baa3550bc3)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-09-22 20:42:14 +03:00
Miss Islington (bot)
6c6b044304
[3.12] gh-81691: Fix handling of multiple "--" (double dashes) in argparse (GH-124233) (GH-124267)
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-09-20 13:02:30 +03:00
Serhiy Storchaka
8ea6cc14a5
[3.12] gh-121018: Fix more cases of exiting in argparse when exit_on_error=False (GH-121056) (GH-121129)
* 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 14:52:07 +00:00
Miss Islington (bot)
4868d1bab1
[3.12] gh-121018: Ensure ArgumentParser.parse_args with exit_on_error=False raises instead of exiting when given unrecognized arguments (GH-121019) (GH-121031)
(cherry picked from commit 0654336dd5)

Co-authored-by: blhsing <blhsing@gmail.com>
2024-06-26 10:16:44 +00:00
Miss Islington (bot)
dae7341da2
[3.12] gh-96310: Fix a traceback in argparse when all options in a mutually exclusive group are suppressed (GH-96311) (GH-115767)
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.
(cherry picked from commit 5f7df88821)

Co-authored-by: Daniel Mach <daniel.mach@suse.com>
2024-02-21 18:20:29 +02:00
Miss Islington (bot)
cffb4c78d3
[3.12] gh-60346: Improve handling single-dash options in ArgumentParser.parse_known_args() (GH-114180) (GH-115675)
(cherry picked from commit e47ecbd042)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-02-19 18:25:09 +00:00
Miss Islington (bot)
94ad68264c
[3.12] gh-109475: Fix support of explicit option value "--" in argparse (GH-114814) (GH-115036)
For example "--option=--".
(cherry picked from commit 4aa4f0906d)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-02-05 21:02:00 +00: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
Raymond Hettinger
807f839bbf
bpo-45235: Revert an argparse bugfix that caused a regression (GH-29525)
* Revert "bpo-45235: Fix argparse overrides namespace with subparser defaults (GH-28420) (GH-28443)"

This reverts commit a18d52269a.
2021-11-11 21:53:23 -06:00
andrei kulakov
6fafc25aea
bpo-24444: fix an error in argparse help when help for an option is blank (GH-28050) 2021-10-13 18:31:51 +02:00
Serhiy Storchaka
40348acc18
bpo-45229: Remove test_main in many tests (GH-28405)
Instead of explicitly enumerate test classes for run_unittest()
use the unittest ability to discover tests. This also makes these
tests discoverable and runnable with unittest.

load_tests() can be used for dynamic generating tests and adding
doctests. setUpModule(), tearDownModule() and addModuleCleanup()
can be used for running code before and after all module tests.
2021-09-19 15:27:33 +03:00
Adam Schwalm
a6e8db5e8e
bpo-45235: Fix argparse overrides namespace with subparser defaults (GH-28420) 2021-09-17 23:20:31 -05:00
Maximilian Hils
1512bc21d6
bpo-38956: don't print BooleanOptionalAction's default twice (GH-27672)
Co-authored-by: Micky Yun Chan <michan@redhat.com>
2021-08-16 23:42:21 +02:00
Jack DeVries
0ad173249d
bpo-37880: for argparse add_argument with action='store_const', const now defaults to None. (GH-26707) 2021-07-31 17:27:55 +01:00
Anthony Sottile
17575f73ce
bpo-29298: Fix crash with required subparsers without dest (GH-3680)
Automerge-Triggered-By: GH:encukou
2021-07-23 05:49:04 -07:00