问题:在Python中,使用argparse,仅允许使用正整数

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

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

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

并输出:

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

输出为负:

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

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

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

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'

像这样:

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'

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

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

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.games is negative, but I was curious if there was a way to trap it at the argparse level, so as to take advantage of the automatic usage printing.

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)

回答 0

这应该是可以利用的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)

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

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

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_square function in the docs on argparse.


回答 1

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

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

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

注:使用range的,而不是xrange为Python 3.X

type would be the recommended option to handle conditions/checks, as in Yuushi’s answer.

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

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

Note: Use range instead of xrange for python 3.x


回答 2

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

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

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

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

回答 3

一个更简单的替代方法(尤其是在子类化的情况下argparse.ArgumentParser)是从parse_args方法内部启动验证。

在这样的子类中:

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

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


关于ArgumentParser.error(message)

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


信用:乔纳丹回答

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

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

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


Credit: answer by jonatan


回答 4

如果有人(像我一样)在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")

这使您可以执行以下操作:

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

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

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

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

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:

# 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 foo now allows only positive integers, like the OP asked.

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

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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。