Python 2.7 Argparse 是或否输入
Python 2.7 Argparse Yes or No input
我正在尝试使用 argparse 创建一个实例,我在其中键入我的 Unix 控制台:
python getFood.py --food <(echo Bread) --calories yes
我已经实现了食物选项,并想使用 argparse 添加卡路里是或否选项(二进制输入),这将决定是否从我导入的 class 调用卡路里方法。
我目前的代码主程序是:
parser = argparse.ArgumentParser(description='Get food details.')
parser.add_argument('--food', help='name of food to lookup', required=True, type=file)
args = parser.parse_args()
这成功地允许我使用上面显示的第一个食物选项,returns 食物细节。
基本上我想添加第二个二元选项,如果用户指示为真,将调用一个额外的方法。对于如何编辑我的主例程 argparse 参数有什么帮助吗?我对 argparse 还是很陌生。
您可以简单地添加一个带有 action='store_true'
的参数,如果不包含 --calories
,它将默认 args.calories
为 False。进一步说明,如果用户添加 --calories
,args.calories
将设置为 True
。
parser = argparse.ArgumentParser(description='Get food details.')
# adding the `--food` argument
parser.add_argument('--food', help='name of food to lookup', required=True, type=file)
# adding the `--calories` argument
parser.add_argument('--calories', action='store_true', dest='calories', help='...')
# note: `dest` is where the result of the argument will go.
# as in, if `dest=foo`, then `--calories` would set `args.foo = True`.
# in this case, it's redundant, but it's worth mentioning.
args = parser.parse_args()
if args.calories:
# if the user specified `--calories`,
# call the `calories()` method
calories()
else:
do_whatever()
但是,如果您想专门检查 yes
或 no
,请将 store_true
替换为
parser.add_argument('--calories', action='store_true', dest='calories', help='...')
和store
,如下图
parser.add_argument('--calories', action='store', dest='calories', type='str', help='...')
这将允许您稍后检查
if args.calories == 'yes':
calories()
else:
do_whatever()
请注意,在本例中我添加了 type=str
,它将参数解析为字符串。由于您指定的选择是 yes
或 no
,argparse
实际上允许我们使用 choices
:
进一步指定可能输入的域
parser.add_argument('--calories', action='store', dest='calories', type='str',
choices=['yes', 'no'], help='...')
现在,如果用户在 ['yes', 'no']
中输入任何 而不是 的内容,将会引发错误。
最后一种可能性是添加 default
,这样用户就不必一直指定某些标志:
parser.add_argument('--calories', action='store', dest='calories', type='str',
choices=['yes', 'no'], default='no', help='...')
编辑:正如@ShadowRanger 在评论中指出的,在这种情况下,dest='calories'
、action='store'
和 type='str'
是默认值,因此您可以省略它们:
parser.add_argument('--calories', choices=['yes', 'no'], default='no', help='...')
我正在尝试使用 argparse 创建一个实例,我在其中键入我的 Unix 控制台:
python getFood.py --food <(echo Bread) --calories yes
我已经实现了食物选项,并想使用 argparse 添加卡路里是或否选项(二进制输入),这将决定是否从我导入的 class 调用卡路里方法。
我目前的代码主程序是:
parser = argparse.ArgumentParser(description='Get food details.')
parser.add_argument('--food', help='name of food to lookup', required=True, type=file)
args = parser.parse_args()
这成功地允许我使用上面显示的第一个食物选项,returns 食物细节。
基本上我想添加第二个二元选项,如果用户指示为真,将调用一个额外的方法。对于如何编辑我的主例程 argparse 参数有什么帮助吗?我对 argparse 还是很陌生。
您可以简单地添加一个带有 action='store_true'
的参数,如果不包含 --calories
,它将默认 args.calories
为 False。进一步说明,如果用户添加 --calories
,args.calories
将设置为 True
。
parser = argparse.ArgumentParser(description='Get food details.')
# adding the `--food` argument
parser.add_argument('--food', help='name of food to lookup', required=True, type=file)
# adding the `--calories` argument
parser.add_argument('--calories', action='store_true', dest='calories', help='...')
# note: `dest` is where the result of the argument will go.
# as in, if `dest=foo`, then `--calories` would set `args.foo = True`.
# in this case, it's redundant, but it's worth mentioning.
args = parser.parse_args()
if args.calories:
# if the user specified `--calories`,
# call the `calories()` method
calories()
else:
do_whatever()
但是,如果您想专门检查 yes
或 no
,请将 store_true
替换为
parser.add_argument('--calories', action='store_true', dest='calories', help='...')
和store
,如下图
parser.add_argument('--calories', action='store', dest='calories', type='str', help='...')
这将允许您稍后检查
if args.calories == 'yes':
calories()
else:
do_whatever()
请注意,在本例中我添加了 type=str
,它将参数解析为字符串。由于您指定的选择是 yes
或 no
,argparse
实际上允许我们使用 choices
:
parser.add_argument('--calories', action='store', dest='calories', type='str',
choices=['yes', 'no'], help='...')
现在,如果用户在 ['yes', 'no']
中输入任何 而不是 的内容,将会引发错误。
最后一种可能性是添加 default
,这样用户就不必一直指定某些标志:
parser.add_argument('--calories', action='store', dest='calories', type='str',
choices=['yes', 'no'], default='no', help='...')
编辑:正如@ShadowRanger 在评论中指出的,在这种情况下,dest='calories'
、action='store'
和 type='str'
是默认值,因此您可以省略它们:
parser.add_argument('--calories', choices=['yes', 'no'], default='no', help='...')