简体   繁体   English

如果一个argparse动作为true,则获取另一个值

[英]if one argparse action is true get another value

I am new to python and trying below code. 我是python的新手,请尝试以下代码。 All I need is if one of the argarse value is true, get another value. 我需要的是,如果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))

All I am looking is if user-count is true then code should be prompt for --user_count. 我所要查找的是user-count为true时,应提示代码--user_count。 Thanks much. 非常感谢。

I've updated the conditional statement. 我已经更新了条件语句。 I guess this is what you wanted to do. 我想这就是您想要做的。

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))

Well.. found a workaround as below. 好..找到了一种解决方法,如下所示。 But still would be great to know if there is a option as asked in original question 但是仍然很高兴知道是否存在原始问题中所要求的选项

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM