def test_degenerated_b(capsys): assert calf.call(func0b, []), 'hello' with pytest.raises(SystemExit): calf.call(func0b, ['-h']) captured = capsys.readouterr() assert 'Say hello' in captured.out assert 'Constant' not in captured.out
def test_simple(capsys): assert calf.call(func1, ['12', '--var3', 'x']) == (12, False, 'x', 'bar') assert calf.call(func1, ['12', '-f', '-v', 'foo', '-x', '5']) == (12, True, '5', 'foo') with pytest.raises(SystemExit): calf.call(func1, ['x', '--var3', 'x']) captured = capsys.readouterr() assert 'cannot be converted' in captured.err
def main(argv: typing.Optional[typing.List[str]] = None) -> None: "The main function for your function" args = (sys.argv if argv is None else argv)[1:] fullname = args.pop(0) if fullname in (None, '-h', '--help'): print('Usage: calf <module>.<func> ...\n', file=sys.stderr) print('Use "calf <module>.<func> -h" for help', file=sys.stderr) sys.exit(1) modname, _, func = fullname.rpartition('.') mod = importlib.import_module(modname) calf.call(getattr(mod, func), args, prog='calf ' + fullname)
def test_help(capsys): with pytest.raises(SystemExit): calf.call(func1, ['-h']) captured = capsys.readouterr() assert ' -f, ' in captured.out with pytest.raises(SystemExit): calf.call(func1s, ['-h'], doc_parser=calf.sphinx_doc_parser) captured = capsys.readouterr() assert ' -f, ' in captured.out with pytest.raises(SystemExit): calf.call(func1n, ['-h'], doc_parser=calf.numpy_doc_parser) captured = capsys.readouterr() assert ' -f, ' in captured.out
def main() -> None: import calf calf.call(makefile_menu)
def cronrepo_mgr() -> None: "Main entry of the manager" import calf # pylint: disable=import-outside-toplevel calf.call(mgr)
2: 'Have a nice day.', 3: 'Nice to meet you.', } # Define your function just like other functions, but note the (-s) as # short option names, and {1, 2, 3} as choices. def hello(name: str = 'Isaac', *args, style=2, **kwargs: float) -> None: """Say hello Args: name: name to say hello to style: (-s) style, choose among {1, 2, 3} args: values kwargs: keyword values """ print('Hello,', name) print(GREETING[style]) print(args) print(kwargs) # Your main program just use calf.call to call your CLI function. if __name__ == '__main__': calf.call(hello)
#!/usr/bin/env python3 "Show how to define a function with complex params as CLI using calf" import datetime def nextday(date: datetime.date): print('Next day of %s is %s' % (date, date + datetime.timedelta(1))) if __name__ == '__main__': import calf def _dateconv(datestr): return datetime.datetime.strptime(datestr, '%Y-%m-%d').date() calf.CONVERTERS[datetime.date] = _dateconv calf.call(nextday)
def test_degenerated_c(capsys): assert calf.call(func0c, []), 'hello' with pytest.raises(SystemExit): calf.call(func0c, ['-h']) captured = capsys.readouterr() assert 'func0c' in captured.out
def test_kwargs_only(): assert calf.call(func4, ['bar=42']) == {'bar': '42'} with pytest.raises(ValueError): calf.call(func4, ['bar'])
def test_pos_default(): assert calf.call(func3, ['42', '15', 'bar']) == (42, 15, 'bar') assert calf.call(func3, ['42']) == (42, None, 'foo')
def test_varargs(): assert calf.call(func2, ['foo', 'bar']) == (('foo', 'bar'), {}) assert calf.call(func2, ['foo1=bar', 'foo2=baz']) \ == ((), {'foo1': 'bar', 'foo2': 'baz'}) assert calf.call(func2, ['foo', 'foo1=bar', 'bar']) \ == (('foo', 'bar'), {'foo1': 'bar'})
def test_degenerated_a(): assert calf.call(func0a, []), 'hello' assert calf.call(func0a, [], doc_parser=calf.sphinx_doc_parser), 'hello' assert calf.call(func0a, [], doc_parser=calf.numpy_doc_parser), 'hello'