commit 4afc8b54e590488a6fbde81a8e31ee42b6dec3c5
parent e72293d63a232ccb2c513d40ad7b06cac583a974
Author: lash <dev@holbrook.no>
Date: Wed, 11 May 2022 12:42:52 +0000
Add append xarg example
Diffstat:
2 files changed, 25 insertions(+), 13 deletions(-)
diff --git a/aiee/flag.py b/aiee/flag.py
@@ -185,21 +185,21 @@ class Arg:
def kwargs(self, k):
- return self.__x[k]
+ return self.__x.get(k, {})
def process_args(argparser, args, flags):
for flag in args:
for (short, long, dest, typ,) in args.get(flag):
kw = args.kwargs(short[1])
+
+ if typ == bool:
+ kw['action'] = 'store_true'
+ kw['dest'] = dest
+ kw['type'] = typ
+
if long == None:
- if kw == None:
- argparser.add_argument(short, type=typ, dest=dest)
- else:
- argparser.add_argument(short, type=typ, dest=dest, **kw)
+ argparser.add_argument(short, **kw)
else:
- if kw == None:
- argparser.add_argument(short, long, type=typ, dest=dest)
- else:
- argparser.add_argument(short, long, type=typ, dest=dest, **kw)
+ argparser.add_argument(short, long, **kw) # type=typ, dest=dest)
return argparser
diff --git a/tests/test_arg.py b/tests/test_arg.py
@@ -130,11 +130,23 @@ class TestArg(unittest.TestCase):
def test_xargs(self):
- flag = ArgFlag()
- flag.add('foo')
+ flags = ArgFlag()
+ flags.add('foo')
- arg = Arg(flag)
- arg.add('x', 'foo', help='foo', me=42)
+ args = Arg(flags)
+ args.add('x', 'foo', action='append', help='bah')
+
+ argparser = argparse.ArgumentParser()
+ argparser = process_args(argparser, args, flags)
+
+ r = argparser.parse_args(['-x', '13', '-x', '42'])
+ self.assertListEqual(r.x, ['13', '42'])
+
+ args.add('y', 'foo', help='snuh', me='bar')
+ argparser = argparse.ArgumentParser()
+
+ with self.assertRaises(TypeError):
+ process_args(argparser, args, flags)
if __name__ == '__main__':