python - 如果一个 argparse 操作为真,则获取另一个值
问题描述
我是 python 新手并尝试下面的代码。我所需要的只是如果其中一个 argarse 值是真的,那么得到另一个值。
#! /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。非常感谢。
解决方案
我已经更新了条件语句。我想这就是你想要做的。
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))
推荐阅读
- r - 使用 data.table 移位回收向量值,而不是使用 fill=NA 填充
- vba - VBA:从 Outlook 中与 Access 交互
- c# - Identity Server 4 - IDX10630:PII 被隐藏
- python - python 正则表达式匹配文件名但得到无
- go - 即使不再维护,我还能继续使用 mgo 吗?
- linux - 如何在不使用 ls 命令的情况下获取大目录的文件列表(90GB)
- mongodb - Mongodb:其中一个字段不区分大小写的复合索引
- angular - 如何在 Angular 材料 7 上创建大型菜单
- javascript - 视频错误 - 未捕获(承诺中)DOMException:播放()请求被暂停()调用中断
- scala - Scala 到 Java 字节码工具