Using parameter 'action' in function 'argparse.ArgumentParser.add_subparsers'

Aberta
#92,811 17 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
3/5
Tempo estimado
1-2 dias
Facilidade para iniciantes
38/100
Tipo de issue
Bug
Clareza
Razoavelmente clara
Status de atividade
Estagnada
Stack de tecnologia
python
Domínio
cli

Direção de pesquisa

Reproduza o exemplo em ArgParserTest.py no Python 3.10.4 e, em seguida, inspecione argparse.py em torno da linha 1798 e a documentação dos subcomandos de argparse. Determine se action é compatível com add_subparsers e torne o comportamento resultante, a mensagem de erro ou a documentação consistentes; a conclusão deve incluir a cobertura do exemplo relatado.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

stdlib type-bug

Bug report
The function argparse.ArgumentParser.add_subparsers throws an error regarding parser_class, when passing the parameter action.

Sample-code "ArgParserTest.py":

from argparse import ArgumentParser

parser = ArgumentParser(prog='Testcase for ArgumentParser._subparsers')
# init subparsers for parser
subparsers = parser.add_subparsers(action='count') # Any action works to reproduce.
# Add my_subparser with any "action"
my_subparser = subparsers.add_parser(name='my_subparser')

Terminal output:

> python ArgParserTest.py
Traceback (most recent call last):
  File "REDACTED\ArgParserTest.py", line 5, in <module>
    subparsers = parser.add_subparsers(dest='subcommands', action='count', help='Available subcommands. Run \'<subcommand> -h\' to display further information.')
  File "REDACTED\Python3.10.4\lib\argparse.py", line 1798, in add_subparsers
    action = parsers_class(option_strings=[], **kwargs)
TypeError: _CountAction.__init__() got an unexpected keyword argument 'parser_class'

Possible resolutions

  1. If there is no valid action to subparsers:
    a. Have the error-message complain about the argument action instead of parser_class.
    b. Remove the argument action from the parameter list to the add_subparser-function.
    c. Update the documentation accordingly.
  2. If action can be a valid argument, fix either the error-message or whatever error did occur.

Environment

  • Tested on: Python 3.10.4
  • Operating system: Win10 x64 (Version 21H2, Build 19044.1706)
Linguagem predominante
Python
Estrelas
77.2k
Forks
36k
Merge médio
1d 9h
PRs com merge (30d)
558

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de python/cpython

Todas as issues de python/cpython

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.