我是python的新手,正在尝试以下代码。我需要的是,如果argarse值之一为true,则获取另一个值。
#! /home/y/bin/python3
import argparse
__author__ = "Yogesh"
parser = argparse.ArgumentParser(description='This is demo script')
parser.add_argument('-s','--source_host', help='Source Host Name',required=True)
parser.add_argument('-d','--dest_host',help='Destination Host Name',required=True)
parser.add_argument('-n','--user_count',help="No of users to migrate",required=False)
parser.add_argument('--action', choices=['one-week', 'two-week','user-count','all-users'], default='all-users')
args = parser.parse_args()
print("Source Host:{0}".format(args.source_host))
print("Dest Host:{0}".format(args.dest_host))
if args.action == 'one-week':
print("Migrate one week active users".format(args.action))
elif args.action == 'two-week':
print("Migrate two week active users".format(args.action))
elif args.action == 'user-count':
print("Mingrate user_count".format(args.action))
else:
print("Migrate all users ".format(args.action))
我所要查找的是如果user-count为true,则应提示代码--user_count。非常感谢。
答案 0 :(得分:0)
我已经更新了条件语句。我想这就是您想要做的。
if args.action == 'one-week':
print("Migrate one week active users".format(args.action))
elif args.action == 'two-week':
print("Migrate two week active users".format(args.action))
elif args.action == 'user-count':
user_count = input('Enter user count: ')
print("Mingrate {user_count} users".format(user_count=user_count))
else:
print("Migrate all users ".format(args.action))
答案 1 :(得分:0)
好..找到了一种解决方法,如下所示。但仍然很高兴知道是否存在原始问题中所要求的选项
parser = argparse.ArgumentParser(description='This is demo script')
parser.add_argument('-s','--source_host', help='Source Host Name',required=True)
parser.add_argument('-d','--dest_host',help='Destination Host Name',required=True)
parser.add_argument('-n','--user_count',help="No of users to migrate",required=False)
parser.add_argument('--action', choices=['one-week', 'two-week','user-count','all-users'], default='all-users')
args = parser.parse_args()
print("Source Host:{0}".format(args.source_host))
print("Dest Host:{0}".format(args.dest_host))
if args.action == 'one-week':
print("Migrate one week active users".format(args.action))
elif args.action == 'two-week':
print("Migrate two week active users".format(args.action))
elif args.action == 'user-count':
if args.user_count:
print("Mingrate user_count".format(args.action))
print("No of users:-{0}".format(args.user_count))
else:
print("Provide no of users you want to migrate with -n option")
else:
print("Migrate all users ".format(args.action))