I use argparseto analyze the input of my python3 program. I was recently asked to check the range of some numerical inputs, a seemingly good idea. Argparse has the ability to do this.
Numeric inputs are port numbers in the normal range of 0-65535, so I changed the parsing command line to:
import argparse
cmd_parser = argparse.ArgumentParser()
cmd_parser = add_argument('-p', help='Port number to connect to', dest='cmd_port', default=1234, type=int, choices=range(0,65536))
cmd_parser.parse_args(['-h'])
However, now when I request help, all possible values from argparse are bombarding me. eg.
optional arguments:
-h, --help show this help message and exit
-p {0,1,2,3,4,5,6,7,8,9,10,11,12,13 ...
65478,65479,65480,65481,65482,65483,65484,65485,65486,65487,65488,65489,
65490,65491,65492,65493,65494,65495,65496,65497,65498,65499,65500,65501,
65502,65503,65504,65505,65506,65507,65508,65509,65510,65511,65512,65513,
65514,65515,65516,65517,65518,65519,65520,65521,65522,65523,65524,65525,
65526,65527,65528,65529,65530,65531,65532,65533,65534,65535}
Port number to connect to
...
. (0-65535) - , ? if?
, , argparse . argparse, . https://docs.python.org/2/library/argparse.html