summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_argparse.py
Commit message (Collapse)AuthorAgeFilesLines
* [3.13] gh-126413: Add translation tests for getopt and optparse (GH-126698) ↵Tomas R.2024-11-151-52/+4
| | | | | (GH-126755) (cherry picked from commit dff074d1446bab23578a6b228b0c59a17006299c)
* [3.13] gh-124295: Add translation tests for argparse (GH-124803) (GH-126046)Serhiy Storchaka2024-10-271-1/+65
| | | | | | (cherry picked from commit 0922a4ae0d2803e3a4e9f3d2ccd217364cfd700e) Co-authored-by: Tomas R. <tomas.roun8@gmail.com>
* [3.13] gh-125355: Rewrite parse_intermixed_args() in argparse (GH-125356) ↵Miss Islington (bot)2024-10-221-17/+39
| | | | | | | | | | | | | | | (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 759a54d28ffe7eac8c23917f5d3dfad8309856be) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* [3.13] gh-86357: argparse: use str() consistently and explicitly to print ↵Serhiy Storchaka2024-10-141-1/+30
| | | | | | | | choices (GH-117766) (GH-125431) (cherry picked from commit 66b3922b97388c328c9bd8df050eef11c0261fae) Signed-off-by: Jan Chren ~rindeal <dev.rindeal@gmail.com> Co-authored-by: rindeal <dev.rindeal@gmail.com>
* [3.13] gh-125254: Fix error report about ambiguous option in argparse ↵Miss Islington (bot)2024-10-121-2/+12
| | | | | | | | | (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 63cf4e914f879ee28a75c02e867baa7c6047ea2b) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* [3.13] gh-61011: Fix inheritance of nested mutually exclusive groups in ↵Miss Islington (bot)2024-10-111-0/+29
| | | | | | | | | | | | 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 18c74497681e0107d7cde53e63ea42feb38f2176) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Danica J. Sutherland <djsutherland@users.noreply.github.com>
* [3.13] gh-85935: Improve tests for invalid arguments in test_argparse ↵Serhiy Storchaka2024-10-081-35/+42
| | | | | | (GH-124891) (GH-124901) Check also specific error messages. (cherry picked from commit 2c050d4bc28bffd2990b5a0bd03fb6fc56b13656)
* [3.13] gh-58282: Fix support of tuple metavar for positional arguments in ↵Serhiy Storchaka2024-10-081-1/+97
| | | | | | | | | | 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 9b31a2d83fa7cb0fe4d75ce7cf6a2c9ea2ce0728) Co-authored-by: Cyker Way <cykerway@gmail.com>
* [3.13] gh-58573: Fix conflicts between abbreviated long options in the ↵Serhiy Storchaka2024-10-071-0/+22
| | | | | | | 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 3f27153e077d7e9448e2f081275931968b40cc74)
* [3.13] gh-72795: Make positional arguments with nargs='*' or REMAINDER ↵Miss Islington (bot)2024-10-071-5/+26
| | | | | | | | | | 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 3c83f9958c14cd62ad8951c53536f7788745b0ba) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* [3.13] gh-53780: Ignore the first "--" (double dash) between an option and ↵Miss Islington (bot)2024-10-071-0/+14
| | | | | | | command in argparse (GH-124275) (GH-125073) (cherry picked from commit c578271366176a1d1b0941897efefb6e4d6508b4) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* [3.13] gh-95468: Add more tests for "--" (double dash) in test_argparse ↵Miss Islington (bot)2024-10-071-2/+41
| | | | | | | (GH-124274) (GH-125068) (cherry picked from commit baa3550bc3a119f41cc4eaed5373f9d695208e8e) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* [3.13] gh-61181: Fix support of choices with string value in argparse ↵Miss Islington (bot)2024-10-071-5/+5
| | | | | | | | (GH-124578) (GH-124755) Substrings of the specified string no longer considered valid values. (cherry picked from commit f1a2417b9e2993e584610851ac004c8b0599b323) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* [3.13] gh-116850: Fix argparse for namespaces with not directly writable ↵Miss Islington (bot)2024-10-071-0/+12
| | | | | | | | | 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 95e92ef6c74e973ea13d15180190d0fa2af82fbf) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* [3.13] gh-124345: Support abbreviated single-dash long options with = in ↵Miss Islington (bot)2024-10-071-1/+10
| | | | | | | argparse (GH-124428) (GH-124753) (cherry picked from commit 61180446eee2aef07b042c7e8892c45afabd1499) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* [3.13] gh-80259: Fix conflict between type and default=SUPPRESS in argparse ↵Miss Islington (bot)2024-10-071-7/+13
| | | | | | | | | | (GH-124519) (GH-124751) type() no longer called for SUPPRESS. This only affects positional arguments with nargs='?'. (cherry picked from commit 9bcadf589ab6f7b9d309290de7a80156b6905d35) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* [3.13] gh-104860: Fix allow_abbrev=False for single-dash long options ↵Miss Islington (bot)2024-10-071-0/+17
| | | | | | | (GH-124340) (GH-124749) (cherry picked from commit 49e105f9488de18d3d92948232fcbd956cbe0c6e) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* [3.13] gh-59317: Improve parsing optional positional arguments in argparse ↵Miss Islington (bot)2024-10-071-36/+93
| | | | | | | | | (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 4a5e4aade420c594c5b3fe0589e9e6b444bd6ee5) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* [3.13] gh-63143: Fix parsing mutually exclusive arguments in argparse ↵Miss Islington (bot)2024-10-071-6/+115
| | | | | | | | | | (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 3094cd17b0e5ba69309c54964744c797a70aa11b) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* [3.13] gh-81691: Fix handling of multiple "--" (double dashes) in argparse ↵Miss Islington (bot)2024-10-071-1/+59
| | | | | | | | | (GH-124233) (GH-124266) Only the first one has now been removed, all subsequent ones are now taken literally. (cherry picked from commit aae126748ff3d442fdbcd07933855ffd7ae6f59c) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* [3.13] gh-124245: Fix UserWarning in test_argparse (GH-124246) (#124255)Miss Islington (bot)2024-09-301-2/+1
| | | | | | gh-124245: Fix UserWarning in test_argparse (GH-124246) (cherry picked from commit 992e8f6102e317b4967a762fbefea82f9fcf9dfb) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* [3.13] GH-87041: Fix incorrect indentation in argparse help (GH-124230) ↵Miss Islington (bot)2024-09-301-0/+40
| | | | | | | | | | | | | | (#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 7ee99217345af3010bf05b1f5241c661a5e0ea9b) Co-authored-by: Savannah Ostrowski <savannahostrowski@gmail.com> Co-authored-by: Pavel Ditenbir <pavel.ditenbir@gmail.com>
* [3.13] gh-121151: argparse: Fix wrapping of long usage text of arguments ↵Miss Islington (bot)2024-09-021-4/+22
| | | | | | | | 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 013a0929750ed2b46ae990b59d02e3db84337474) Co-authored-by: Ali Hamdan <ali.hamdan.dev@gmail.com>
* [3.13] gh-121018: Fix more cases of exiting in argparse when ↵Serhiy Storchaka2024-06-281-5/+43
| | | | | | | | | | 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 81a654a3425eaa05a51342509089533c1f623f1b)
* [3.13] gh-121018: Ensure ArgumentParser.parse_args with exit_on_error=False ↵Miss Islington (bot)2024-06-261-0/+3
| | | | | | | raises instead of exiting when given unrecognized arguments (GH-121019) (GH-121032) (cherry picked from commit 0654336dd5138aec04e3017e15ccbb90a44e053d) Co-authored-by: blhsing <blhsing@gmail.com>
* gh-62090: Simplify argparse usage formatting (GH-105039)Ali Hamdan2024-05-071-0/+134
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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
* gh-96310: Fix a traceback in argparse when all options in a mutually ↵Daniel Mach2024-02-211-0/+21
| | | | | | | | | | | 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.
* gh-60346: Improve handling single-dash options in ↵Serhiy Storchaka2024-02-191-0/+28
| | | | ArgumentParser.parse_known_args() (GH-114180)
* gh-83648: Support deprecation of options, arguments and subcommands in ↵Serhiy Storchaka2024-02-051-2/+137
| | | | argparse (GH-114086)
* gh-109475: Fix support of explicit option value "--" in argparse (GH-114814)Serhiy Storchaka2024-02-051-0/+16
| | | For example "--option=--".
* gh-101599: argparse: simplify the option help string (GH-103372)Jokimax2024-02-021-3/+3
| | | | | | | | | | | 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
* gh-103558: Add coverage tests for argparse (#103570)Tian Gao2023-06-051-1/+85
| | | | | Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Co-authored-by: hauntsaninja <hauntsaninja@gmail.com>
* gh-92248: Deprecate `type`, `choices`, `metavar` parameters of ↵Nikita Sobolev2023-05-191-0/+43
| | | | | | | | `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>
* gh-101640: Make argparse _print_message catch any write error (#101802)Oleg Iarygin2023-05-061-0/+31
| | | | | | * In particular, don't exit when trying to print to stderr = None. * Add tests Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
* gh-101979: argparse: fix a bug where parentheses in metavar argument of ↵Yeojin Kim2023-03-051-0/+22
| | | | add_argument() were dropped (#102318)
* gh-80448: argparse: Fix IndexError on store_true action (#15656)Hai Shi2022-11-121-1/+1
| | | | | 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>
* gh-92445 Improve interaction between nargs="*" and choices() (GH-92565)Harry2022-08-251-0/+7
|
* gh-94315: Check for DAC override capability (GH-94316)Christian Heimes2022-06-271-8/+5
| | | | | ``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.
* gh-85308: Add argparse tests for reading non-ASCII arguments from file ↵Serhiy Storchaka2022-06-241-12/+23
| | | | (GH-94160)
* gh-90473: Make chmod a dummy on WASI, skip chmod tests (GH-93534)Christian Heimes2022-06-061-0/+1
| | | WASI does not have the ``chmod(2)`` syscall yet.
* bpo-45046: Support context managers in unittest (GH-28045)Serhiy Storchaka2022-05-081-4/+2
| | | | | | Add methods enterContext() and enterClassContext() in TestCase. Add method enterAsyncContext() in IsolatedAsyncioTestCase. Add function enterModuleContext().
* gh-88753: Make BooleanOptionalAction's addition of default to help more ↵Toshio Kuratomi2022-05-031-11/+15
| | | | | | | | | | | | | | | | 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>
* bpo-39716: Raise on conflicting subparser names. (GH-18605)Antony Lee2022-05-011-0/+13
| | | | | | | | | | | 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
* gh-91832: Add 'required' attr to argparse.Action repr (GH-91841)Abhigyan Bose2022-04-281-2/+4
| | | | | | | | | # 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
* gh-91984: Fix trailing spaces in multiline test strings in test_argparse ↵Abhigyan Bose2022-04-281-4/+2
| | | | (GH-91986)
* bpo-14156: Make argparse.FileType work correctly for binary file modes when ↵MojoVampire2022-03-061-13/+102
| | | | | | | 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').
* bpo-46080: fix argparse help generation exception in edge case (GH-30111)Felix Fontein2022-01-201-2/+5
| | | | | Fix an uncaught exception during help text generation when argparse.BooleanOptionalAction is used with default=argparse.SUPPRESS and help is specified.
* bpo-46411: Remove unnecessary calls to sys.exc_info() in tests (GH-30638)Irit Katriel2022-01-181-6/+4
|
* bpo-22047: [argparse] deprecate nested argument groups and mutually ↵Irit Katriel2021-12-161-2/+12
| | | | exclusive groups (GH-30098)
* bpo-26952: [argparse] clearer error when formatting an empty mutually… ↵Irit Katriel2021-12-151-0/+7
| | | | (GH-30099)