[英]AttributeError: 'Namespace' object has no attribute 'rmvApp'
我试图在我的脚本中添加 2 个选项,“add”和“rmv”,但是当我使用 add 选项时,我得到了这个错误:
回溯(最后一次调用):文件“/home/bsony/bin/moveVM”,第 240 行,在 elif(args.rmvApp) 中:AttributeError: 'Namespace' object 没有属性 'rmvApp'
反之亦然,当我使用 rmv 选项时,我收到此错误:
回溯(最后一次调用):文件“/home/bsony/bin/moveVM”,第 238 行,在 if (args.addApp): AttributeError: 'Namespace' object has no attribute 'addApp'
相关代码截图如下:
#Set main parser
parser = argparse.ArgumentParser(description='A scirpt to add, move or remove apps file the various apps file')
sub_parser = parser.add_subparsers(dest='command')
#Set add parser
addApp = sub_parser.add_parser('add', help='Add new app to apps file')
addApp.add_argument('-a', '--add', dest='addApp', type=str, help='The application you want to add to the apps file')
#Set remove parser
rmvApp = sub_parser.add_parser('rmv', help='Remove app from apps file')
rmvApp.add_argument('-r', '--rmv', dest='rmvApp', type=str, help='The application you want to remove from the apps file')
args = parser.parse_args()
#Dictionary to store variables making it easier to pass between functions
appDict = {
"environment": "",
"appToAdd": "",
"appToRmv": "",
"appToMove": "",
"realEnv": "",
"newHost": "",
"newVMID": "",
"oldAppLine": "",
"newAppLine": "",
"oldPRLines": []
}
appsFileList = [] #empty list to ingest apps file to
#Get evironment name for apps file
appDict["environment"] = raw_input('What environment would you like to move an app from? (dev, pt, pr): ').lower()
if (appDict["environment"] not in ["dev", "ppe", "pt", "pr"]):
print("\nYou did not enter a valid environment...exiting...")
exit()
#Set apps file location
userID = getpass.getuser() #gets linux user ID to use in appsFilePath
#check for add or remove flags and call function if flags are used
if (args.addApp):
addApp(appsFileList, appDict, appsFilePath, userID)
if(args.rmvApp):
rmvApp(appsFileList, appDict)
您需要先查看指定了哪个命令,然后才能访问特定于命令的 arguments。 所以你可以这样做:
if args.command == 'add':
print(args.addApp)
elif args.command == 'rmv':
print(args.rmvApp)
else:
print("No command specified")
只有指定命令的子解析器 arguments 才会出现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.