aiee

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

commit ec20ebf7684a9d77fd3339f0ab0bbbd6915920a6
parent f7ec82c08944ec2b14637968fe01c65d3e87876f
Author: lash <dev@holbrook.no>
Date:   Wed, 11 May 2022 11:37:40 +0000

Add type setting for arg

Diffstat:
A.gitignore | 5+++++
Maiee/flag.py | 54+++++++++++++++++++++++++++++++++++++++++++++++++++---
Arun_tests.sh | 14++++++++++++++
Mtests/test_arg.py | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 134 insertions(+), 3 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,5 @@ +__pycache__ +*.pyc +*.egg-info +dist/ +build/ diff --git a/aiee/flag.py b/aiee/flag.py @@ -102,7 +102,6 @@ class ArgFlag: return self.val(v) - class Arg: def __init__(self, flags): @@ -111,9 +110,12 @@ class Arg: self.__long = {} self.__k = [] self.__dest = {} + self.__x = {} + self.__crsr = 0 + self.__typ = {} - def set(self, k, v, dest=None): + def set(self, k, v, typ=str, dest=None, **kwargs): if len(k) != 1: raise ValueError('short flag must have length 1, got "{}"'.format(k)) v = self.__flags.val(v) @@ -126,6 +128,9 @@ class Arg: dest = k self.__dest[k] = dest + self.__x[k] = kwargs + self.__typ[k] = typ + def set_long(self, short, long, dest=None): if len(short) != 1: @@ -149,5 +154,48 @@ class Arg: if long != None: long = '--' + long dest = self.__dest[short] + typ = self.__typ[short] short = '-' + short - return (short, long, dest,) + return (short, long, dest, typ,) + + + def __iter__(self): + self.__crsr = 1 + return self + + + def __next__(self): + while True: + v = None + try: + v = self.__flags.val(self.__crsr) + except ValueError: + self.__crsr = 0 + if self.__crsr == 0: + raise StopIteration() + + v = self.__crsr + self.__crsr <<= 1 + if self.__v.get(v) != None: + return v + + + def kwargs(self, k): + return self.__x[k] + + +def process_args(argparser, arg, flags): + for flag in arg: + (short, long, dest, typ) = arg.get(flag) + kw = arg.kwargs(short[1]) + if long == None: + if kw == None: + argparser.add_argument(short, type=typ, dest=dest) + else: + argparser.add_argument(short, type=typ, dest=dest, **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) + return argparser diff --git a/run_tests.sh b/run_tests.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +set -a +set -e +set -x +default_pythonpath=$PYTHONPATH:. +export PYTHONPATH=${default_pythonpath:-.} +>&2 echo using pythonpath $PYTHONPATH +for f in `ls tests/*.py`; do + python $f +done +set +x +set +e +set +a diff --git a/tests/test_arg.py b/tests/test_arg.py @@ -1,10 +1,12 @@ # standard imports import unittest +import argparse # local imports from aiee.flag import ( ArgFlag, Arg, + process_args, ) @@ -42,6 +44,68 @@ class TestArg(unittest.TestCase): self.assertEqual(r[1], '--yy') self.assertEqual(r[2], 'yyyyy') + + + def test_arg_iter(self): + flags = ArgFlag() + flags.add('foo') + flags.add('bar') + + r = [] + arg = Arg(flags) + for flag in arg: + r.append(flag) + + self.assertEqual(len(r), 0) + + r = [] + arg.set('y', 'bar') + for flag in arg: + r.append(flag) + self.assertListEqual(r, [flags.BAR]) + + r = [] + arg.set('x', 'foo') + for flag in arg: + r.append(flag) + self.assertListEqual(r, [flags.FOO, flags.BAR]) + + + def test_process_argparser(self): + flags = ArgFlag() + flags.add('foo') + + arg = Arg(flags) + arg.set('x', 'foo') + + argparser = argparse.ArgumentParser() + argparser = process_args(argparser, arg, flags) + r = argparser.parse_args(['-x', '13']) + + self.assertEqual(r.x, '13') + + + def test_process_argparser_typ(self): + flags = ArgFlag() + flags.add('foo') + + arg = Arg(flags) + arg.set('x', 'foo', typ=int) + + argparser = argparse.ArgumentParser() + argparser = process_args(argparser, arg, flags) + + r = argparser.parse_args(['-x', '13']) + + self.assertEqual(r.x, 13) + + + def test_xargs(self): + flag = ArgFlag() + flag.add('foo') + + arg = Arg(flag) + arg.set('x', 'foo', help='foo', me=42) if __name__ == '__main__':