def main(): """Testing 'argparse-addons' package""" parser = argparse.ArgumentParser() parser.add_argument( "-v", "--verbosity", dest="verbosity", action="count", default=0, help="set verbosity level", ) # start test arguments # https://github.com/eerimoq/argparse_addons/blob/af6c473f176a41b26c17da354c008bf87257bfe6/examples/integer.py parser.add_argument("--min-max", type=argparse_addons.Integer(0, 255)) parser.add_argument("--min", type=argparse_addons.Integer(0, None)) parser.add_argument("--max", type=argparse_addons.Integer(None, 255)) parser.add_argument("--any", type=argparse_addons.Integer()) # end args = parser.parse_args() if args.verbosity == 1: logging.basicConfig(level=logging.INFO) elif args.verbosity > 1: logging.basicConfig(level=logging.DEBUG) else: logging.basicConfig(level=logging.ERROR) logging.debug(f"{argparse_addons.__name__} {argparse_addons.__version__}")
def test_integer(self): integer = argparse_addons.Integer() self.assertEqual(integer('0'), 0) self.assertEqual(integer('0x1a'), 0x1a) with self.assertRaises(ValueError): integer('ab')
def test_integer_max(self): integer = argparse_addons.Integer(None, 5) self.assertEqual(integer('5'), 5) self.assertEqual(integer('-111'), -111) with self.assertRaises(argparse.ArgumentTypeError) as cm: integer('6') self.assertEqual(str(cm.exception), '6 is not in the range -inf..5')
def test_integer_min(self): integer = argparse_addons.Integer(0, None) self.assertEqual(integer('0'), 0) self.assertEqual(integer('100000'), 100000) with self.assertRaises(argparse.ArgumentTypeError) as cm: integer('-1') self.assertEqual(str(cm.exception), '-1 is not in the range 0..inf')
def test_integer_min_and_max(self): integer = argparse_addons.Integer(0, 255) self.assertEqual(integer('0'), 0) self.assertEqual(integer('255'), 255) with self.assertRaises(argparse.ArgumentTypeError) as cm: integer('-1') self.assertEqual(str(cm.exception), '-1 is not in the range 0..255') with self.assertRaises(argparse.ArgumentTypeError) as cm: integer('256') self.assertEqual(str(cm.exception), '256 is not in the range 0..255') with self.assertRaises(ValueError): integer('foobar')
import argparse import argparse_addons parser = argparse.ArgumentParser() parser.add_argument('--min-max', type=argparse_addons.Integer(0, 255), help='an integer in the range 0..255') parser.add_argument('--min', type=argparse_addons.Integer(0, None), help='an integer in the range 0..inf') parser.add_argument('--max', type=argparse_addons.Integer(None, 255), help='an integer in the range -inf..255') parser.add_argument('--any', type=argparse_addons.Integer(), help='any integer') args = parser.parse_args() print(f'--min-max: {args.min_max}') print(f'--min: {args.min}') print(f'--max: {args.max}') print(f'--any: {args.any}')
def test_integer_hex(self): integer = argparse_addons.Integer(0, 5) self.assertEqual(integer('0x1'), 1)
def test_integer_repr(self): self.assertEqual(repr(argparse_addons.Integer(0, 1)), 'integer')