You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2021-08-23.23:22:20.648>labels= ['type-feature', 'library', '3.11']
title='Date formats in help messages of argparse'updated_at=<Date2021-09-03.00:59:17.771>user='https://github.com/SengerM'
If the help message of an argument in argparse contains a date format, e.g. %Y-%m-%d, it crashes when printing the help after being invoked with the -h option. Uploaded an example.
As the help string supports %-formatting, if you want a literal % to appear in the help string, you must escape it as %%.
SengerM commented on Aug 26, 2021
SengerMmannequin
MannequinAuthor
More actions
With this change there should be no need to escape anything, and it is still compatible with the old formatting. Plus, with the current implementation if you forget to escape something like %Y the error message is not helpful at all and only happens when invoked with -h option.
The proposed change is not backwards-compatible. You can currently do something like this:
import argparse
parser = argparse.ArgumentParser(prog='foo')
parser.add_argument('--arg', help='Some argument to %(prog)r (note the quotes)')
parser.add_argument('--debug', help='For the record, the available params are %s')
print(parser.format_help())
(These particular examples may not be useful, but we can't simply assume that no one found anything useful along these lines.)
Rather than guess what the programmer meant, it would be better to tell them more clearly what is wrong: catch the (TypeError, ValueError) and add a note saying Hint: escape % as %% in help text.
I'll add that to my TODO list :)
Activity
SengerM commentedon Aug 23, 2021
If the help message of an argument in argparse contains a date format, e.g. %Y-%m-%d, it crashes when printing the help after being invoked with the -h option. Uploaded an example.
Julian commentedon Aug 25, 2021
This is documented already I believe:
https://docs.python.org/3/library/argparse.html#help
SengerM commentedon Aug 26, 2021
With this change there should be no need to escape anything, and it is still compatible with the old formatting. Plus, with the current implementation if you forget to escape something like %Y the error message is not helpful at all and only happens when invoked with -h option.
encukou commentedon Apr 15, 2024
The proposed change is not backwards-compatible. You can currently do something like this:
(These particular examples may not be useful, but we can't simply assume that no one found anything useful along these lines.)
Rather than guess what the programmer meant, it would be better to tell them more clearly what is wrong: catch the
(TypeError, ValueError)and add a note sayingHint: escape % as %% in help text.I'll add that to my TODO list :)
serhiy-storchaka commentedon Sep 28, 2024
This is a duplicate of #65865.
serhiy-storchaka commentedon Oct 2, 2024
#124899 adds checks for invalid help arguments to
add_argument(),add_subparsers()andadd_parser().