There is a standard library called argparse that supports the argument-related processing of Python scripts. With argparse, you can add valid arguments, including validation checks. This time, I will describe how to specify the argument of "there is a valid range of numerical values" with argparse.
argparse is a Python library that parses command line options and arguments. It is available from Python 2.7 and is included as a standard library (so you don't have to bother to use pip and you don't have to add "please put this library" when distributing it to other users).
Official Python documentation argparse https://docs.python.jp/3/library/argparse.html
In addition to argparse, there are the following Python libraries that perform this kind of analysis.
Option parser that Pythonista knows http://qiita.com/petitviolet/items/aad73a24f41315f78ee4
To include the argument analysis process using argparse, implement as follows. (The following adds the integer value option "-s".)
Argument check sample.py
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='LightsOut Solver')
parser.add_argument('-s', type=int, help='LightsOut Size')
ʻIf you specify string or int in the argument type
of ArgumentParser # add_argument`, it will check whether the value specified as the argument can be treated as that type.
In order to specify "tolerance range" as a condition for adding arguments as described above, it is OK to enter a value indicating the tolerance range in the argument choice
of ʻArgumentParser # add_argument`.
Argument check sample2.py
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='LightsOut Solver')
parser.add_argument('-s', type=int, choice=range(1,6), help='LightsOut Size')
However, you can specify a solid value for this choice only if it can be represented by an array. In other words, only a finite number of ranges can be narrowed down. To specify an infinite number of ranges (for example, "integer of 2 or more" or "real number of 0 or more and 1 or less"), "function to judge whether it is an acceptable value" instead of "array that specifies the range" It is OK if you specify. Below is a sample code that specifies an integer greater than or equal to 2 as a range.
Argument check sample3.py
class range_check(object):
def __init__(self, low_limit=None, high_limit=None, vtype="integer"):
self.min = low_limit
self.max = high_limit
self.type = vtype
def __contains__(self, val):
ret = True
if self.min is not None:
ret = ret and (val >= self.min)
if self.max is not None:
ret = ret and (val <= self.max)
return ret
def __iter__(self):
low = self.min
if low is None:
low = "-inf"
high = self.max
if high is None:
high = "+inf"
L1 = self.type
L2 = " {} <= x <= {}".format(low, high)
return iter((L1, L2))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='LightsOut Solver')
parser.add_argument('-s', type=int, choices=range_check(low_limit=2), help='LightsOut Size')
In addition, how to describe the sample project built using this code. https://github.com/LyricalMaestro/lightsout-solver-python
If you make full use of the check method, you can use argparse to perform validation check with a high degree of freedom. Use it to create great scripts.
Official Python documentation argparse https://docs.python.jp/3/library/argparse.html
Option parser that Pythonista knows http://qiita.com/petitviolet/items/aad73a24f41315f78ee4