python / python/cpython

Confusing error message when options for `python` are erroneously quoted

Open
#132,414 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:
#!/bin/python -q -i

I was writing an unrelated post about shebang lines tricks+tips, and happened to pick python as an example, only to discover some weird behavior. The above gets parsed as /bin/python "-q -i" <path/to/script>

But the message python prints is quite confusing:

Unknown option: - 
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Try `python -h' for more information.

What's happening is that the "unknown option" is a single space, being parsed as a short-form option, and printed. But the output is pretty opaque, since the space is not quoted, and it's just a trailing invisible space.

Aside: Several other invalid one-byte options like \n get printed wrongly. This doesn't seem worth fixing.


Proposed fix:

I think the shebang case happens to users often enough that it would be good to have some kind of better error message if a short-form options list contains either of

  • - (this would require scanning ahead!), which either indicates some quoting problem, or an incorrect long-form option (-check-hash-based-pycs rather than --check-hash-based-pycs).

I think a good error message could be unknown option: -q -i or unknown option: "-q -i". Quoting the argument when it contains a space seems clearer to me.

The current long-form options have pretty clear error messages either way, mostly by happenstance.

Currently, -help-* parses as -h which then prints help, stopping option parsing. I have no strong opinion about whether that behavior is better or worse than printing an error message.

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the reported shebang invocation /bin/python -q -i and inspect CPython's command-line option parsing and error-reporting entry points. Focus on how a quoted options string becomes a short-form option, then verify that the resulting diagnostic visibly identifies the invalid argument without changing unrelated invalid one-byte option behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.