summaryrefslogtreecommitdiffstats
path: root/Lib/argparse.py
Commit message (Collapse)AuthorAgeFilesLines
* GH-127133: Remove ability to nest argument groups & mutually exclusive ↵Savannah Ostrowski2024-11-241-17/+3
| | | | groups (#127186)
* gh-86463: Fix default prog in subparsers if usage is used in the main parser ↵Serhiy Storchaka2024-11-221-1/+1
| | | | | | | | | | (GH-125891) The usage parameter of argparse.ArgumentParser no longer affects the default value of the prog parameter in subparsers. Previously the full custom usage of the main parser was used as the prog prefix in subparsers.
* gh-117941: Reject option names starting with "--no-" in ↵Serhiy Storchaka2024-11-111-0/+3
| | | | | | argparse.BooleanOptionalAction (GH-125894) They never worked correctly.
* gh-126068: Fix exceptions in the argparse module (GH-126069)Serhiy Storchaka2024-10-301-26/+22
| | | | | | | | * Only error messages for ArgumentError and ArgumentTypeError are now translated. * ArgumentError is now only used for command line errors, not for logical errors in the program. * TypeError is now raised instead of ValueError for some logical errors.
* gh-58032: Deprecate the argparse.FileType type converter (GH-124664)Serhiy Storchaka2024-10-231-5/+13
|
* gh-125355: Rewrite parse_intermixed_args() in argparse (GH-125356)Serhiy Storchaka2024-10-221-65/+34
| | | | | | | | | | | * 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().
* gh-125542: Deprecate prefix_chars in ArgumentParser.add_argument_group() ↵Savannah Ostrowski2024-10-171-0/+8
| | | | (GH-125563)
* GH-99749: Add optional feature to suggest correct names (ArgumentParser) ↵Savannah Ostrowski2024-10-171-9/+26
| | | | (GH-124456)
* gh-86357: argparse: use str() consistently and explicitly to print choices ↵rindeal2024-10-141-7/+5
| | | | | (GH-117766) Signed-off-by: Jan Chren ~rindeal <dev.rindeal@gmail.com>
* gh-85935: Check for nargs=0 for positional arguments in argparse (GH-124839)Serhiy Storchaka2024-10-121-1/+9
| | | | | | Raise ValueError in add_argument() if either explicit nargs=0 or action that does not consume arguments (like 'store_const' or 'store_true') is specified for positional argument.
* gh-125254: Fix error report about ambiguous option in argparse (GH-125273)Serhiy Storchaka2024-10-121-1/+1
| | | | | 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.
* gh-59330: Improve error message for dest= for positionals (GH-125215)Serhiy Storchaka2024-10-121-1/+2
| | | | | | | Also improve the documentation. Specify how dest and metavar are derived from add_argument() positional arguments. Co-authored-by: Simon Law <sfllaw@sfllaw.ca>
* gh-65865: Raise early errors for invalid help strings in argparse (GH-124899)Serhiy Storchaka2024-10-121-7/+24
|
* gh-61011: Fix inheritance of nested mutually exclusive groups in argparse ↵Serhiy Storchaka2024-10-111-1/+5
| | | | | | | | | (GH-125210) Previously, all nested mutually exclusive groups lost their connection to the group containing them and were displayed as belonging directly to the parser. Co-authored-by: Danica J. Sutherland <djsutherland@users.noreply.github.com>
* GH-124693: Support parsing negative scientific and complex numbers argparse ↵Savannah Ostrowski2024-10-091-1/+1
| | | | | (GH-124823) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* gh-58282: Fix support of tuple metavar for positional arguments in argparse ↵Serhiy Storchaka2024-10-021-3/+10
| | | | | | | | (GH-124782) Previously, formatting help output or error message for positional argument with a tuple metavar raised exception. Co-authored-by: Cyker Way <cykerway@gmail.com>
* gh-66436: Improved prog default value for argparse.ArgumentParser (GH-124799)Serhiy Storchaka2024-10-011-5/+23
| | | | | | | | | It can now have one of three forms: * basename(argv0) -- for simple scripts * python arv0 -- for directories, ZIP files, etc * python -m module -- for imported modules Co-authored-by: Alyssa Coghlan <ncoghlan@gmail.com>
* gh-58573: Fix conflicts between abbreviated long options in the parent ↵Serhiy Storchaka2024-09-291-34/+28
| | | | | | parser and subparsers in argparse (GH-124631) Check for ambiguous options if the option is consumed, not when it is parsed.
* gh-116850: Fix argparse for namespaces with not directly writable dict ↵Serhiy Storchaka2024-09-291-1/+2
| | | | | | (GH-124667) It now always uses setattr() instead of setting the dict item to modify the namespace. This allows to use a class as a namespace.
* gh-61181: Fix support of choices with string value in argparse (GH-124578)Serhiy Storchaka2024-09-291-5/+9
| | | Substrings of the specified string no longer considered valid values.
* gh-53834: Fix support of arguments with choices in argparse (GH-124495)Serhiy Storchaka2024-09-291-5/+1
| | | | | | Positional arguments with nargs equal to '?' or '*' no longer check default against choices. Optional arguments with nargs equal to '?' no longer check const against choices.
* gh-124345: Support abbreviated single-dash long options with = in argparse ↵Serhiy Storchaka2024-09-291-2/+4
| | | | (GH-124428)
* gh-80259: Fix conflict between type and default=SUPPRESS in argparse (GH-124519)Serhiy Storchaka2024-09-291-1/+1
| | | | | type() no longer called for SUPPRESS. This only affects positional arguments with nargs='?'.
* gh-104860: Fix allow_abbrev=False for single-dash long options (GH-124340)Serhiy Storchaka2024-09-291-1/+1
|
* bpo-44864: Do not translate user-provided strings in ↵Jérémie Detrey2024-09-241-2/+2
| | | | | | ArgumentParser.add_subparsers() (GH-27667) Call _() on literal strings only.
* gh-113008: Correct argparse usage output for required, mutually exclusive ↵Payton2024-09-241-2/+2
| | | | groups (GH-113085)
* gh-59317: Improve parsing optional positional arguments in argparse (GH-124303)Serhiy Storchaka2024-09-241-6/+7
| | | | Fix parsing positional argument with nargs equal to '?' or '*' if it is preceded by an option and another positional argument.
* gh-72795: Make positional arguments with nargs='*' or REMAINDER non-required ↵Serhiy Storchaka2024-09-241-3/+2
| | | | | | | (GH-124306) This allows to use positional argument with nargs='*' and without default in mutually exclusive group and improves error message about required arguments.
* gh-53780: Ignore the first "--" (double dash) between an option and command ↵Serhiy Storchaka2024-09-241-4/+8
| | | | in argparse (GH-124275)
* gh-63143: Fix parsing mutually exclusive arguments in argparse (GH-124307)Serhiy Storchaka2024-09-241-3/+2
| | | | | | 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".
* GH-87041: Fix incorrect indentation in argparse help (GH-124230)Savannah Ostrowski2024-09-231-4/+3
| | | | | | | | 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. Co-authored-by: Pavel Ditenbir <pavel.ditenbir@gmail.com>
* GH-124321: Fix argparse negative number parsing to capture -.5(GH-124322)Savannah Ostrowski2024-09-231-1/+1
| | | Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
* gh-81691: Fix handling of multiple "--" (double dashes) in argparse (GH-124233)Serhiy Storchaka2024-09-201-7/+5
| | | | | Only the first one has now been removed, all subsequent ones are now taken literally.
* GH-123945: Update regex for parsing negative numbers that contain ↵Savannah Ostrowski2024-09-171-1/+1
| | | | | | | | underscores (#123970) --------- Co-authored-by: Brandt Bucher <brandtbucher@gmail.com> Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
* gh-121151: argparse: Fix wrapping of long usage text of arguments inside a ↵Ali Hamdan2024-08-071-3/+12
| | | | mutually exclusive groups (GH-121159)
* gh-121018: Fix more cases of exiting in argparse when exit_on_error=False ↵Serhiy Storchaka2024-06-281-9/+13
| | | | | | | | | (GH-121056) * 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.
* gh-121018: Ensure ArgumentParser.parse_args with exit_on_error=False raises ↵blhsing2024-06-261-2/+4
| | | | instead of exiting when given unrecognized arguments (GH-121019)
* gh-118805: Remove type, choices, metavar params of `BooleanOptionalAction` ↵Nikita Sobolev2024-05-091-28/+0
| | | | | (#118806) Co-authored-by: Alex Waygood <Alex.Waygood@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>