我試圖讓多個命名空間爲它在遠程服務器上執行以下.py文件:argparse回報嵌套值只有1命名空間
cmd = 'ssh [email protected]{0} 2>/dev/null "/root/abc.py {1} {2} {3} {4} {5} {6} {7} {8}"'.format(hostname, from_vm, to_vm, from_slot, to_slot, from_port, to_port, from_vm_inst, to_vm_inst)
除了主機,所有其他實體都列出。
在abc.py,我有以下代碼:
parser = argparse.ArgumentParser()
parser.add_argument(nargs='+', dest='from_vm',
help='list of name of source VMs')
print("List of items: {}".format(args.from_vm))
這將返回我以下內容:項目
列表:[ '[myvm-R 1-1,',' myvm-r1-1「,」myvm-r2-1「,」myvm-r1-1「,」myvm-r1-1「,」myvm-r2-1「,」myvm-r2-1 ,'myvm-r1-1]','myvm-r2-1','myvm-r2-1','myvm-spt','myvm-spt','myvm-r1-1' 1','myvm-spt','myvm-r2-1','myvm-spt]','[1,','1','1','1','1 ,'1','1','1','1','1','1','1','1','1','1',' 1','1]','[2','3','1','1','5','2','4','4]', '[3,','6,','1,','3,','6,','4 '實例000000bc'實例000000bc'實例000000bf'實例000000bc'實例000000bc','實例000000bc','實例000000bc','實例000000bc','實例000000bc','實例000000bf','實例000000bf','實例000000bc]','實例000000bf','實例000000bf','實例000000c0','實例000000c0','實例000000bc,', '實例000000c0,', '實例000000bf,', '實例000000c0]']
只有1與此相關聯的名稱空間:
命名空間( from_vm = ['[myvm-r1-1','myvm-r1-1','myvm-r2-1','myvm-r1-1','myvm-r1-1',' myvm-r2-1','myvm-r2-1','myvm-r1-1]','[myvm-r2-1','myvm-r2-1','myvm-spt, ','myvm-spt','myvm-r1-1','myvm-spt','myvm-r 2-1,','myvm-spt]','[1,','1','1','1','1','1','1',' 1]','1','1','1','1','1','1','1','1]','[2' ,'3','1','1','5','2','4','4]','[3','6','1, '','3','6','4','5','2]','[instance-000000bc','instance-000000bc','instance-000000bf','實例000000bc','實例000000bc','實例000000bf','實例000000bf','實例000000bc]','[實例000000bf','實例000000bf','實例000000c0,', '實例000000c0,', '實例000000bc,', '實例000000c0,', '實例000000bf,', '實例000000c0]'])
這似乎對我來說是一個嵌套列表。我想爲這個嵌套列表中的每個列表都有不同的名稱空間,但沒有成功。我嘗試使用單獨的dest = xyz單獨的parser.add_argument,但沒有成功。有人可以幫忙嗎?我的要求是爲每個列表都有不同的名稱空間。
DId你從代碼示例中省略了'args = parser.parse_args()'命令? – hpaulj
@hpaulj,我在我的代碼中,它只是我沒有在上面的例子中提到它。 –