1

I've google'd quite a bit, and read the argparse documentation that I think suggests using something with vars(). I get the Namespace violation as expected, I just cant figure out the path around this issue.

Essentially, I would like to take an argparse multi-value argument and create a list from those values so I can run a for-loop through them. This is to interface with our VNX array to reset the data snapshot on all the Developer environments.

When I run the command I can see the argparse is getting the values correctly, but its throwing the Namespace exception and not actually using the values for the argument.

Much appreciation for any guidance, even a link to some better docs that will explain my problem better. I know the issue, and how I want to fix it, I'm just not sure what to even read(or google) to get around this syntax-wise?

This is what I get when i run the code:

[[email protected] tmp]# ./envrestore.py -e dev1 dev2 dev3

Namespace(myenv=['dev1', 'dev2', 'dev3'])

Traceback (most recent call last): File "./envrestore.py", line 43, in run_create_snap() File "./envrestore.py", line 36, in run_create_snap for e in myenv: TypeError: 'Namespace' object is not iterable

[[email protected] tmp]#

#!/usr/bin/env python

import pexpect, sys, datetime, argparse, time
from fabric.api import *

parser = argparse.ArgumentParser()
parser.add_argument('-e', '--myenv', nargs='*', type=str)
print parser.parse_args()

array = "vnx.lipsum.com"
seckey = "/opt/Navisphere/blah"
myenv = parser.parse_args()
dbhosts = ['mongo01', 'mysql01']

# !! DO NOT CHANGE IDs !!
lunpnum = "0000000"
mongo_plunid = "3"
mysql_plunid = "4"

def delete_snap(env=myenv, host=dbhosts):
    child = pexpect.spawn('naviseccli -secfilepath %s -h %s snap -destroy -id %s-%s-snap' % (seckey, array, host, env))
    print child
    child.logfile = sys.stdout
    child.expect('Are you sure you want to perform this operation\?\(y\/n\):')
    child.sendline('n')

def create_snap(env=myenv, host=dbhosts, lunid=''):
    print "naviseccli -secfilepath %s -h %s snap -create -res %s -name %s-%s-snap -allowReadWrite yes" % (seckey, array, lunid, host, env)

def run_delete_snap():
    for e in myenv:
      for h in dbhosts:
        delete_snap(env=e, host=h)

def run_create_snap():
    for e in myenv:
      for h in dbhosts:
        if "mysql" in h:
          create_snap(env=e, host=h, lunid=mysql_plunid)
        elif "mongo" in h:
          create_snap(env=e, host=h, lunid=mongo_plunid)

run_create_snap()

2 Answers 2

1

I believe the problem is in what you are passing as myenv:

myenv = parser.parse_args()

I think you mean

myenv = parser.parse_args().myenv

Cheers!

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

1 Comment

Thanks very much , that did the trick. [[email protected] tmp]# ./envrestore.py -e dev1 naviseccli -secfilepath /opt/Navisphere/blah -h vnx.lipsum.com snap -create -res 3 -name mongo01-dev1-snap -allowReadWrite yes naviseccli -secfilepath /opt/Navisphere/blah -h vnx.lipsum.com snap -create -res 4 -name mysql01-dev1-snap -allowReadWrite yes
1

myenv is the argparse.Namespace instance itself. To get the values in the option named myenv, use myenv.myenv.

for e in myenv.myenv:
    print(e)

Or, to make the code clearer, name the Namespace something else:

args = parser.parse_args()
for e in args.myenv:
   ...

1 Comment

Thanks so much, always something so simple - and without your help i don't think I would have figured that out. Your method works perfectly. However, I went with Jblasco's below since it allows me to keep the for loop cleaner. Thanks again so much!

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.