Commit graph

137 commits

Author SHA1 Message Date
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)
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)
306368c9c8
[3.12] bpo-44864: Do not translate user-provided strings in ArgumentParser.add_subparsers() (GH-27667) (GH-124505)
Call _() on literal strings only.
(cherry picked from commit d3c76dff44)

Co-authored-by: Jérémie Detrey <jdetrey@users.noreply.github.com>
2024-09-25 10:28:27 +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)
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)
3185a1ba11
[3.12] bpo-44865: Fix yet one missing translations in argparse (GH-27668) (GH-115974)
(cherry picked from commit 6087315926)

Co-authored-by: Jérémie Detrey <jdetrey@users.noreply.github.com>
2024-02-26 23:09:57 +00:00
Serhiy Storchaka
f0c194ad15
[3.12] gh-77956: Add the words 'default' and 'version' help text localizable (GH-12711) (GH-115967)
(cherry picked from commit da382aaf52)

Co-authored-by: paul.j3
Co-authored-by: Emmanuel Arias <eamanu@yaerobi.com>
Co-authored-by: Jérémie Detrey <jdetrey@users.noreply.github.com>
2024-02-26 21:11:27 +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
Burak Saler
01cc9c1ff7
gh-104273: Remove redundant len() calls in argparse function (#104274) 2023-05-07 19:43:50 -04: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
Jonathon Reinhart
30878a7735
argparse: Remove unused name variable when handling ArgumentTypeError (#96549)
This removes the unused `name` variable in the block where `ArgumentTypeError` is handled.

`ArgumentTypeError` errors are handled by showing just the string of the exception; unlike `ValueError`, the name (`__name__`) of the function is not included in the error message.

Fixes #96548
2022-09-05 14:32:23 -07:00
Harry
ad7340e8c5
gh-92445 Improve interaction between nargs="*" and choices() (GH-92565) 2022-08-25 06:18:38 -05:00
Inada Naoki
9877f4c624
gh-85308: argparse: Use filesystem encoding for arguments file (GH-93277) 2022-06-23 12:09:57 +09:00
DjMorgul
b885b8f4be
Allow translating argument error messages (#17169) 2022-05-05 00:32:49 -05: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
Victor Stinner
c77953b23e
Revert "gh-85567: Register a cleanup function to close files for FileType objects in argparse (#32257)" (#91771)
This reverts commit 328dbc051f.
2022-04-21 03:10:51 +02:00
achhina
328dbc051f
gh-85567: Register a cleanup function to close files for FileType objects in argparse (#32257)
* bpo-41395: Register a cleanup function to close files for FileType objects in argparse

* Added import as top level import, and renamed file as fh.
2022-04-17 22:53:37 -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
Kumar Aditya
45f5f52601
bpo-46510: update Python2-style exception handling in argparse (GH-30881) 2022-01-25 15:34:03 +00: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
Jade Lovelace
8e75c6b49b
argparse docs: prog default is the basename of argv[0] (GH-30298) 2022-01-02 12:16:25 -08: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
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