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

Python 如果一个argparse操作为true,则获取另一个值,python,argparse,Python,Argparse,我是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 Na

我是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\u count。多谢

我已经更新了条件语句。我想这就是你想做的

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

嗯。。找到如下所示的解决方法。但若能知道有并没有像原问题中所问的那个样的选择,那个还是很好的

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

如果你要对不同的行为有完全不相关的行为,你可能想要;这让你有一个初始参数来决定应用哪一组特性,主解析器识别公共参数,每个子解析器能够实现它特有的附加参数。您可能希望发布一个包含预期输出的脚本,以获得更好的答案。
argparse
在调用脚本时查看命令行中提供的字符串。这不是一个交互式输入。您可以使用
输入
,但这不是使用命令行解析的常用方法。通常我们希望用户在一开始就提供所有需要的信息。如果他们不这样做,我们就会出错。