Skip to content

Date formats in help messages of argparse #89149

Closed as not planned
Closed as not planned

Description

@SengerM
mannequin
BPO 44986
Nosy @rhettinger, @Julian, @SengerM
PRs
  • bpo-44986: Fixed bug with date formats like %Y-%m-%d in help messages of argparse #27923
  • Files
  • deleteme.py
  • 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 = None
    closed_at = None
    created_at = <Date 2021-08-23.23:22:20.648>
    labels = ['type-feature', 'library', '3.11']
    title = 'Date formats in help messages of argparse'
    updated_at = <Date 2021-09-03.00:59:17.771>
    user = 'https://github.com/SengerM'

    bugs.python.org fields:

    activity = <Date 2021-09-03.00:59:17.771>
    actor = 'paul.j3'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2021-08-23.23:22:20.648>
    creator = 'mail.de.senger'
    dependencies = []
    files = ['50232']
    hgrepos = []
    issue_num = 44986
    keywords = ['patch']
    message_count = 3.0
    messages = ['400183', '400246', '400327']
    nosy_count = 5.0
    nosy_names = ['rhettinger', 'Julian', 'python-dev', 'paul.j3', 'mail.de.senger']
    pr_nums = ['27923']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue44986'
    versions = ['Python 3.11']

    Activity

    1. SengerM commented on Aug 23, 2021

      SengerMmannequin
      MannequinAuthor

      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.

    2. added
      type-crashA hard crash of the interpreter, possibly with a core dump
      3.11only security fixes
      stdlibStandard Library Python modules in the Lib/ directory
      on Aug 23, 2021
    3. added
      type-featureA feature request or enhancement
      and removed
      type-crashA hard crash of the interpreter, possibly with a core dump
      on Aug 24, 2021
    4. Julian commented on Aug 25, 2021

      Julianmannequin
      Mannequin

      This is documented already I believe:

      https://docs.python.org/3/library/argparse.html#help

      As the help string supports %-formatting, if you want a literal % to appear in the help string, you must escape it as %%.

    5. SengerM commented on Aug 26, 2021

      SengerMmannequin
      MannequinAuthor

      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.

    6. transferred this issue fromon Apr 10, 2022
    7. encukou commented on Apr 15, 2024

      @encukou
      Member

      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 :)

    8. serhiy-storchaka commented on Sep 28, 2024

      @serhiy-storchaka
      Member

      This is a duplicate of #65865.

    9. serhiy-storchaka commented on Oct 2, 2024

      @serhiy-storchaka
      Member

      #124899 adds checks for invalid help arguments to add_argument(), add_subparsers() and add_parser().

    10. moved this from Features to Doc issues in Argparse issueson Oct 12, 2024
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.11only security fixesstdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions