我正在解析一些命令行参数,并且大多数都需要传递给方法,但不是全部。
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--dir", help = "Directory name", type = str, default = "backups")
parser.add_argument("-n", "--dbname", help = "Name of the database", type = str, default = "dmitrii")
parser.add_argument("-p", "--password", help = "Database password", type = str, default = "1123581321")
parser.add_argument("-u", "--user", help = "Database username", type = str, default = "Dmitriy")
parser.add_argument("-a", "--archive", help = "Archive backup", action="store_true")
args = parser.parse_args()
backup(**vars(args)) # the method where i need to pass most of the arguments, except archive. Now it passes all.
答案 0 :(得分:5)
创建一个没有该密钥的新词典:
new_args = dict(k, v for k, v in args.items() if k != 'archive')
或从原始字典中删除密钥:
archive_arg = args['archive'] # save for later
del args['archive'] #remove it