Package: cli-cake
CLI-cakeEasily pass CLI args into functions. Turn your functions into scripts.CLI-cake takes this:importsysdefecho(*args,capitalize=False):ifcapitalize:args=[a.upper()forainargs]print(' '.join(args))caps=Falseforarginargs:ifarg=='--capitalize':caps=Trueecho(sys.argv[1:],caps)and turns it into thisimportcake@cake.runnabledefecho(*args,capitalize=False):ifcapitalize:args=[a.upper()forainargs]print(' '.join(args))echo.runCLI()$pythonecho.pyhelloworld--capitalize
HELLOWORLDWhyI realized that when I'm building quick, simple scripts for internal use, I resort to one of two approaches:I just hardcode the values into my script, and just change the values each time I use itI build out some type of custom CLI or gui interface for the scriptBoth of these approaches have downsides- with the first approach, you end up spending more time between runs editing code, and end up with the bad practice of directly editing your source code for non-permanent changes. With the second approach, you end up spending extra time up front writing code, and it can make concise scripts more messy by adding in this extra code.clicake is a bunch of convenience methods that solve these problems, making it super quick and easy to transform your functions into fully-featured CLI-callable scripts.InstallingGetting Startedclicake is really easy to use! Try creating the file 'echo.py' and add the following code:importcake@cake.runnabledefecho(*args,capitalize=False):ifcapitalize:args=[a.upper()forainargs]print(' '.join(args))if__name__=='__main__':echo()ORimportcakedefecho(*args,capitalize=False):ifcapitalize:args=[a.upper()forainargs]print(' '.join(args))if__name__=='__main__':cake.run(echo)And in your terminal$pythonecho.pyhelloworld--capitalize
HELLOWORLDDocumentationDecoratorsdefrunnable(callback,args,print_output):passMethodsdefrun(callback):passObjectsclassCake():defrunnable(callback,args,print_output):passdefrun(callback):passExamplesA simple example:importcake@cake.runnabledefecho(*args,capitalize=False):ifcapitalize:args=[a.upper()forainargs]print(' '.join(args))echo.runCLI()$pythonecho.pyhelloworld--capitalize
HELLOWORLDMultiple MethodsUsing the Cake() object allows you to build runners that can handle multiple functions from the command line. The 'first' argument the script recieves (argv[1]) is interpreted as the name of the function, which can be set explicitly through thenameparameter, or defaults to the method name.Take the following code asexample.py:importcakecli=cake.Cake()@cli.runnabledefecho(*args):return(' '.join([str(a)forainargs]))@cli.runnable(name="add")defsum(*args):_sum=0forarginargs:_sum+=argreturn_sumcli.run()$pythonexample.pyechohelloworld
helloworld

$pythonexample.pyadd1234515Defining a method as runnable also doesn't modify the__call__method for the decorated function, the only change with the wrapped function is it adds a callablerunCLIattribute to the function that can be used to tell the program to parse the CLI args and execute the function based on those args. This lets us use @runnable decorated functions without having to modify dependent codeimportcakecli=cake.Cake()@cli.runnabledefsum(*args):_sum=0forarginargs:_sum+=argreturn_sum@cli.runnabledefdoublesum(*args):returnsum(*args)*2# sum function can be called like normal!cli.run()$pythonexample.pysum1234515$pythonexample.pydoublesum1234530TestingFrom root, you can run the tests withpython-mcake.testsBuild / DeployInstall build dependencies locally:pip install --user --upgrade twine setuptools wheelBuild source and distribution files:python setup.py sdist bdist_wheelupload to PyPi with Twine:python -m twine upload dist/*You'll be prompted to use__token__as the username, and you'll need to use an API token from PyPi as the password.Planned Features (todo)Add support for specifying explicit types, and code hydration for passing in lambdasAdd support for input() based text interfaces in addition to just CLI args? (This is already decently well supported by just running the python interactively and importing the desired functions... will evaluate to see if there's actually a need here)Add option to wrap errors with 'pretty' printed outputAdd option to specify printed output streamAdd more extensive testing / unit testsimproved documentationAdvanced TopicspyinstallerI'd recommend usingpyinstallerif you want to generate standalone executable script builds in addition to cli-cake. Read more on there sitehere.