14

When I run this code I get

AttributeError: 'ArgumentParser' object has no attribute 'max_seed'

Here's the code

import argparse
import ConfigParser

CFG_FILE='/my.cfg'

# Get command line arguments
args = argparse.ArgumentParser()
args.add_argument('verb', choices=['new'])
args.add_argument('--max_seed', type=int, default=1000)
args.add_argument('--cmdline')
args.parse_args()

if args.max_seed:
    pass

if args.cmdline:
    pass

My source file is called "fuzz.py"

2 Answers 2

15

You should first initialize the parser and arguments and only then get the actual arguments from parse_args() (see example from the docs):

import argparse
import ConfigParser

CFG_FILE='/my.cfg'

# Get command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('verb', choices=['new'])
parser.add_argument('--max_seed', type=int, default=1000)
parser.add_argument('--cmdline')

args = parser.parse_args()
if args.max_seed:
    pass

if args.cmdline:
    pass

Hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

1

If you use argparse parsed arguments inside another class (somewhere you do self.args = parser.parse_args() ), you might need to explicitly tell your lint parser to ignore Namespace type checking. As told by @frans at Avoid Pylint warning E1101: 'Instance of .. has no .. member' for class with dynamic attributes :

Just to provide the answer that works for me now - as [The Compiler][1] suggested you can add a rule for the problematic class in your projects .pylintrc:

[TYPECHECK]
ignored-classes=Namespace

[1]: https://stackoverflow.com/users/2085149/the-compiler

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.