python argparse: use arguments as values for a list -
i've google'd quite bit, , read argparse documentation think suggests using vars(). namespace violation expected, cant figure out path around issue.
essentially, take argparse multi-value argument , create list values can run for-loop through them. interface our vnx array reset data snapshot on developer environments.
when run command can see argparse getting values correctly, throwing namespace exception , not using values argument.
much appreciation guidance, link better docs explain problem better. know issue, , how want fix it, i'm not sure read(or google) around syntax-wise?
this when run code:
[root@robot.lipsum.com 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 e in myenv: typeerror: 'namespace' object not iterable
[root@robot.lipsum.com tmp]#
#!/usr/bin/env python import pexpect, sys, datetime, argparse, time 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'] # !! 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 sure want perform 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(): e in myenv: h in dbhosts: delete_snap(env=e, host=h) def run_create_snap(): e in myenv: 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()
i believe problem in passing myenv:
myenv = parser.parse_args()
i think mean
myenv = parser.parse_args().myenv
cheers!
Comments
Post a Comment