aiee

Python modules for common command line interfacing utils
git clone git://git.defalsify.org/aiee.git
Log | Files | Refs | LICENSE

commit a0278043e387fcb88993172f003b9ec656277e41
parent 4afc8b54e590488a6fbde81a8e31ee42b6dec3c5
Author: lash <dev@holbrook.no>
Date:   Wed, 11 May 2022 12:59:45 +0000

Omit generation of args for missing flags

Diffstat:
Maiee/flag.py | 12+++++++++---
Mtests/test_arg.py | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
2 files changed, 68 insertions(+), 7 deletions(-)

diff --git a/aiee/flag.py b/aiee/flag.py @@ -190,16 +190,22 @@ class Arg: def process_args(argparser, args, flags): for flag in args: + + if flag & flags == 0: + continue + for (short, long, dest, typ,) in args.get(flag): + kw = args.kwargs(short[1]) if typ == bool: kw['action'] = 'store_true' + else: + kw['type'] = typ kw['dest'] = dest - kw['type'] = typ if long == None: argparser.add_argument(short, **kw) else: - argparser.add_argument(short, long, **kw) # type=typ, dest=dest) - return argparser + argparser.add_argument(short, long, **kw) + return argparser diff --git a/tests/test_arg.py b/tests/test_arg.py @@ -92,7 +92,7 @@ class TestArg(unittest.TestCase): arg.add('x', 'foo') argparser = argparse.ArgumentParser() - argparser = process_args(argparser, arg, flags) + argparser = process_args(argparser, arg, flags.FOO) r = argparser.parse_args(['-x', '13']) self.assertEqual(r.x, '13') @@ -107,13 +107,53 @@ class TestArg(unittest.TestCase): arg.add('y', 'foo') argparser = argparse.ArgumentParser() - argparser = process_args(argparser, arg, flags) + argparser = process_args(argparser, arg, flags.FOO) r = argparser.parse_args(['-x', '13', '-y', '42']) self.assertEqual(r.x, '13') self.assertEqual(r.y, '42') + def test_process_argparser_multi_alias(self): + flags = ArgFlag() + flags.add('foo') + flags.add('bar') + flags.alias('baz', 'foo', 'bar') + + arg = Arg(flags) + arg.add('x', 'foo') + arg.add('y', 'bar') + + argparser = argparse.ArgumentParser() + argparser = process_args(argparser, arg, flags.BAZ) + r = argparser.parse_args(['-x', '13', '-y', '42']) + + self.assertEqual(r.x, '13') + self.assertEqual(r.y, '42') + + + def test_process_argparser_multi_alias_selective(self): + flags = ArgFlag() + flags.add('foo') + flags.add('bar') + flags.alias('baz', 'foo', 'bar') + + arg = Arg(flags) + arg.add('y', 'bar') + + argparser = argparse.ArgumentParser() + argparser = process_args(argparser, arg, flags.BAZ) + r = argparser.parse_args(['-y', '42']) + + self.assertEqual(r.y, '42') + + argparser = argparse.ArgumentParser() + argparser = process_args(argparser, arg, flags.FOO) + + with self.assertRaises(SystemExit): + r = argparser.parse_args(['-y', '42']) + + def test_process_argparser_typ(self): flags = ArgFlag() flags.add('foo') @@ -122,7 +162,7 @@ class TestArg(unittest.TestCase): arg.add('x', 'foo', typ=int) argparser = argparse.ArgumentParser() - argparser = process_args(argparser, arg, flags) + argparser = process_args(argparser, arg, flags.FOO) r = argparser.parse_args(['-x', '13']) @@ -137,7 +177,7 @@ class TestArg(unittest.TestCase): args.add('x', 'foo', action='append', help='bah') argparser = argparse.ArgumentParser() - argparser = process_args(argparser, args, flags) + argparser = process_args(argparser, args, flags.FOO) r = argparser.parse_args(['-x', '13', '-x', '42']) self.assertListEqual(r.x, ['13', '42']) @@ -149,5 +189,20 @@ class TestArg(unittest.TestCase): process_args(argparser, args, flags) + def test_bool_type(self): + flags = ArgFlag() + flags.add('foo') + + args = Arg(flags) + args.add('x', 'foo', typ=bool) + + argparser = argparse.ArgumentParser() + argparser = process_args(argparser, args, flags.FOO) + + r = argparser.parse_args(['-x']) + + self.assertIsInstance(r.x, bool) + + if __name__ == '__main__': unittest.main()