argparse: document exceptions, subclass from SystemExit and ArgparseError
Personne n'a encore pris cette issue.
- Langage dominant
- Python
- Étoiles
- 77.2k
- Forks
- 36k
- Métriques de merge des PR
- Métriques de PR en attente
Description
There are many problems with exit status people encounter using argparse (and some of them are open, e.g. https://github.com/python/cpython/issues/85427).
It is reasonable and consistent with other code that it raises SystemExit (in case of unrecognized arguments).
However, this is not flexible and requires pretty much work from users if they want to catch this error, and not exit (there are many alternatives (which in itself contradicts the Zen of Python), the default proposes to subclass the ArgumentParser).
Why can't argparse just subclass SystemExit? This is usually a good guideline for a module to create its own exception classes.
Also, all three argparse exceptions could derive from one argparse exception class (to make their interception easier) (in the title I suggested ArgparseError, but a better name might be found). However, this is not so crucial as this SystemExit: I run a big function in one place of which there is parse_args; I don't want to attribute all SystemExit-s to argparse when I catch exceptions from that function (for now I had to create my own subclass of SystemExit raised from the line with parse_args).
And (should it be a separate feature request?) could there be a subsection dedicated to exceptions in the argparse module documentation? I had to read Python help to understand the initialisation of ArgumentError.
If argparse could guarantee the types of exceptions that it can raise, that would be ideal.
Originally posted by @ericvsmith in https://github.com/python/cpython/issues/83514#issuecomment-1093850932
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par les points d’entrée de argparse mentionnés dans l’issue : ArgumentParser, parse_args et ArgumentError, puis examinez la section sur les méthodes de sortie de la documentation de argparse ainsi que la sortie d’aide de Python. Déterminez la hiérarchie d’exceptions prévue, les garanties concernant les types d’exceptions levées et la portée d’une sous-section dédiée aux exceptions avant de proposer des modifications et des mises à jour de la documentation.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- cli, documentation
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 25/100