Repository navigation
argparse: Allow nargs=0 for positional arguments with custom actions #157922
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Sep 21, 2026 - addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Sep 21, 2026 Looks like not quite clean solution imo to use
SomeActionlike yours just for pretty printing.Maybe this bug can be transfer to feature by extend help arg like this
parser.add_argument( dest="target", metavar="<DISK|PARTITION|IMAGE>", nargs=1, help=( "Path to disk device", "Path to parititon device", "Path to image file", ), )
Here help it sequence where help[0] is for "DISK", help[1] for "PARTITION" etc.
But I guess it needs approve @savannahostrowski @serhiy-storchaka
- addedtype-featureA feature request or enhancementA feature request or enhancementand removedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Sep 22, 2026 This is not a bug IMO and possibly a feature but I would say it is too niche.
Reacted by Kliment Lamonov and M. KocherIt is also better to describe the parameter in the same paragraph. I do not think the stdlib should have this extension (this is not what most common CLIs would do).
A custom formatter class might help. Alternatively, you may subclass
argparse.ArgumentParserand override relevant methods (print_help?) directly.I guess issue can be closed as not planned? @picnixz
I want to hear it from Savannah or Serhiy. I can see some reason to allow it (where a positional argument becomes an optional value and where all other options are specified by --) but I can also see it as a niche. Using it to document multiple arguments is however hacky IMO
Reacted by Kliment LamonovI don’t think we should re-enable
nargs=0positionals to create help-only entries; that’s a hacky use of parser actions. The layout you want can be produced with the built-inRawDescriptionHelpFormatterand a formatted description.Reacted by Kliment Lamonov- addedpendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is provided
on Sep 25, 2026
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDoc issues
Bug report
Bug description:
Python 3.14 raises an error when
nargs=0is given for a positional argument, as implemented in #124839 (gh-85935: Check for nargs=0 for positional arguments in argparse). Before it became an error, I could use such a construction with a custom action (for other reasons) to extend the help message for a multi-purpose positional argument like<a|b|c>. A small reproducer for what I mean:Running this with
python3.13shows the following help message, which I like better than one entry trying to explain three different things:Coincidentally, the custom action was always being run, which could be useful for validating or post-processing the namespace as part of the
parse_args()call, although I expect it will not be that important to you since those can usually be handled afterwards anyway:I don't see how these can be achieved with optional arguments. For the help message, I think I might have to write a custom help formatter (bad idea), which might not even be enough. Advice welcome. Anyway, I'm rather late reporting this, but I wanted to mention this use case at least as a side note.
CPython versions tested on:
3.14
Operating systems tested on:
Linux