python / python/cpython

Argparse: add method for retrieving the parsers of actions

オープン
#144,932 コメント 2 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

stdlib type-feature
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

Feature or enhancement

Proposal:

I have a CLI interface with an argparse.ArgumentParser composed of a bunch of different actions. I was trying to validate if an argument had been set to a default value, and with just the main parser available, I was having some issues using get_default() working from the main parser, rather than the parsers for the various actions.

I ended up going with the following comparison, in order to reduce the amount of refactoring I would need to do, where args is the var holding the object returned from parse_args():

        args.my_param == parser._actions[1].choices[args.action]].get_default(args.my_param)

I could also have solved this by adding a wrapper class holding all the subparsers in an object of some kind.

But - I think it would be nice to have a public method for accessing these action parsers, mostly for enabling this kind of use case.

Has this already been discussed elsewhere?

Not that I could find

Links to previous discussion of this feature:

No response

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

argparse.ArgumentParser、パーサーの _actions コレクション、subparser choices、get_default() から始めます。action parser を取得する public method の意図された範囲と動作を明確にし、説明されているデフォルト値の検証ユースケースをどのようにサポートするべきかも含めて検討します。API とその動作が明確に定義され、適切なテストで feature がカバーされれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
cli
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。