在 Python 中,使用 argparse,只允许正整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14117415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:31:52  来源:igfitidea点击:

In Python, using argparse, allow only positive integers

pythonargparse

提问by jgritty

The title pretty much summarizes what I'd like to have happen.

标题几乎总结了我希望发生的事情。

Here is what I have, and while the program doesn't blow up on a nonpositive integer, I want the user to be informed that a nonpositive integer is basically nonsense.

这是我所拥有的,虽然程序不会在非正整数上爆炸,但我希望用户被告知非正整数基本上是无稽之谈。

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-g", "--games", type=int, default=162,
                    help="The number of games to simulate")
args = parser.parse_args()

And the output:

和输出:

python simulate_many.py -g 20
Setting up...
Playing games...
....................

Output with a negative:

输出为负:

python simulate_many.py -g -2
Setting up...
Playing games...

Now, obviously I could just add an if to determine if args.gamesis negative, but I was curious if there was a way to trap it at the argparselevel, so as to take advantage of the automatic usage printing.

现在,显然我可以添加一个 if 来确定if args.games是否为负,但我很好奇是否有办法将其捕获在argparse级别上,以便利用自动使用打印。

Ideally, it would print something similar to this:

理想情况下,它会打印类似于以下内容的内容:

python simulate_many.py -g a
usage: simulate_many.py [-h] [-g GAMES] [-d] [-l LEAGUE]
simulate_many.py: error: argument -g/--games: invalid int value: 'a'

Like so:

像这样:

python simulate_many.py -g -2
usage: simulate_many.py [-h] [-g GAMES] [-d] [-l LEAGUE]
simulate_many.py: error: argument -g/--games: invalid positive int value: '-2'

For now I'm doing this, and I guess I'm happy:

现在我正在这样做,我想我很高兴:

if args.games <= 0:
    parser.print_help()
    print "-g/--games: must be positive."
    sys.exit(1)

采纳答案by Yuushi

This should be possible utilizing type. You'll still need to define an actual method that decides this for you:

这应该可以利用type. 您仍然需要定义一个实际的方法来为您决定:

def check_positive(value):
    ivalue = int(value)
    if ivalue <= 0:
        raise argparse.ArgumentTypeError("%s is an invalid positive int value" % value)
    return ivalue

parser = argparse.ArgumentParser(...)
parser.add_argument('foo', type=check_positive)

This is basically just an adapted example from the perfect_squarefunction in the docson argparse.

这基本上是从刚刚适应的例子perfect_square在函数文档argparse

回答by ben author

The quick and dirty way, if you have a predictable max as well as min for your arg, is use choiceswith a range

如果您的 arg 有可预测的最大值和最小值,则快速而肮脏的方法是choices与范围一起使用

parser.add_argument('foo', type=int, choices=xrange(0, 1000))

回答by aneroid

typewould be the recommended option to handle conditions/checks, as in Yuushi's answer.

type将是处理条件/检查的推荐选项,如 Yuushi 的回答。

In your specific case, you can also use the choicesparameter if your upper limit is also known:

在您的特定情况下,choices如果您的上限也已知,您也可以使用该参数:

parser.add_argument('foo', type=int, choices=xrange(5, 10))

Note:Use rangeinstead of xrangefor python 3.x

注意:使用range代替xrangepython 3.x

回答by Acumenus

A simpler alternative, especially if subclassing argparse.ArgumentParser, is to initiate the validation from inside the parse_argsmethod.

一个更简单的替代方法,特别是在子类化时argparse.ArgumentParser,是从parse_args方法内部启动验证。

Inside such a subclass:

在这样一个子类中:

def parse_args(self, args=None, namespace=None):
    """Parse and validate args."""
    namespace = super().parse_args(args, namespace)
    if namespace.games <= 0:
         raise self.error('The number of games must be a positive integer.')
    return namespace

This technique may not be as cool as a custom callable, but it does the job.

这种技术可能不像自定义可调用函数那么酷,但它可以完成这项工作。



About ArgumentParser.error(message):

关于ArgumentParser.error(message)

This method prints a usage message including the message to the standard error and terminates the program with a status code of 2.

此方法打印一条包含标准错误消息的用法消息,并以状态代码 2 终止程序。



Credit: answer by jonatan

信用:乔纳坦的回答

回答by pallgeuer

In case someone (like me) comes across this question in a Google search, here is an example of how to use a modular approach to neatly solve the more general problem of allowing argparse integers only in a specified range:

如果有人(像我一样)在 Google 搜索中遇到这个问题,这里有一个示例,说明如何使用模块化方法巧妙地解决更一般的问题,即允许指定范围内的argparse 整数:

# Custom argparse type representing a bounded int
class IntRange:

    def __init__(self, imin=None, imax=None):
        self.imin = imin
        self.imax = imax

    def __call__(self, arg):
        try:
            value = int(arg)
        except ValueError:
            raise self.exception()
        if (self.imin is not None and value < self.imin) or (self.imax is not None and value > self.imax):
            raise self.exception()
        return value

    def exception(self):
        if self.imin is not None and self.imax is not None:
            return argparse.ArgumentTypeError(f"Must be an integer in the range [{self.imin}, {self.imax}]")
        elif self.imin is not None:
            return argparse.ArgumentTypeError(f"Must be an integer >= {self.imin}")
        elif self.imax is not None:
            return argparse.ArgumentTypeError(f"Must be an integer <= {self.imax}")
        else:
            return argparse.ArgumentTypeError("Must be an integer")

This allows you to do something like:

这允许您执行以下操作:

parser = argparse.ArgumentParser(...)
parser.add_argument('foo', type=IntRange(1))     # Must have foo >= 1
parser.add_argument('bar', type=IntRange(1, 7))  # Must have 1 <= bar <= 7

The variable foonow allows only positive integers, like the OP asked.

变量foo现在只允许正整数,就像 OP 所要求的那样。

Note that in addition to the above forms, just a maximum is also possible with IntRange:

请注意,除了上述形式外,还可以使用最大值IntRange

parser.add_argument('other', type=IntRange(imax=10))  # Must have other <= 10