如何使用 argparse 为参数设置可选值? [英] How to make an optional value for argument using argparse?

查看:44
本文介绍了如何使用 argparse 为参数设置可选值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 python 脚本,我想在其中使用一个参数来处理作为输出获得的搜索结果的数量.我目前将参数命名为 --head.这是我希望它具有的功能:

  1. --head 未在命令行中传递时,我希望它默认为一个值.在这种情况下,一个相当大的,比如 80

  2. --head 没有任何值传递时,我希望它默认为另一个值.在这种情况下,一些有限的东西,比如 10

  3. --head 传递一个值时,我希望它存储传递的值.

这是一些描述问题的代码:

<预><代码>>>>导入参数解析>>>解析器 = argparse.ArgumentParser()>>>parser.add_argument('-h',' - 头',dest='大小',常量=80,默认值=10,动作="我不知道",help='只打印输出的头部')>>># OFC,最后一行将失败,因为动作是未知的,... # 但这是我希望它的工作方式... parser.parse_args(''.split())命名空间(大小=80)>>>parser.parse_args('--head'.split())命名空间(大小=10)>>>parser.parse_args('--head 15'.split())命名空间(大小=15)

我知道我可能可以为此编写自定义操作,但我首先想看看是否有任何默认行为可以做到这一点.

解决方案

在文档中多读了一点后,我找到了我需要的东西:nargs='?'.这与 store 操作一起使用,完全符合我的要求.

这是一个例子:

<预><代码>>>>导入参数解析>>>解析器 = argparse.ArgumentParser()>>>parser.add_argument('--head',dest='大小',常量=10,默认值=80,行动='商店',nargs='?',类型=整数,help='只打印输出的头部')>>>parser.parse_args(''.split())... 命名空间(大小 = 80)>>>parser.parse_args('--head'.split())...命名空间(大小=10)>>>parser.parse_args('--head 15'.split())...命名空间(大小=15)

来源:http://docs.python.org/3/library/argparse.html#nargs

I am creating a python script where I want to have an argument that manipulates how many search results you get as output. I've currently named the argument --head. This is the functionality I'd like it to have:

  1. When --head is not passed at the command line I'd like it to default to one value. In this case, a rather big one, like 80

  2. When --head is passed without any value, I'd like it to default to another value. In this case, something limited, like 10

  3. When --head is passed with a value, I'd like it to store the value it was passed.

Here is some code describing the problem:

>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('-h',
                        '--head',
                        dest='size',
                        const=80,
                        default=10,
                        action="I don't know",
                        help='Only print the head of the output')
>>> # OFC, that last line will fail because the action is uknown,
... # but here is how I'd like it to work
... parser.parse_args(''.split())
Namespace(size=80)
>>> parser.parse_args('--head'.split())
Namespace(size=10)
>>> parser.parse_args('--head 15'.split())
Namespace(size=15)

I know I probably can write a custom action for this, but I first want to see if there is any default behaviour that does this.

解决方案

After a little more reading in the documentation I found what I needed: nargs='?'. This is used with the store action, and does exactly what I want.

Here is an example:

>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--head',
                        dest='size',
                        const=10,
                        default=80,
                        action='store',
                        nargs='?',
                        type=int,
                        help='Only print the head of the output')
>>> parser.parse_args(''.split())
... Namespace(size=80)
>>> parser.parse_args('--head'.split())
... Namespace(size=10)
>>> parser.parse_args('--head 15'.split())
... Namespace(size=15)

Source: http://docs.python.org/3/library/argparse.html#nargs

这篇关于如何使用 argparse 为参数设置可选值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆