Skip to content

GH-54732: Tweak wording around empty lines in argument files#150980

Merged
savannahostrowski merged 3 commits into
python:mainfrom
savannahostrowski:gh-54732-fromfile-empty-line-doc
Jun 9, 2026
Merged

GH-54732: Tweak wording around empty lines in argument files#150980
savannahostrowski merged 3 commits into
python:mainfrom
savannahostrowski:gh-54732-fromfile-empty-line-doc

Conversation

@savannahostrowski

@savannahostrowski savannahostrowski commented Jun 5, 2026

Copy link
Copy Markdown
Member

Follow-up to #136795.

Revises the empty-lines note to address the concerns raised in #54732 (comment), and adds a caveat to the convert_arg_line_to_args example that split-based parsing can't represent arguments containing spaces.

@savannahostrowski savannahostrowski added docs Documentation in the Doc dir needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Jun 5, 2026
@github-project-automation github-project-automation Bot moved this to Todo in Docs PRs Jun 5, 2026
@read-the-docs-community

read-the-docs-community Bot commented Jun 5, 2026

Copy link
Copy Markdown

Comment thread Doc/library/argparse.rst
Comment on lines +445 to +446
Each line is treated as a single argument, so an empty line is read as an
empty string (``''``).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to simulate the situation where empty strings are passed from a file and it doesn't return an unrecognized argument, so this note makes much more sense to me and is perfectly understandable.

I also tried to simulate the unrecognized arguments error to see when it appears and my understanding is that it's related to leftover tokens in general rather than empty strings. So, if my understanding is correct, it also makes sense that it was removed here.

So, overall, the new wording is much clearer to me.

>>> import argparse
>>> with open("args.txt", "w") as f:
...   f.write("-f\n\nbar\n")
...
8
>>> parser = argparse.ArgumentParser(fromfile_prefix_chars="@")
>>> parser.add_argument("-f")
_StoreAction(option_strings=['-f'], dest='f', nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None, deprecated=False)
>>> parser.add_argument("rest", nargs="*")
_StoreAction(option_strings=[], dest='rest', nargs='*', const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None, deprecated=False)
>>> parser._read_args_from_files(["@args.txt"])
['-f', '', 'bar']
>>> parser.parse_args(["@args.txt"])
Namespace(f='', rest=['bar'])
>>> p2 = argparse.ArgumentParser(fromfile_prefix_chars="@")
>>> p2.add_argument("-f")
_StoreAction(option_strings=['-f'], dest='f', nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None, deprecated=False)
>>> p2.parse_args(["@args.txt"])
usage: [-h] [-f F]
: error: unrecognized arguments: bar

@savannahostrowski savannahostrowski enabled auto-merge (squash) June 9, 2026 15:28
@savannahostrowski savannahostrowski merged commit 528550e into python:main Jun 9, 2026
30 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in Docs PRs Jun 9, 2026
@miss-islington-app

Copy link
Copy Markdown

Thanks @savannahostrowski for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Jun 9, 2026

Copy link
Copy Markdown

GH-151165 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jun 9, 2026
@bedevere-app

bedevere-app Bot commented Jun 9, 2026

Copy link
Copy Markdown

GH-151166 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Jun 9, 2026
@bedevere-app

bedevere-app Bot commented Jun 9, 2026

Copy link
Copy Markdown

GH-151167 is a backport of this pull request to the 3.13 branch.

savannahostrowski added a commit that referenced this pull request Jun 9, 2026
…H-150980) (#151167)

GH-54732: Tweak wording around empty lines in argument files (GH-150980)
(cherry picked from commit 528550e)

Co-authored-by: Savannah Ostrowski <savannah@python.org>
savannahostrowski added a commit that referenced this pull request Jun 9, 2026
…H-150980) (#151166)

GH-54732: Tweak wording around empty lines in argument files (GH-150980)
(cherry picked from commit 528550e)

Co-authored-by: Savannah Ostrowski <savannah@python.org>
savannahostrowski added a commit that referenced this pull request Jun 9, 2026
…H-150980) (#151165)

GH-54732: Tweak wording around empty lines in argument files (GH-150980)
(cherry picked from commit 528550e)

Co-authored-by: Savannah Ostrowski <savannah@python.org>
clin1234 pushed a commit to clin1234/cpython that referenced this pull request Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir skip news

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

argparse has problem parsing option files containing empty rows

2 participants