summaryrefslogtreecommitdiffstats
path: root/Lib/argparse.py
Commit message (Collapse)AuthorAgeFilesLines
* [3.13] GH-87041: Fix incorrect indentation in argparse help (GH-124230) ↵Miss Islington (bot)2024-09-301-4/+3
| | | | | | | | | | | | | | (#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] bpo-44864: Do not translate user-provided strings in ↵Miss Islington (bot)2024-09-301-2/+2
| | | | | | | | | | ArgumentParser.add_subparsers() (GH-27667) (#124506) bpo-44864: Do not translate user-provided strings in ArgumentParser.add_subparsers() (GH-27667) Call _() on literal strings only. (cherry picked from commit d3c76dff444046504754a437dceebc9a9c87ef18) Co-authored-by: Jérémie Detrey <jdetrey@users.noreply.github.com>
* [3.13] gh-121151: argparse: Fix wrapping of long usage text of arguments ↵Miss Islington (bot)2024-09-021-3/+12
| | | | | | | | 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-9/+13
| | | | | | | | | | 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-2/+4
| | | | | | | 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-72/+28
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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-116159: argparse: performance improvement parsing large number of options ↵Amethyst Reese2024-03-011-4/+5
| | | | | | | | | | | | | | (#116162) When parsing positional vs optional arguments, the use of min with a list comprehension inside of a loop results in quadratic time based on the number of optional arguments given. When combined with use of prefix based argument files and a large number of optional flags, this can result in extremely slow parsing behavior. This replaces the min call with a simple loop with a short circuit to break at the next optional argument. Co-authored-by: Zsolt Dollenstein <zsol.zsol@gmail.com>
* bpo-44865: Fix yet one missing translations in argparse (GH-27668)Jérémie Detrey2024-02-261-1/+2
|
* gh-77956: Add the words 'default' and 'version' help text localizable ↵Emmanuel Arias2024-02-261-2/+4
| | | | | | | (GH-12711) Co-authored-by: paul.j3 Co-authored-by: Jérémie Detrey <jdetrey@users.noreply.github.com>
* gh-96310: Fix a traceback in argparse when all options in a mutually ↵Daniel Mach2024-02-211-0/+2
| | | | | | | | | | | 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.
* argparse: remove incoherent and redundant docstring for private method ↵Frank Dana2024-02-211-8/+0
| | | | | (GH-101591) Signed-off-by: FeRD (Frank Dana) <ferdnyc@gmail.com>
* gh-60346: Improve handling single-dash options in ↵Serhiy Storchaka2024-02-191-23/+28
| | | | ArgumentParser.parse_known_args() (GH-114180)
* gh-83648: Support deprecation of options, arguments and subcommands in ↵Serhiy Storchaka2024-02-051-24/+68
| | | | argparse (GH-114086)
* gh-109475: Fix support of explicit option value "--" in argparse (GH-114814)Serhiy Storchaka2024-02-051-1/+1
| | | For example "--option=--".
* gh-101599: argparse: simplify the option help string (GH-103372)Jokimax2024-02-021-7/+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-109653: Defer importing `warnings` in several modules (#110286)Alex Waygood2023-10-041-2/+3
|
* gh-103558: Add coverage tests for argparse (#103570)Tian Gao2023-06-051-6/+6
| | | | | 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-3/+24
| | | | | | | | `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-104273: Remove redundant len() calls in argparse function (#104274)Burak Saler2023-05-071-3/+4
|
* gh-101640: Make argparse _print_message catch any write error (#101802)Oleg Iarygin2023-05-061-3/+5
| | | | | | * 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-3/+10
| | | | add_argument() were dropped (#102318)
* gh-80448: argparse: Fix IndexError on store_true action (#15656)Hai Shi2022-11-121-1/+5
| | | | | 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>
* argparse: Remove unused name variable when handling ArgumentTypeError (#96549)Jonathon Reinhart2022-09-051-1/+0
| | | | | | | | 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
* gh-92445 Improve interaction between nargs="*" and choices() (GH-92565)Harry2022-08-251-1/+3
|
* gh-85308: argparse: Use filesystem encoding for arguments file (GH-93277)Inada Naoki2022-06-231-1/+3
|
* Allow translating argument error messages (#17169)DjMorgul2022-05-051-1/+1
|
* gh-88753: Make BooleanOptionalAction's addition of default to help more ↵Toshio Kuratomi2022-05-031-5/+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/+7
| | | | | | | | | | | 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-0/+1
| | | | | | | | | # 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
* Revert "gh-85567: Register a cleanup function to close files for FileType ↵Victor Stinner2022-04-211-7/+3
| | | | | objects in argparse (#32257)" (#91771) This reverts commit 328dbc051f84bd5fdf61101bb4fa61d85f8b7feb.
* gh-85567: Register a cleanup function to close files for FileType objects in ↵achhina2022-04-181-3/+7
| | | | | | | 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.
* bpo-14156: Make argparse.FileType work correctly for binary file modes when ↵MojoVampire2022-03-061-4/+4
| | | | | | | 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-46510: update Python2-style exception handling in argparse (GH-30881)Kumar Aditya2022-01-251-6/+4
|
* bpo-46080: fix argparse help generation exception in edge case (GH-30111)Felix Fontein2022-01-201-1/+1
| | | | | Fix an uncaught exception during help text generation when argparse.BooleanOptionalAction is used with default=argparse.SUPPRESS and help is specified.
* argparse docs: prog default is the basename of argv[0] (GH-30298)Jade Lovelace2022-01-021-1/+2
|
* bpo-22047: [argparse] deprecate nested argument groups and mutually ↵Irit Katriel2021-12-161-0/+18
| | | | exclusive groups (GH-30098)
* bpo-26952: [argparse] clearer error when formatting an empty mutually… ↵Irit Katriel2021-12-151-0/+3
| | | | (GH-30099)
* bpo-45235: Revert an argparse bugfix that caused a regression (GH-29525)Raymond Hettinger2021-11-121-7/+6
| | | | | * Revert "bpo-45235: Fix argparse overrides namespace with subparser defaults (GH-28420) (GH-28443)" This reverts commit a18d52269ab6071a605d6c72f6af585a4c533ca4.
* bpo-24444: fix an error in argparse help when help for an option is blank ↵andrei kulakov2021-10-131-5/+6
| | | | (GH-28050)
* bpo-45235: Fix argparse overrides namespace with subparser defaults (GH-28420)Adam Schwalm2021-09-181-6/+7
|
* bpo-38956: don't print BooleanOptionalAction's default twice (GH-27672)Maximilian Hils2021-08-161-1/+1
| | | | Co-authored-by: Micky Yun Chan <michan@redhat.com>
* bpo-37880: for argparse add_argument with action='store_const', const now ↵Jack DeVries2021-07-311-2/+3
| | | | defaults to None. (GH-26707)
* bpo-29298: Fix crash with required subparsers without dest (GH-3680)Anthony Sottile2021-07-231-0/+2
| | | Automerge-Triggered-By: GH:encukou
* bpo-9694: Fix misleading phrase "optional arguments" (GH-23858)Raymond Hettinger2020-12-231-1/+1
|
* bpo-40862: Raise TypeError when const is given to ↵Rémi Lapeyre2020-06-051-1/+0
| | | | argparse.BooleanOptionalAction (GH-20623)
* bpo-39058: Preserve attribute order in argparse Namespace reprs. (GH-17621)Raymond Hettinger2020-05-181-1/+1
|
* bpo-39546: argparse: Honor allow_abbrev=False for specified prefix_chars ↵Kyle Meyer2020-02-181-28/+28
| | | | | | | | | | | | | | | | | | | | | | | | | | (GH-18337) When `allow_abbrev` was first added, disabling the abbreviation of long options broke the grouping of short flags ([bpo-26967](https://bugs.python.org/issue26967)). As a fix, b1e4d1b603 (contained in v3.8) ignores `allow_abbrev=False` for a given argument string if the string does _not_ start with "--" (i.e. it doesn't look like a long option). This fix, however, doesn't take into account that long options can start with alternative characters specified via `prefix_chars`, introducing a regression: `allow_abbrev=False` has no effect on long options that start with an alternative prefix character. The most minimal fix would be to replace the "starts with --" check with a "starts with two prefix_chars characters". But `_get_option_tuples` already distinguishes between long and short options, so let's instead piggyback off of that check by moving the `allow_abbrev` condition into `_get_option_tuples`. https://bugs.python.org/issue39546
* Defer import of shutil which only needed for help and usage (GH-17334)Raymond Hettinger2019-11-221-3/+3
|
* bpo-38821: Fix crash in argparse when using gettext (GH-17192)Federico Bond2019-11-201-2/+3
|
* bpo-38438: Simplify argparse "star nargs" usage. (GH-17106)Brandt Bucher2019-11-111-1/+5
|