def test_default(self): def f(must: int, optional: int = 0): return must + optional assert cmdfunc(f).parse(['1']) == 1 assert cmdfunc(f).parse(['1', '-o', '2']) == 3 assert cmdfunc(f).parse(['1', '--optional', '2']) == 3
def test_basic(self): def plus(a, b): return a + b def plusTyped(a: int, b: int) -> int: return a + b assert cmdfunc(plus).parse(['1', '1']) == '11' assert cmdfunc(plusTyped).parse(['1', '1']) == 2
def test_choices(self): class LEFT_OR_RIGHT(Enum): LEFT = 'LEFT' RIGHT = 'RIGHT' def whereAreYouGoing(d: LEFT_OR_RIGHT) -> str: if (d == LEFT_OR_RIGHT.LEFT): return 'GOING LEFT !' elif (d == LEFT_OR_RIGHT.RIGHT): return 'GOING RIGHT' assert cmdfunc(whereAreYouGoing).parse(['LEFT']) == 'GOING LEFT !' assert cmdfunc(whereAreYouGoing).parse(['RIGHT']) == 'GOING RIGHT'
def test_unsupportedType(self): class T: pass def unsupType(i: T): pass def unsupTypeArg(i: List[T]): pass with self.assertRaises(UnsupportedTypeException): cmdfunc(unsupType) with self.assertRaises(UnsupportedTypeException): cmdfunc(unsupTypeArg)
def test_boolean(self): def isItTrue(b: bool): if (b): return 'yes' else: return 'no' assert cmdfunc(isItTrue).parse([]) == 'no' assert cmdfunc(isItTrue).parse(['-b']) == 'yes' def isItTrueWithDefault(b: bool = True): if (b): return 'yes' else: return 'no' assert cmdfunc(isItTrueWithDefault).parse([]) == 'yes' assert cmdfunc(isItTrueWithDefault).parse(['-b']) == 'no'
def test_enum(self): #TEST NON HOMOGENOUS class nonHomogenous(Enum): a = 1 b = '2' def nonHomogenousFunc(t: nonHomogenous): pass # check that s.split fails when the separator is not a string with self.assertRaises(NonHomogenousEnumTypeException): cmdfunc(nonHomogenousFunc) #TEST NO TYPE class noType(Enum): pass def noTypeFunc(t: noType): pass # check that s.split fails when the separator is not a string with self.assertRaises(EnumHasNoTypeException): cmdfunc(noTypeFunc)
#examples/choices.py from autofunccli import cmdfunc #you have to import the Enum class first from enum import Enum class LEFT_OR_RIGHT(Enum): LEFT = 'LEFT' RIGHT = 'RIGHT' def whereAreYouGoing(d:LEFT_OR_RIGHT)->str: if(d==LEFT_OR_RIGHT.LEFT): return 'GOING LEFT !' elif(d==LEFT_OR_RIGHT.RIGHT): return 'GOING RIGHT' out = cmdfunc(whereAreYouGoing).main(__name__) if(out != None): print(out)
def test_input(self): def integer(i: int): return i with self.assertRaises(WrongInputTypeException): cmdfunc(integer).parse(['a'])
def test_tuple(self): def color(c: Tuple[int, int, int]) -> str: return "RGB({},{},{})".format(c[0], c[1], c[2]) assert cmdfunc(color).parse(['1', '2', '3']) == 'RGB(1,2,3)'
def test_list(self): def sum(l: List[int]) -> int: return reduce(lambda prev, act: prev + act, l) assert cmdfunc(sum).parse(['1', '2', '3']) == 6
#examples/list.py from autofunccli import cmdfunc from typing import List from functools import reduce def sum(l: List[int]) -> int: return reduce(lambda prev, act: prev + act, l) out = cmdfunc(sum).main(__name__) if (out != None): print(out)
#examples/default.py from autofunccli import cmdfunc def f(must:int,optional:int=0): return must+optional out = cmdfunc(f).main(__name__) if(out != None): print(out)
#examples/tuple.py from autofunccli import cmdfunc from typing import Tuple def color(c:Tuple[int,int,int])->str: return "RGB({},{},{})".format(c[0],c[1],c[2]) out = cmdfunc(color).main(__name__) if(out != None): print(out)
#examples/bool.py from autofunccli import cmdfunc def isItTrue(b: bool): if (b): return 'yes' else: return 'no' out = cmdfunc(isItTrue).main(__name__) if (out != None): print(out)
#examples/basic.py from autofunccli import cmdfunc #define a simple plus function def plus(a: int, b: int) -> int: return a + b #Use it as a main #.main test if __name__ == '__main__' # if it's the case, parse the command line input out = cmdfunc(plus).main(__name__) if (out != None): print(out)