The Python Oracle

In Python, using argparse, allow only positive integers

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------


Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT


Music by Eric Matyas
https://www.soundimage.org
Track title: Digital Sunset Looping

--

Chapters
00:00 In Python, Using Argparse, Allow Only Positive Integers
01:03 Accepted Answer Score 320
01:26 Answer 2 Score 15
01:40 Answer 3 Score 90
02:03 Answer 4 Score 14
02:57 Thank you

--

Full question
https://stackoverflow.com/questions/1411...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #argparse

#avk47



ACCEPTED ANSWER

Score 320


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.




ANSWER 2

Score 90


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




ANSWER 3

Score 15


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



ANSWER 4

Score 14


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