python / python/cpython

argparse: long options with mixed prefix characters

未關閉
#138,701 1 則留言 0 個 reaction 已指派 1 人 在 GitHub 檢視

@serhiy-storchaka 已經在處理了。

開始於 2025年9月9日。

stdlib triaged type-bug type-feature
主要語言
Python
星號
77.2k
分支
35.9k
PR 合併指標
PR 指標待擷取

描述

argparse supports alternate prefix characters. I believe that this is to support on/off switches like -x/+x and Windows-style options like /h (for complete emulation of the Windows command syntax we need support of the : separator and combining options like in dir /s/w/o/p/a:-d, but this is another story). I never seen even double alternate prefix character, like ++foo or //foo in the wild, but there is no issue with supporting this.

The problem is that this allows specifying options that use two different prefix character, like -+foo or +-foo. It is a problem because you cannot distinguish a sigle-dash option from a double-dash option without knowing prefix._chars. And its value is not always available (for example in the Action constructor). BooleanOptionalAction can recognize ++foo and +foo, but it is confused by +^foo (see #138692).

There are no existing tests for mixed alternate prefix characters. I suppose this is not an intentionally added feature, but just an implementation artifact. I think that we should revisit code and only interpret option as a "double-dash long option" if it starts with the same prefix character doubled. This may have no effect on common users, since -+foo can still be interpreted as a "single-dash long option" (with name "+foo"). We should ensure that single-dash long options are not second-class citizens (for example see #138697).

I'm not sure how to classify this issue or what action to take. This requires further research and experimentation. I'm leaving this as a reminder that something needs to be done.

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。