Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

415
Visualizações
Python argparse: args has no attribute func

Intro

I'm trouble for a school project. I'm making a testsuit and i'm needing bot a configuration generation interface and a test runner. For that i used the library argparse and two subparsers cgi and run

The issue itself

So here is the failing code section:

def CGI(args):
    print("CGI: Work In Progress")
    exit(0)


def runTest(args):
    print("Run: Work in Progress")
    exit(0)

parser = argparse.ArgumentParser()
subparser = parser.add_subparsers()
cgi = subparser.add_parser("cgi", help="CSV Generator Interface")
run = subparser.add_parser("run", help="Test running")
verbosity = parser.add_argument_group("Verbosity").add_mutually_exclusive_group()
check = run.add_argument_group("Checks")

# Arguments
#Run parser
run.set_defaults(func=runTest)

# Run argument declaration ...

# Verbosity argument declaration ...

# Check argument declaration ...

#CGI
cgi.set_defaults(func=CGI)

args = parser.parse_args()
args.func(args) # Error is here

Whenever i run this code i have the following error:

  File "/home/thor/Projects/EPITA/TC/test/test.py", line 44, in main
    args.func(args)
AttributeError: 'Namespace' object has no attribute 'func'

Python version

$ python -V
Python 3.6.4

Argparse version

$ pip show argparse
Name: argparse
Version: 1.4.0
Summary: Python command-line parsing library
Home-page: https://github.com/ThomasWaldmann/argparse/
Author: Thomas Waldmann
Author-email: tw@waldmann-edv.de
License: Python Software Foundation License
Location: /usr/lib/python3.6/site-packages
Requires: 

EDIT

If i install argparse manually it work sudo pip install argparse. But is there any native solution. I'm not sure it will work on school's computers (we can' install packages)

EDIT 2

OK my bad i've been a total idiot i didn't rewrited my running script so i forgot to input run or cgi

Thanks for reading my message and for your future help :)

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

This is a known bug in the Python 3 version of argparse (https://bugs.python.org/issue16308). In Python 2, if the script is called with no arguments whatsoever (i.e., no subcommand), it exits cleanly with "error: too few arguments". In Python3, however, you get an unhandled AttributeError. Luckily, the workaround is pretty straightforward:

    try:
        func = args.func
    except AttributeError:
        parser.error("too few arguments")
    func(args)
over 4 years ago · Santiago Trujillo Relatório

0

parser = ArgumentParser()
parser.set_defaults(func=lambda args: parser.print_help())

imho better than try..except

over 4 years ago · Santiago Trujillo Relatório

0

Another solution could be:

if len(args.__dict__) <= 1:
    # No arguments or subcommands were given.
    parser.print_help()
    parser.exit()
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda