【发布时间】:2020-10-01 21:26:10
【问题描述】:
我正在尝试向我的脚本添加 2 个选项,“add”和“rmv”,但是当我使用 add 选项时,我收到了这个错误:
Traceback(最近一次调用最后一次): 文件“/home/bsony/bin/moveVM”,第 240 行,在 elif(args.rmvApp): AttributeError:“命名空间”对象没有属性“rmvApp”
反之亦然,当我使用 rmv 选项时,我会收到此错误:
Traceback(最近一次调用最后一次): 文件“/home/bsony/bin/moveVM”,第 238 行,在 如果(args.addApp): AttributeError:“命名空间”对象没有属性“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)
【问题讨论】:
标签: python namespaces argparse