简体   繁体   中英

if one argparse action is true get another value

I am new to python and trying below code. All I need is if one of the argarse value is true, get another value.

#! /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. 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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