Jump to content

Python AttriuteError: 'Namespace' object has no attribute 'parser_list'

parser = argparse.ArgumentParser(description='Some commands :D.')
mainsubparsers = parser.add_subparsers()

###

# Arg (sub)list <sub>
parser_list = mainsubparsers.add_parser('list', help='List different things, see list -h')
subparsers = parser_list.add_subparsers()

###

args = parser.parse_args()
if __name__ == '__main__':
    if args.parser_list:
      # Do stuff
py script.py list
Traceback (most recent call last):
  File "script.py", line 178, in <module>
    elif args.parser_list:
AttributeError: 'Namespace' object has no attribute 'parser_list'

 

What am I doing wrong?

 

EDIT:

I'm trying to add commands like this

 

list

  • players
  • invites

show

  • player <id> (then some optional arguments)

where list has sub commands and the sub commands got arguments, etc show player <id>

Back-end developer, electronics "hacker"

Link to post
Share on other sites

3 minutes ago, geo3 said:

looks like parser_list  is not part of the args object.  

Try: 


if parser_list:
    #do stuff

 

Does not work.

 

I solved this (I think) with adding dest="somehere" and it would be available at args.somewhere

 

Back-end developer, electronics "hacker"

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×